Description
Your task is counting the segments of different colors you can see at last.
Input
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
x1 x2 c
x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.
All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input
Sample Output
1 1
0 2
1 1
题意:读入 a,b,c 表示用c颜色去涂 a,b区间.问最后表面色各个颜色各有几段.
注意:这里因为是区间不是 a~b的点.所以更新的时候更新 [a+1,b]即可
代码:
#include <cstdio> #include <cstring> #include <algorithm> #define MAX 8010 using namespace std; struct node { int l,r,mid,data; }; node tree[MAX<<2]; int book[MAX]; int ans[MAX]; void CreatTree(int rt,int l,int r) { tree[rt].l=l,tree[rt].r=r; tree[rt].mid=(l+r)>>1; tree[rt].data=-1; if(l==r){ return; } CreatTree(rt<<1,l,tree[rt].mid); CreatTree(rt<<1|1,tree[rt].mid+1,r); } void change(int rt,int l,int r,int v){ if(tree[rt].data==v)return; if(tree[rt].l==l && tree[rt].r==r){ tree[rt].data=v; //printf("%d %d %d\n",l,r,v); return; } if(tree[rt].data!=-1){ tree[rt<<1].data=tree[rt<<1|1].data=tree[rt].data; tree[rt].data=-1; } if(l>tree[rt].mid)change(rt<<1|1,l,r,v); else if(r<=tree[rt].mid)change(rt<<1,l,r,v); else{ change(rt<<1,l,tree[rt].mid,v); change(rt<<1|1,tree[rt].mid+1,r,v); } } void Solve(int rt) { if(tree[rt].l==tree[rt].r){ ans[tree[rt].l]=tree[rt].data; return; } if(tree[rt].data!=-1){ tree[rt<<1].data=tree[rt<<1|1].data=tree[rt].data; tree[rt].data=-1; } Solve(rt<<1); Solve(rt<<1|1); } int main() { int t,i,n,m,left,right,v,cas=0; while(scanf("%d",&n)!=EOF){ CreatTree(1,1,8000); for(i=1;i<=n;i++){ scanf("%d%d%d",&left,&right,&v); change(1,left+1,right,v); } memset(book,0,sizeof(book)); Solve(1); for(i=1;i<=8000;i++){ if(i==1 || ans[i]!=ans[i-1])book[ans[i]]++; } for(i=0;i<=8000;i++){ if(book[i])printf("%d %d\n",i,book[i]); } printf("\n"); } return 0; }