F-count the Colors ZOJ 1610 (segment tree + node is a length of one interval + tree traversal)

Source: Internet
Author: User


F-count the Colors
Time limit:2000ms Memory limit:65536kb 64bit IO format:%lld &%llu
Submit Status Practice ZOJ 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

Test instructions is the interval coloring, the last question you can see is the color, and their number of segments,

Really is a very good question, here the smallest is the length of a range, not a point, and again use the delay mark,

The number of segments is the traversal of the tree, and the TMP records the current node's pioneer nodes color, used to determine the color of the number of segments,


#include <bits/stdc++.h> #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <cstdio> #include <map> #include <algorithm>using namespace Std;const int maxn=8011;const int inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M,r#define M ((L+R) >    &GT;1) #define for (i,n) for (int i=0;i< (n); i++) Template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32);    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return x;}    Template<class t> inline void Read_ (t&x,t&y) {read (x); Read (y);}    Template<class t> inline void read__ (t&x,t&y,t&z) {read (x);    Read (y); Read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');} Template<class t>inline void Writeln (T x) {WRITE (x); Putchar (' \ n ');} -------IO template------typedef long LONG ll;struct node{int color;//-1 indicates that the interval is not painted, 2 indicates that the interval is painted in mixed color} p[maxn<<3];vo    ID build (int rt,int l,int R) {p[rt].color=-1;    if (r-l==1) return;    Build (Lson); Build (Rson);} int cnt[maxn];void update (int rt,int l,int r,int x,int y,int color) {if (l==r| |    P[rt].color==color) return;        if (l==x&&y==r) {p[rt].color=color;    return;        } if (p[rt].color>=0) {p[rt<<1].color=p[rt<<1|1].color=p[rt].color;    P[rt].color=-2;    } if (l<=x&&y<=m) update (LSON,X,Y,COLOR);    else if (x>=m&&y<=r) update (RSON,X,Y,COLOR);        else {update (lson,x,m,color);    Update (Rson,m,y,color); } p[rt].color=-2;}        void Solve (int rt,int l,int r,int& tmp) {if (p[rt].color==-1) {tmp=-1;    return;       } if (p[rt].color>=0) {if (tmp!=p[rt].color) {tmp=p[rt].color;     cnt[p[rt].color]++;    } return;        } if (l+1!=r) {solve (lson,tmp);    Solve (rson,tmp);    }}int Main () {int n;    Freopen ("In.txt", "R", stdin);        while (~SCANF ("%d", &n)) {build (1,0,8000);        int m1=0;            for (i,n) {int left,right,color;            read__ (Left,right,color);            Update (1,0,8000,left,right,color);        M1=max (M1,color);        } memset (Cnt,0,sizeof (CNT));        int tmp=-1;        Solve (1,0,8000,tmp);        for (int i=0; i<=m1; i++) if (Cnt[i]) printf ("%d%d\n", i,cnt[i]);    printf ("\ n"); } return 0;}



F-count the Colors ZOJ 1610 (segment tree + node is a length of one interval + tree traversal)

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.