Zoj 1610 Count the Colors (segment tree + interval update + brute force count)

Source: Internet
Author: User

Main topic:

There are n operations, each time is a line of a period of dyeing (color is not the same), sometimes the back color may be covered in front of the color, ask the last color, can see the color there are several, each color has several parts?

Problem Solving Ideas:

This topic makes some difference, not by the point as the object, but with the interval as the object, it is clear that the segment of the tree line operation, update the time to take the interval as a unit, and the calculation of each interval appears several times when the tree can be traversed according to the characteristics of the tree.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 Const intMAXN =8005;7 structnode8 {9     intL, R, C;Ten     intMid () One     { A         return(R + L)/2; -     } - }; theNode tree[maxn*4]; - intCNT[MAXN], temp; -  - voidBuild (intRootintLintR) + { -TREE[ROOT].L =l; +TREE[ROOT].R =R; ATREE[ROOT].C =-1; at     if(L = = R-1) -         return ; -Build (2*root+1, L, Tree[root]. Mid ()); -Build (2*root+2, Tree[root]. Mid (), r); - } - voidUpdate (intRootintLintRintc) in{//interval Update, -     if(L==TREE[ROOT].L && tree[root].r==R) to     { +TREE[ROOT].C =C; -         return ; the     } *     if(Tree[root].c! =-1) $     {Panax Notoginsengtree[2*root+1].C = tree[2*root+2].C =tree[root].c; -TREE[ROOT].C =-1; the     } +     if(L >=Tree[root]. Mid ()) AUpdate (2*root+2, L, R, c); the     Else if(R <=Tree[root]. Mid ()) +Update (2*root+1, L, R, c); -     Else $     { $Update (2*root+1, L, Tree[root]. Mid (), c); -Update (2*root+2, Tree[root]. Mid (), R, c); -     } the } - voidCount (introot)Wuyi { the     if(Tree[root].c! =-1) -{//The current interval is exposed to the outer surface Wu         if(tree[root].c! = temp)//The current interval has not been counted. -CNT[TREE[ROOT].C] + +; Abouttemp =tree[root].c; $         return ; -     } -     if(TREE[ROOT].L = = tree[root].r-1) -{//returns an area that has not been overwritten, otherwise the stack overflows Atemp =-1; +         return ; the     } -Count (2*root+1); $Count (2*root+2); the } the  the intMain () the { -     intN, M; in      while(SCANF ("%d", &n)! =EOF) the     { theBuild (0,0, MAXN); About  the          while(N--) the         { the             intx, Y, C; +scanf (" %d%d%d", &x, &y, &c); -Update (0, X, Y, c); the         }Bayitemp =-1; thememset (CNT,0,sizeof(CNT)); theCount (0); -          for(intI=0; i<maxn; i++) -             if(Cnt[i]) theprintf ("%d%d\n", I, Cnt[i]); theprintf ("\ n"); the     } the     return 0; -}

Zoj 1610 Count the Colors (segment tree + interval update + brute force count)

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.