标签:poj
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 4645 | Accepted: 1706 |
Description
Input
Output
Sample Input
1 5 0 4 4 0 3 1 3 4 2 0 2 2 0 2 3
Sample Output
1
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 16005
using namespace std;
struct tree{
int l,r,color;
}tree[M<<2];
struct node{
int x,y1,y2;
}segment[8005];
bool a[8005][8005]; //bool型省下了大量空间,用int会MLE.
bool cmp(node a,node b){
return a.x<b.x;
}
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].color=-1;
if(l==r)return;
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
}
void pushdown(int root)
{
if(tree[root].l==tree[root].r)return;
if(tree[root].color!=-1)
{
tree[root<<1].color=tree[root<<1|1].color=tree[root].color;
tree[root].color=-1;
}
return;
}
void update(int l,int r,int z,int root)
{
if(tree[root].l==l&&tree[root].r==r){
tree[root].color=z;
return ;
}
pushdown(root);
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid)update(l,r,z,root<<1);
else if(l>mid)update(l,r,z,root<<1|1);
else {
update(l,mid,z,root<<1);
update(mid+1,r,z,root<<1|1);
}
}
void query(int l,int r,int c,int root)
{
if(tree[root].color!=-1)
{
a[tree[root].color][c]=true;
return;
}
if(tree[root].l==tree[root].r)return;
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid) query(l,r,c,root<<1);
else if(l>mid) query(l,r,c,root<<1|1);
else {
query(l,mid,c,root<<1);
query(mid+1,r,c,root<<1|1);
}
}
int main()
{
int d,i,j,k,n;
scanf("%d",&d);
while(d--)
{
scanf("%d",&n);
memset(a,false,sizeof(a));
build(0,16000,1);
for(i=0;i<n;i++)
{
scanf("%d%d%d",&segment[i].y1,&segment[i].y2,&segment[i].x);
}
sort(segment,segment+n,cmp);
for(i=0;i<n;i++)
{
query(2*segment[i].y1,2*segment[i].y2,i,1);
update(2*segment[i].y1,2*segment[i].y2,i,1);
}
int ans=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i][j]){
for(k=j+1;k<n;k++)
if(a[j][k]&&a[i][k])ans++;
}
}
printf("%d\n",ans);
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
poj 1436 Horizontally Visible Segments(线段树、区间覆盖)
标签:poj
原文地址:http://blog.csdn.net/aaaaacmer/article/details/47147087