[Partition] Count the colors

Source: Internet
Author: User
Painting some colored segments on a line, some previously paintedsegments may be covered by some the subsequent ones.Your task is counting the segments of different colors you cansee at last.InputThe 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 nonnegativeintegers separated by single spaces:x1 x2 cx1 and x2 indicate the left endpoint and right endpoint of thesegment, c indicates the color of the segment.All the numbers are in the range [0, 8000], and they are allintegers.Input may contain several data set, process to the end of file.OutputEach line of the output should contain a color index that canbe seen from the top, following the count of the segments ofthis color, they should 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 Input50 4 40 3 13 4 20 2 20 2 340 1 13 4 11 3 21 3 160 1 01 2 12 3 11 2 02 3 01 2 1Sample Output1 12 13 11 10 21 1

An entry into the segment.

First parse the colors of all vertices (the method can be seen in the Mayer's posters), and then proceed to the planning.
Program injection errors.

Accode:

# Include <cstdio> # include <cstring> # include <cstdlib> # include <bitset> const char fi [] = "zoj1610.in"; const char fo [] = "zoj1610.out "; const int maxn = 8010; const int max = 0x3fffff00; const int min =-Max; struct segtree {int L, R, LC, RC, Col ;}; segtree tree [maxn <2]; int Col [maxn]; int CNT [maxn]; int L [maxn], R [maxn]; int n, m, TOT, n; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W", stdou T);} void build (int l, int R) {int now = ++ tot; tree [now]. L = L; tree [now]. R = r; tree [now]. col =-1; int mid = (L + r)> 1; if (L + 1 <r) {tree [now]. lc = tot + 1; build (L, mid); tree [now]. rc = tot + 1; build (MID, R) ;}} void insert (INT now, int I) {If (tree [now]. col = Col [I]) return; If (L [I] <= tree [now]. L & R [I]> = tree [now]. r) {tree [now]. col = Col [I]; return;} If (tree [now]. col>-2) {Tree [tree [now]. LC]. col = tree [tree [now]. RC]. col = tree [now]. col; tree [now]. col =-2;} int mid = (tree [now]. L + tree [now]. r)> 1; if (L [I] <mid) insert (tree [now]. LC, I); If (mid <R [I]) insert (tree [now]. RC, I); If (tree [tree [now]. LC]. col>-2 & tree [tree [now]. RC]. col>-2 & tree [tree [now]. LC]. col = tree [tree [now]. RC]. COL) tree [now]. col = tree [tree [now]. LC]. col; // note that the last dyeing may dye the entire segment into the same color. // Click the marker.} Void count (INT now) {If (tree [now]. col =-1) return; If (tree [now]. col>-1) {Col [tree [now]. L + 1] = tree [now]. col; If (COL [tree [now]. R + 1] <-1) COL [tree [now]. R + 1] =-1;} // uses an approximate decentralized operation to mark the start and end of each segment, // LR is added to prevent 0 downgrading and facilitate planning. If (tree [now]. col <-1) {count (tree [now]. lc); count (tree [now]. RC);} // a multi-color binary system.} Void work () {While (scanf ("% d", & N )! = EOF) {tot = 0; build (0, maxn-1); n = 0; For (INT I = 1; I <n + 1; ++ I) {scanf ("% d", L + I, R + I, Col + I); n = STD: max (COL [I], n ); insert (1, I);} memset (COL, 0xfe, sizeof (COL); // first, convert the colors of all points to an impossible value. Memset (CNT, 0, sizeof (CNT); count (1); int last =-1; for (INT I = 1; I <maxn; ++ I) if (COL [I]>-2) {If (COL [I]>-1 & Col [I]! = Last) ++ CNT [col [I]; // It is colored and consistent with the previous section. Last = Col [I];} For (INT I = 0; I <n + 1; ++ I) if (CNT [I]) printf ("% d \ n", I, CNT [I]); printf ("\ n") ;}} int main () {init_file (); work (); exit (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.