ZOJ 1610 Count the Colors (segment tree interval update)

Source: Internet
Author: User

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: n operations, each time a period of update, and finally count the number of segments of each color




#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8//typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I <b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) MEMS ET ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf (          "%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define Bug PF ("hi\n") using namespace std; #define INF 0x3f3f3f3f#define n 8005struct stud{int le,ri;int va;} F[n*4];int n,k;int color[n];struct studd{int le,ri;int va;}     ans[n];inline void pushdown (int pos) {f[l (POS)].va=f[r (POS)].va=f[pos].va; F[pos].va=-1;} void build (int pos,int le,int ri) {f[pos].le=le;f[pos].ri=ri;f[pos].va=-1;if (Le==ri) return, int mid=mid (LE,RI), Build (L (POS), Le,mid), Build (R (POS), Mid+1,ri);}     void update (int pos,int le,int Ri,int va) {if (F[pos].le==le&&f[pos].ri==ri) {f[pos].va=va;   return;   } if (F[pos].va!=-1) pushdown (POS);   int Mid=mid (F[POS].LE,F[POS].RI);   if (mid>=ri) Update (L (POS), Le,ri,va);   ElseIf (mid<le) Update (R (POS), Le,ri,va);    else {update (L (POS), Le,mid,va);   Update (R (POS), Mid+1,ri,va); }if (F[l (POS)].va==f[r (POS)].va&&f[l (POS)].va!=-1) F[pos].va=f[pos].va;}    void query (int pos) {if (f[pos].va!=-1) {Ans[k].le=f[pos].le;ans[k].ri=f[pos].ri;ans[k++].va=f[pos].va;return;}    if (F[pos].le==f[pos].ri) return;    Query (L (POS)); Query (R (POS));}       int main () {int i,j;while (~SF (n)) {build (1,0,n);       int Le,ri,va;         Fre (i,0,n) {sfff (LE,RI,VA);       Update (1,LE,RI-1,VA);    }k=0;query (1);    MEM (color,0);    color[ans[0].va]++; Fre (I,1,k) if (ans[i].va!=ans[i-1].va| | Ans[i-1].ri+1<ans[i].le) Color[ans[i].va]++;fre (i,0,n) if (Color[i]) pf ("%d%d\n", I,color[i]);p f ("\ n"); return 0;}


ZOJ 1610 Count the Colors (segment tree interval update)

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.