poj 1436 Horizontally Visible Segments - 线段树区间更新
2017-10-02 19:58
369 查看
/* 题意: 有很多垂直的线段,两个线段之间存在连线且和其他的线段没有交点为可见, 给出一组数据,求三条线段两两可见的组数。 题解: 将输入的的线段按x从小到大排列,然后先查询再更新, 就是每次插入线段之前,先查询即将插入的那条线段和以前插入的线段是否可见, 每条线段都有一种颜色,所以用一个vis[i][j]表示i与j可见 最后直接暴力枚举就可以过了 */ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAXN=16100; bool vis[MAXN>>1][MAXN>>1]; struct node{ int l,r; int color; }tree[MAXN<<2]; struct Node{ int x,y1,y2; }p[MAXN>>1]; bool cmp(Node a,Node b){ return a.x<b.x; } void build(int l,int r,int node){ tree[node].l=l; tree[node].r=r; tree[node].color=0; if(l==r) return; int mid=(l+r)>>1; build(l,mid,node<<1); build(mid+1,r,node<<1|1); } void pushdown(int node){ if(!tree[node].color) return; tree[node<<1].color=tree[node<<1|1].color=tree[node].color; tree[node].color=0;///取消标记 } void query(int l,int r,int node,int val){ if(tree[node].color!=0){ vis[tree[node].color][val]=1; return; } if(tree[node].l==tree[node].r) return; pushdown(node); int mid=(tree[node].l+tree[node].r)>>1; if(l<=mid) query(l,r,node<<1,val); if(r>mid) query(l,r,node<<1|1,val); } void update(int l,int r,int node,int val){ if(tree[node].l>=l&&tree[node].r<=r){ tree[node].color=val; return; } pushdown(node); int mid=(tree[node].l+tree[node].r)>>1; if(l<=mid) update(l,r,node<<1,val); if(r>mid) update(l,r,node<<1|1,val); } int main(){ int t,n,i,j,k; scanf("%d",&t); while(t--){ scanf("%d",&n); int maxn = 0; for(i=1;i<=n;i++){ scanf("%d%d%d",&p[i].y1,&p[i].y2,&p[i].x); p[i].y1=2*p[i].y1; p[i].y2=2*p[i].y2; maxn = max(maxn,p[i].y1); maxn = max(maxn,p[i].y2); } build(0,maxn,1); sort(p+1,p+n+1,cmp); memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++){ query(p[i].y1,p[i].y2,1,i); update(p[i].y1,p[i].y2,1,i); } int ans=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(vis[i][j]) for(k=1;k<=n;k++){ if(vis[i][k]&&vis[j][k]) ans++; } printf("%d\n",ans); } return 0; } |
相关文章推荐
- POJ 1436 Horizontally Visible Segments(线段树区间更新)
- (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。
- POJ 1436 Horizontally Visible Segments [线段树-区间更新]【数据结构】
- poj 1436 Horizontally Visible Segments(线段树基础,区间染色,拆点)
- poj 1436 Horizontally Visible Segments(线段树、区间覆盖)
- POJ 1436 Horizontally Visible Segments(线段树区间修改)
- POJ 1436 Horizontally Visible Segments (线段树+区间覆盖)
- POJ 1436 Horizontally Visible Segments(线段树区间染色查询)
- POJ 1436——Horizontally Visible Segments(线段树,区间染色+暴力+简单hash)
- POJ1436:Horizontally Visible Segments(区间更新)
- POJ 1436 Horizontally VisibleSegments(线段树:区间覆盖染色)
- POJ 1436 (线段树 区间染色) Horizontally Visible Segments
- poj 1436 线段树区间操作 Horizontally Visible Segments
- POJ 1436 Horizontally Visible Segments(区间更新 + 倍增区间)
- poj 1436 Horizontally Visible Segments(线段树 区间的覆盖关系)
- POJ 1436 Horizontally Visible Segments(线段树区间染色问题)
- POJ 1436 Horizontally Visible Segments (线段树·区间染色)
- poj 1436 Horizontally Visible Segments(线段树)(第二部分成段更新 不需要延迟标记 )
- POJ 1436 Horizontally Visible Segments(线段树)
- POJ 1436 Horizontally Visible Segments(线段树)