 /**//*
給出一些平行于axes或與坐標軸成45°的線段
問其中有多少個三角形
n <= 50 (x,y) ∈ [-100,100]
不會做,一點想法都沒有 哎
解題報告 http://www.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm239
規模比較小,直接枚舉三條邊
但有個問題就是有些邊可以連接起來,需要再處理

范圍比較小,可以放大兩倍(處理對角線交點),然后進行標記,看哪些邊是有線段的
mark[x,y,d] 表示點(x,y)在方向d的單位長度上有線段覆蓋
這種記錄方法很好!!!
pLen[x,y,d]表示點(x,y)在方向d能延伸多長
有了以上的預處理后
統計時,枚舉點(x,y),方向d,還有長度len(由于題目的三角形都是等腰的,所以枚舉長度即可)
對于(x,y,d),另外一條直角邊就行(x,y,d+2),斜邊為(x,y,d+3)
長度要判斷一下,跟角度有關
解題報告的代碼寫得真好??!
*/
#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<set>
#include<list>

using namespace std;

const int MAXN = 400;

 int dx[] = {1,1,0,-1,-1,-1,0,1};
 int dy[] = {0,1,1,1,0,-1,-1,-1};

bool mark[MAXN+1][MAXN+1][8];
int pLen[MAXN+1][MAXN+1][8];

 class HiddenTriangles {
public:

 int sign(int x) {∈
return x < 0 ? -1 : x > 0 ;
}
 void markIt(int x1,int y1,int x2,int y2) {
int d = 0;
while(dx[d] != sign(x2-x1) || dy[d] != sign(y2-y1))d++;
 while(x1!=x2 || y1!=y2) {// x1 = x2 && y1 = y2 , then break
mark[x1][y1][d] = true;
x1 += dx[d];
y1 += dy[d];
mark[x1][y1][(d+4)%8] = true;
}
}

 int calLen(int x ,int y , int d) {
 if(pLen[x][y][d] == -1) {
if(mark[x][y][d])pLen[x][y][d] = 1 + calLen(x+dx[d],y+dy[d],d);
else pLen[x][y][d] = 0;
}
return pLen[x][y][d];
}

 void calLen() {
memset(pLen , -1 , sizeof pLen);
for(int x = 0 ; x <= MAXN ; x++)
for(int y = 0 ; y <= MAXN ; y++)
for(int d = 0 ; d < 8 ; d++)
pLen[x][y][d] = calLen(x,y,d);
}
 int count() {
int ans = 0;
for(int x = 0 ; x <= MAXN ; x ++)
for(int y = 0 ; y <= MAXN ; y++)
 for(int d = 0 ; d < 8 ; d++) {
 for(int len = 1 ;;len++) {
if(pLen[x][y][d] < len || pLen[x][y][(d+2)%8] < len)break;
int side = d % 2 == 0 ? 1 : 2; // parallel to axes : form a 45 degree angle
if(pLen[x+dx[d]*len][y+dy[d]*len][(d+3)%8] >= len*side)ans++;
}
}
return ans;
}

 int howMany(vector <int> X1, vector <int> Y1, vector <int> X2, vector <int> Y2) {
memset(mark,false,sizeof mark);
int n = X1.size();
 for(int i = 0 ; i < n ; i++) {
markIt(2*X1[i]+200,2*Y1[i]+200,2*X2[i]+200,2*Y2[i]+200);//extend it
}
calLen();
return count();
}

};

|