Kuangbin topic seven zoj1610 count the colors (flexible line segment tree)

Source: Internet
Author: User

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.


<B <dd = "">

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.


<B <dd = "">

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

 


<B <dd = "">

Sample output

1 1
2 1
3 1

 

1 1

0 2
1 1

 

Because it is a line segment tree topic, I think about the line segment tree. Because I just used the line segment tree, it seems useless to build the tree here, but I'm not sure, but pushup is definitely useless. In addition, when querying, you need to run all the leaf nodes, so you do not need to make any judgments. The general idea is correct, that is, you did not expect to use the last record.

 

1 # include <iostream> 2 # include <stdio. h> 3 # include <math. h> 4 # include <string. h> 5 # include <stdlib. h> 6 # include <string> 7 # include <vector> 8 # include <set> 9 # include <map> 10 # include <queue> 11 # include <algorithm> 12 # include <sstream> 13 # include <stack> 14 using namespace STD; 15 # define fo freopen ("in.txt", "r", stdin); 16 # define rep (I, A, n) for (INT I = A; I <N; I ++) 17 # define per (I, A, n) for (int I = n-1; I> = A; I --) 18 # define Pb push_back19 # define MP make_pair20 # define all (x ). begin (), (x ). end () 21 # define fi first22 # define se second23 # define SZ (x) (INT) (x ). size () 24 # define debug (x) cout <"&" <x <"&" <Endl; 25 # define lowbit (X) (X &-x) 26 # define MEM (a, B) memset (a, B, sizeof (a); 27 typedef vector <int> VI; 28 typedef long ll; 29 typedef pair <int, int> PII; 30 const ll mod = 1000000 007; 31 const int INF = 0x3f3f3f; 32 ll powmod (ll a, LL B) {ll res = 1; A % = MOD; For (; B; B >>= 1) {If (B & 1) RES = res * A % MOD; A = A * A % MOD;} return res ;} 33 ll gcd (ll a, LL B) {return B? Gcd (B, A % B): A;} 34 // head35 36 const int maxn = 8010; // color of the last segment 37 int lazy [maxn <2], n, ANS [maxn], last; // lazy is the tree and does not need to process any information 38 39 void Pushdown (int rt) {40 if (lazy [RT]! =-1) {41 lazy [RT <1] = lazy [RT <1 | 1] = lazy [RT]; 42 lazy [RT] =-1; 43} 44} 45 46 void updata (int rt, int L, int R, int L, int R, int Val) {47 If (L> = L & R <= r) {48 lazy [RT] = val; 49 return; 50} 51 Pushdown (RT ); 52 int mid = (L + r)> 1; 53 If (L <= mid) updata (RT <1, L, mid, L, R, Val ); 54 if (r> mid) updata (RT <1 | 1, Mid + 1, R, L, R, Val); 55} 56 57 void query (int rt, int L, int R, int L, int R) {// do not be too rigid. Learn to change 58 If (L = r) {59 If (lazy [RT]! =-1 & lazy [RT]! = Last) {// clever 60 ans [lazy [RT] ++; 61} 62 last = lazy [RT]; 63 return; 64} 65 Pushdown (RT ); 66 int mid = (L + r)> 1; // query (RT <1, L, mid, L, R) of all leaf nodes needs to be traversed ); 68 query (RT <1 | 1, Mid + 1, R, L, R); 69} 70 71 int main () {72 while (~ Scanf ("% d", & N) {73 MEM (ANS, 0); 74 MEM (lazy,-1); 75 int X, Y, Val; 76 while (n --) {77 scanf ("% d", & X, & Y, & Val); 78 updata (1,1, maxn, x + 1, y, Val); // change 0 to 179} 80 last =-1; 81 query (, maxn, 1, maxn); 82 rep (I, 0, maxn) {83 If (ANS [I]) printf ("% d \ n", I, ANS [I]); 84} 85 printf ("\ n "); 86} 87}

 

Kuangbin topic seven zoj1610 count the colors (flexible line segment tree)

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.