ZOJ 1610--count the Colors —————— "segment tree interval replacement, to find the number of different color interval segments"

Source: Internet
Author: User

Count the ColorsTime limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld &%llu< /c7> SubmitStatusPracticeZOJ 1610

Description

Painting some colored segments on a line, some previously painted segments could 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 all data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored Segme Nts.

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 is in the range [0, 8000], and they is all integers.

Input may contain several data set, and process to the end of file.

Output



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

If some color can ' t is 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

Error point: At the beginning of thinking that N is the largest range of trees, and then always reported a section error (RE), and later found that MAXN is, after the settlement and WA a few times. There is also (2,3,0) and (4,5,0) is actually two paragraphs, need to pay attention to. Then refer to the online kind of writing on the past, as to why I did not, but other why, I am also very puzzled. Problem-solving ideas: interval substitution, point to line, statistics segment number.
/*AC code */#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define MID (L +R)/2#define Lson rt*2,l,mid#define rson rt*2+1,mid+1,rconst int maxn=50000;int col[maxn*4];int Hash[maxn*2];int tmp[        Maxn*2];int k;void pushdown (int rt) {if (col[rt]>=0) {COL[RT*2]=COL[RT];        COL[RT*2+1]=COL[RT];    Col[rt]=-1; }}void Update (int rt,int l,int r,int l_ran,int r_ran,int _col) {if (L_ran<=l&&r<=r_ran) {Col[rt]=_c               Ol    return;    } pushdown (RT);    if (l_ran<=mid) {update (LSON,L_RAN,R_RAN,_COL);    } if (R_ran>mid) {update (RSON,L_RAN,R_RAN,_COL); }}void query (int rt,int l,int R) {if (col[rt]>=0) {for (int i=l;i<=r;i++) {//difference here Tmp[i]=co                 L[RT];        }//difference here col[rt]=-1;    return;    } if (l==r) return;    Query (Lson); Query (Rson);} void Get_ans (int n) {if (tmp[0]>=0) Hash[tmP[0]]=1;        for (int i=1;i<n;i++) {if (tmp[i]!=tmp[i-1]) {hash[tmp[i]]++; }}}void Debug2 () {for (int i=0;i<k;i++) {printf (".    %d%d\n ", i,tmp[i]);    }}int Main () {int n;        while (scanf ("%d", &n)!=eof) {int ta,tb,tc;        memset (tmp,-1,sizeof (TMP));        memset (col,-1,sizeof (col));            for (int i=0;i<n;i++) {scanf ("%d%d%d", &AMP;TA,&AMP;TB,&AMP;TC);        Update (1,0,MAXN,TA*2,TB*2,TC);        } memset (Hash,0,sizeof (Hash));        k=0;     Query (1,0,MAXN);     printf ("%d\n", K);       Debug2 ();        Get_ans (k);                for (int i=0;i<maxn;i++) {//difference here if (tmp[i]!=-1) {int J;                for (j=i;j<maxn&&tmp[i]==tmp[j];j++);                hash[tmp[i]]++;            I=j-1;                }}//difference here for (int i=0;i<maxn+20;i++) {if (Hash[i]) {            printf ("%d%d\n", i,hash[i]);}} printf ("\ n"); } return 0;} /*wa code */#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define MID (L +R)/2#define Lson rt*2,l,mid#define rson rt*2+1,mid+1,rconst int maxn=50000;int col[maxn*4];int Hash[maxn*2];int tmp[        Maxn*2];int k=0;void pushdown (int rt) {if (col[rt]>=0) {COL[RT*2]=COL[RT];        COL[RT*2+1]=COL[RT];    Col[rt]=-1; }}void Update (int rt,int l,int r,int l_ran,int r_ran,int _col) {if (L_ran<=l&&r<=r_ran) {Col[rt]=_c        Ol    return;    } pushdown (RT);    if (l_ran<=mid) {update (LSON,L_RAN,R_RAN,_COL);    } if (R_ran>mid) {update (RSON,L_RAN,R_RAN,_COL);       }}void query (int rt,int l,int R) {if (col[rt]>=0) {TMP[K++]=COL[RT];        The difference is here col[rt]=-1;    return;    } if (l==r) return;    Query (Lson); Query (Rson);}    void Get_ans (int n) {if (tmp[0]>=0) hash[tmp[0]]=1; for (int i=1;i<n;i++) {if (tMp[i]!=tmp[i-1]) {hash[tmp[i]]++; }}}void Debug2 () {for (int i=0;i<k;i++) {printf (".    %d%d\n ", i,tmp[i]);    }}int Main () {int n;        while (scanf ("%d", &n)!=eof) {int ta,tb,tc;        memset (col,-1,sizeof (col));            for (int i=0;i<n;i++) {scanf ("%d%d%d", &AMP;TA,&AMP;TB,&AMP;TC);        Update (1,0,MAXN,TA*2,TB*2,TC);        } memset (Hash,0,sizeof (Hash));        k=0;     Query (1,0,MAXN);     printf ("%d\n", K);        Debug2 ();                     Get_ans (k);            The difference is here for (int i=0;i<maxn+20;i++) {if (Hash[i]) {printf ("%d%d\n", i,hash[i]);    }} printf ("\ n"); } return 0;}

  

ZOJ 1610--count the Colors —————— "segment tree interval replacement, to find the number of different color interval segments"

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.