线段树(区间更新以及统计片段颜色)

来源:互联网 发布:女主是编程天才的小说 编辑:程序博客网 时间:2024/06/08 04:22
 ZOJ - 1610
题目:
  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


代码:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int N=8010;#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)struct node{    int l,r;    int flag;   //flag µÈÓÚ -1 ±íʾʲôɫ¶¼Ã»ÓÐ}tree[N*3];int color[N];    //¼Ç¼ÿ¿é°åÉϵÄÊÇʲôÑÕÉ«void build(int l,int r,int rt){    tree[rt].l=l;    tree[rt].r=r;    tree[rt].flag=-1;    if(l==r)        return ;    int mid=(l+r)>>1;    build(l,mid,L(rt));    build(mid+1,r,R(rt));}void PushDown(int rt){    tree[L(rt)].flag=tree[rt].flag;    tree[R(rt)].flag=tree[rt].flag;    tree[rt].flag=-1;}void update(int val,int l,int r,int rt){    if(tree[rt].flag==val)        return ;    if(l<=tree[rt].l && tree[rt].r<=r){        tree[rt].flag=val;        return ;    }    if(tree[rt].flag!=-1)   //ÑÓ³Ù¸²¸Ç ²»È»¿ÉÄÜ»áTLE£¨×î½ü×öÒ»Ö±¶¼ÊÇÑÓ³Ù¸²¸Ç£©        PushDown(rt);    int mid=(tree[rt].l+tree[rt].r)>>1;    if(r<=mid)        update(val,l,r,L(rt));    else if(l>=mid+1)        update(val,l,r,R(rt));    else{        update(val,l,r,L(rt));        update(val,l,r,R(rt));    }    if((tree[L(rt)].flag==tree[R(rt)].flag) && (tree[L(rt)].flag!=-1))  //µÝ¹é»ØÀ´Ð޸ĸ¸½áµãÑÕÉ«        tree[rt].flag=tree[L(rt)].flag;}void query(int rt){    if(tree[rt].flag!=-1){        for(int i=tree[rt].l;i<=tree[rt].r;i++)            color[i]=tree[rt].flag;        return ;    }    if(tree[rt].l==tree[rt].r)  //ÕâÊDz»¼ÓÕâ¸ö¾¹È»»áÔËÐв»ÆðÀ´        return ;    query(L(rt));    query(R(rt));}void Solve(){    int res[N];    memset(res,0,sizeof(res));    int pre=-1;    for(int i=0;i<N;i++)        if(pre!=color[i]){  //ÑÕÉ«¿é²»ÏàÁ¬¾Í++            pre=color[i];            res[pre]++;        }    for(int i=0;i<N;i++)        if(res[i]!=0)            printf("%d %d\n",i,res[i]);    printf("\n");}int main(){    int n;    while(~scanf("%d",&n)){        memset(color,-1,sizeof(color));        build(0,N,1);        int x,y,val;        while(n--){            scanf("%d%d%d",&x,&y,&val);            if(x==y)            continue;            update(val,x,y-1,1);    //ºóÃæÒ»¸ö²»Í¿¾ÍÄܱÜÃâÄÇÖÖÇé¿ö        }           query(1);        Solve();    }    return 0;}

原创粉丝点击