Description
There is a number of disjoint vertical line segments in the plane. We say that segments is horizontally visible if they can be connected by a horizontal line segment that does not having Any common points with other vertical segments. Three different vertical segments is said to form a triangle of segments if each of the them is horizontally visible. How many triangles can is found in a given set of vertical segments? Test instructions roughly means to give you some line segments, asking if there are any public parts of the adjacent two segments, and no other line segments are blocked. First the X is sorted, then the enumeration, for each segment, first ask all the other segments on the interval, and then overwrite the update. For a col array, if 1 indicates that there are multiple segments on this interval, otherwise the number of segments is represented (this assumes that a segment numbered 0 is placed first.) PS: Can I say this question I have been debugging for an afternoon, just beginning to set COL to bool variable, the result ... T, has not found the error to ... Pain... ) code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#defineLson l,m,po*2#defineRson m+1,r,po*2+1using namespacestd;Const intn=8000*2;intcol[8008*2*4];BOOLmap1[8008][8008];voidPushdown (intPO) { if(col[po]>=0) {Col[po*2]=col[po*2+1]=Col[po]; COL[PO]=-1; }}voidUpdateintUlinturintUtintLintRintPO) { if(ul<=l&&ur>=R) {Col[po]=ut; return; } pushdown (PO); intM= (L+R)/2; if(ul<=M) Update (Ul,ur,ut,lson); if(ur>M) Update (Ul,ur,ut,rson); if(col[po*2]==col[po*2+1])//This is a pruning operation that can reduce the number of recursion. col[po]=col[po*2]; ElseCol[po]=-1;}voidQueryintQlintQrintQtintLintRintPO) { if(col[po]!=-1) {Map1[qt][col[po]]=map1[col[po]][qt]=1; return; } if(l==R)return; intM= (L+R)/2; if(ql<=M) query (Ql,qr,qt,lson); if(qr>M) query (Ql,qr,qt,rson);}structstate{inty1,y2,x1;}; State sta[8008];intcmpConst void*a,Const void*b) { return(* (state*) a). x1-(* (state*) b). X1;}intMain () {intD; CIN>>D; intN; while(d--) {memset (MAP1,0,sizeof(MAP1)); memset (COL,0,sizeof(COL)); CIN>>N; for(intI=0; i<n;++i) scanf (" %d%d%d",&sta[i].y1,&sta[i].y2,&sta[i].x1); Qsort (Sta,n,sizeof(state), CMP); for(intI=1; i<=n;++i) {query (Sta[i-1].y1*2, sta[i-1].y2*2I0N1); Update (Sta[i-1].y1*2, sta[i-1].y2*2I0N1); } Long Longans=0; for(intI=1; i<=n;++i)//Direct Brute force solution, incredibly not timed out ... for(intj=i+1; j<=n;++j)if(Map1[i][j]) for(intk=j+1; k<=n;++k)if(map1[i][k]&&Map1[j][k])++ans; cout<<ans<<Endl; } return 0;}
View Code
Medium POJ 1436 horizontally Visible segments, segment tree + interval update.