zoj 1610 Count the Colors(线段树 区间更新)

来源:互联网 发布:js object 遍历 值 编辑:程序博客网 时间:2024/03/28 17:34

Count the Colors

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.


Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

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

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

If some color can't be seen, you shouldn't print it.

Print a blank line after every dataset.


Sample Input

5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1


Sample Output

1 1
2 1
3 1

1 1

0 2
1 1



给一个区间内的线段染色 注意是区间 如把【1,2】区间内染色 只是算一个 总共染n次

问最后能看到颜色的段数 注意是段数!!!

所以要求出所有点中颜色的情况 最后统计颜色出现的段数

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 8010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;struct node{    int l,r;    int col;};node no[MAXN*4];int a[MAXN];int m;void build(int l,int r,int k){    no[k].l=l;    no[k].r=r;    no[k].col=-1;//-1代表没有颜色或者不是一种颜色    if(l==r)        return;    int mid=(l+r)/2;    build(l,mid,k*2);    build(mid+1,r,k*2+1);}void pushdown(int k){    if(no[k].col!=-1)    {        no[k*2].col=no[k*2+1].col=no[k].col;        no[k].col=-1;    }}void update(int l,int r,int k,int col){//    bug;    if(no[k].col==col)        return;    if(no[k].l>=l&&no[k].r<=r)    {        no[k].col=col;        return;    }    pushdown(k);    int mid=(no[k].l+no[k].r)/2;    if(r<=mid) update(l,r,k*2,col);    else if(l>mid) update(l,r,k*2+1,col);    else    {        update(l,mid,k*2,col);        update(mid+1,r,k*2+1,col);    }    if((no[k*2].col==no[k*2+1].col)&&(no[k*2].col!=-1))        no[k].col=no[k*2].col;}int cnt[MAXN];void query(int k){//    bug;    if(no[k].col!=-1)    {        for(int i=no[k].l;i<=no[k].r;i++)        {            a[i]=no[k].col;        }        return;    }    if(no[k].l==no[k].r)        return;    query(k*2);    query(k*2+1);}void getcnt(){//    bug;    MEM(cnt,0);    int pre=-1;    for(int i=0;i<MAXN;i++)    {        if(pre!=a[i])        {            pre=a[i];            cnt[pre]++;        }    }}int main(){//    fread;    int n;    while(scanf("%d",&n)!=EOF)    {//        cout<<"n  "<<n<<endl;        MEM(a,-1);        build(0,MAXN,1);        m=-1;        while(n--)        {            int l,r,num;            scanf("%d%d%d",&l,&r,&num);//            cout<<"l "<<l<<"  r  "<<r<<"  "<<num<<endl;            if(l==r)                continue;//            m=max(m,num);            r--;            update(l,r,1,num);//            bug;        }//        bug;        query(1);        getcnt();        for(int i=0;i<MAXN;i++)        {            if(cnt[i])            {                printf("%d %d\n",i,cnt[i]);            }        }        puts("");    }    return 0;}






0 0
原创粉丝点击