U-Count the Colors (segment update + statistics)

Source: Internet
Author: User

U-Count the Colors (segment update + statistics)

 

 

There are n operations. Each operation is defined as x1, x2, and c, which indicates that the interval [x1, x2] is colored c and the interval can be repeatedly colored, the last question is how many intervals exist for each color.

 

Note that the interval is colored, not the dot.

Segment update is very easy. When calculating the intervals in which each color is located, map the node information in the line segment tree to the array and then make statistics,Without thinking of this, I have been thinking about how to make statistics on the online segment tree.

 

 

#include 
 
  #include 
  
   #include
   #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            
             #include #define LL long long#define _LL __int64#define eps 1e-12#define PI acos(-1.0)using namespace std;const int maxn = 8010;struct node{int l,r;int col;}tree[maxn*4];int ans[maxn];int arr[maxn];void build(int v, int l, int r){tree[v].l = l;tree[v].r = r;tree[v].col = -1;if(l == r)return;int mid = (l + r) >> 1;build(v*2,l,mid);build(v*2+1,mid+1,r);}void update(int v, int l, int r, int col){if(tree[v].l == l && tree[v].r == r){tree[v].col = col;return;}if(tree[v].col != -1){tree[v*2].col = tree[v*2+1].col = tree[v].col;tree[v].col = -1;}int mid = (tree[v].l + tree[v].r) >> 1;if(r <= mid)update(v*2,l,r,col);else if(l > mid)update(v*2+1,l,r,col);else{update(v*2,l,mid,col);update(v*2+1,mid+1,r,col);}}void query(int v, int l, int r){if(tree[v].col != -1){for(int i = tree[v].l; i <= tree[v].r; i++){arr[i] = tree[v].col;}return;}if(tree[v].l == tree[v].r){arr[tree[v].l] = tree[v].col;return;}int mid = (tree[v].l + tree[v].r) >> 1;if(r <= mid)query(v*2,l,r);else if(l > mid)query(v*2+1,l,r);else{query(v*2,l,mid);query(v*2+1,mid+1,r);}}int main(){int n,cn,t;int ll[maxn],rr[maxn],cc[maxn];while(~scanf(%d,&t)){memset(ans,0,sizeof(ans));n = -1;cn = -1;for(int i = 1; i <= t; i++){scanf(%d %d %d,&ll[i],&rr[i],&cc[i]);n = max(n,rr[i]);cn = max(cn,cc[i]);}build(1,0,n-1);for(int i = 1; i <= t; i++){update(1,ll[i],rr[i]-1,cc[i]);}query(1,0,n-1);ans[arr[0]] += 1;for(int i = 1; i <= n-1; i++){if(arr[i] != arr[i-1])ans[arr[i]] += 1;}for(int i = 0; i <= cn; i++){if(ans[i])printf(%d %d,i,ans[i]);}printf();}return 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.