Zoj 1610 count the colors

Source: Internet
Author: User

Line Segment tree: latency tag + brute force update

I remember that when I first learned the line segment tree, I made this question wa a version ..... Now let's get it done in minutes ....


Count the colors Time Limit: 2 seconds memory limit: 65536 KB Painting some colored segments on a line, some previusly painted segments may be covered by some of 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 shoshould contain a color index that can be seen from the top, following the count of the segments of this color, they shocould 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


Author: standlove
Source: zoj monthly, May 2003


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=8200;int color[maxn],cov[maxn<<2],ans[maxn];void init(){    memset(color,-1,sizeof(color));    memset(ans,0,sizeof(ans));    memset(cov,-1,sizeof(cov));}void push_down(int l,int r,int rt){    if(cov[rt]!=-1)    {        cov[rt<<1]=cov[rt<<1|1]=cov[rt];        if(l==r)        {            color[l]=cov[rt];        }        cov[rt]=-1;    }}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&R>=r)    {        cov[rt]=c;        return ;    }    push_down(l,r,rt);    int m=(l+r)/2;    if(L<=m) update(L,R,c,lson);    if(R>m) update(L,R,c,rson);}void debug(int l,int r,int rt){    cout<<rt<<": "<<l<<" <---> "<<r<<" color: "<<color[rt]<<"  cov: "<<cov[rt]<<endl;    if(l==r) return ;    int m=(l+r)/2;    debug(lson); debug(rson);}void over_tree(int l,int r,int rt){    push_down(l,r,rt);    if(l==r) return ;    int m=(l+r)/2;    over_tree(lson); over_tree(rson);}int n;int main(){    while(scanf("%d",&n)!=EOF)    {        init();        while(n--)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            update(a,b-1,c,0,8100,1);        }        over_tree(0,8100,1);        int last=-1;        for(int i=0;i<8100;i++)        {            if(last==color[i]) continue;            else            {                last=color[i];                if(last!=-1)                {                    ans[last]++;                }            }        }        for(int i=0;i<=8000;i++)        {            if(ans[i])            {                printf("%d %d\n",i,ans[i]);            }        }        putchar(10);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.