Title Link: http://poj.org/problem?id=1177
Analysis: Not very understanding of the scan line ~ ~ ~
Leave a hole.
Code:
/************************************************author:ltwycreated time:2015 January 17 Saturday 18:06 53 seconds file Name : 1.cpp*************************************************/#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<stack>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intMAXN =10010;intN;intX[MAXN];intnum =0;structline{inty, x1, x2; intF;} LINE[MAXN];structnode{intL,r; intCNT; intLF, RF; intnumseg; intC; BOOLLcover,rcover;} TREE[MAXN<<2];BOOLCMP (line A, line B) {returnA.y <b.y;}voidBuildintRtintLintR) {TREE[RT].L=l; TREE[RT].R=R; Tree[rt].cnt= Tree[rt].numseg = TREE[RT].C =0; Tree[rt].lcover= Tree[rt].rcover =false; TREE[RT].LF=X[l]; TREE[RT].RF=X[r]; if(L +1==R)return; intMid = (l+r) >>1; Build (Rt<<1, L, mid); Build (Rt<<1|1, Mid, R);}voidCalen (intRT) { if(Tree[rt].c >0) {tree[rt].numseg=1; Tree[rt].cnt= TREE[RT].RF-TREE[RT].LF; Tree[rt].lcover= Tree[rt].rcover =true; return; } if(TREE[RT].L +1==TREE[RT].R) {tree[rt].cnt=0; Tree[rt].numseg=0; Tree[rt].lcover= Tree[rt].rcover =false; } Else{tree[rt].cnt= tree[rt<<1].CNT + tree[rt<<1|1].cnt; Tree[rt].lcover= tree[rt<<1].lcover; Tree[rt].rcover= tree[rt<<1|1].rcover; Tree[rt].numseg= tree[rt<<1].numseg + tree[rt<<1|1].numseg; if(tree[rt<<1].rcover&&tree[rt<<1|1].lcover) Tree[rt].numseg--; }}voidUpdateintRT, line E) { if(Tree[rt].lf = = E.x1 && Tree[rt].rf = =e.x2) {tree[rt].c+=e.f; Calen (RT); return; } if(E.x2 <= tree[rt<<1].RF) Update (rt<<1, E); Else if(E.x1 >= tree[rt<<1|1].LF) Update (rt<<1|1, E); Else{Line Temp=e; Temp.x2= tree[rt<<1].rf; Update (RT<<1, temp); Temp=e; Temp.x1= tree[rt<<1|1].LF; Update (RT<<1|1, temp); } calen (RT);}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intX1,y1,x2,y2; while(SCANF ("%d", &n)! =EOF) {num=0; for(intI=1; i<=n; i++) {scanf ("%d %d%d%d", &x1, &y1, &X2, &y2); Line[num].y=Y1; Line[num].x1=X1; X[num]=X1; LINE[NUM].F=1; Line[num++].X2 =x2; Line[num].y=Y2; Line[num].x1=X1; X[num]=x2; LINE[NUM].F= -1; Line[num++].X2 =x2; } sort (line, line+num, CMP); Sort (x, x+num); intm = Unique (x, X+num)-x; Build (1,1, M); intAns =0; intLast =0; for(intI=0; i<num-1; i++) {Update (1, Line[i]); Ans+ = tree[1].NUMSEG *2* (line[i+1].Y-line[i].y); Ans+ = ABS (tree[1].CNT-Last ); Last= tree[1].cnt; } Update (1, line[num-1]); Ans+ = ABS (tree[1].CNT-Last ); printf ("%d\n", ans); } return 0;}
POJ 1177 Picture Segment tree assists in the scanning line rectangle perimeter and