POJ1436Horizontally Visible Segments line segment tree + lazy, line segment tree lazy
Determine whether three lines are connected. If any two lines are connected, the three lines are connected; open a hash [I] [j] To save the relationship between the I-th line segment and the j-th line segment, before inserting a new line segment, you must first determine whether the line is connected to other line segments before inserting the line segment. PS: note that the x coordinates of all line segments must be sorted first, and then the line segments are inserted in the mature order;
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define maxn 8010int col[maxn*10];bool hash[maxn][maxn];struct bian{ int y1,y2,x;}edge[maxn];int cmp(bian a,bian b){ return a.x<b.x;}void pushdown(int rt){ if(~col[rt]) { col[rt<<1]=col[rt<<1|1]=col[rt]; col[rt]=-1; }}void build(int l,int r,int rt){ col[rt]=-1; if(l==r) return ; int m=(l+r)>>1; build(lson); build(rson);}void update(int L,int R,int cha,int l,int r,int rt){ if(L<=l&&R>=r) { col[rt]=cha; return ; } pushdown(rt); int m=(l+r)>>1; if(L<=m) update(L,R,cha,lson); if(R>m) update(L,R,cha,rson);}void query(int L,int R,int cha,int l,int r,int rt){ if(col[rt]!=-1) { hash[cha][col[rt]]=hash[col[rt]][cha]=cha; return ; } if(l==r) return ; int m=(l+r)>>1; if(L<=m) query(L,R,cha,lson); if(R>m) query(L,R,cha,rson);}int main(){ int t,n,a,b,c; scanf("%d",&t); while(t--) { memset(hash,0,sizeof(hash)); int MAX=-1; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d%d",&a,&b,&c); edge[i].y1=a*2;edge[i].y2=b*2; edge[i].x=c; MAX=max(MAX,edge[i].y2); } sort(edge,edge+n,cmp); build(0,MAX,1); for(int i=0;i<n;i++) { query(edge[i].y1,edge[i].y2,i,0,MAX,1); update(edge[i].y1,edge[i].y2,i,0,MAX,1); } int ans=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(!hash[i][j]) continue; for(int k=j+1;k<n;k++) { if(hash[i][k]&&hash[j][k]) ans++; } } } printf("%d\n",ans); } return 0;}