区间覆盖 B - Count the Colors

和上一题的区别在于这个题是对区间染色,上一题是点染色,区间比点少1

对于左右边界a、b,右边界b不变,左边界变为a+1即可

统计区间个数,所以要把每种染色的左右边界记录下来,看是否相连,不相连染色区间数+1

 1 #include <bits/stdc++.h>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <vector>
 5 #define lson l,m,rt<<1
 6 #define rson m+1,r,rt<<1|1
 7 #define mem(x) memset(x,0,sizeof(x))
 8 using namespace std;
 9 const int maxn=8000+5;
10 int col[maxn<<4];
11 
12 vector < pair<int ,int > > cnt[maxn];
13 int n;
14 
15 void pushdown(int rt){
16     if(col[rt]!=-1){ 
17         col[rt<<1]=col[rt<<1|1]=col[rt];
18         col[rt]=-1;
19     } 
20     return;
21 }
22 void update(int L,int R,int c,int l,int r,int rt){
23     if(L<=l&&R>=r){
24         //cout <<"tiao"<<rt<<" "<<l<<" "<<r<<endl;
25         col[rt]=c;
26         return;
27     }
28     pushdown(rt);
29     int m=(l+r)>>1;
30     if(L<=m) update(L,R,c,lson);
31     if(R>m) update(L,R,c,rson);
32 }
33 
34 
35 void query(int l,int r,int rt){
36     if(col[rt]!=-1){
37         //cout <<"shi"<<rt<<" "<<col[rt]<<endl;
38         cnt[col[rt]].push_back(make_pair<int,int>(l,r));
39         return;
40     }
41     if(l==r) return;
42     int m=(l+r)>>1;
43     query(lson);
44     query(rson);
45 }
46 
47 
48 int main(){
49     while(cin>>n){
50         mem(cnt);
51         memset(col,-1,sizeof(col));
52         for(int i=1;i<=n;i++){
53             int a,b,c;scanf("%d%d%d",&a,&b,&c);
54         //    cout <<a<<" "<<b<<" "<<c<<endl;
55             update(a+1,b,c,0,8000,1);
56         }
57         query(0,8000,1);
58         //cout <<cnt[0][0].second<<" "<<cnt[0][1].first<<endl;
59         for(int i=0;i<=8000;i++){
60             int ans=0;
61             if(cnt[i].size()) ans++;
62             for(int j=1;j<cnt[i].size();j++){
63                 
64                 if(cnt[i][j].first==cnt[i][j-1].second+1) continue;
65                 ans++;
66             }
67             cnt[i].clear();
68             if(ans) printf("%d %d\n",i,ans);
69         } 
70         cout <<endl;
71     }
72     return 0;
73 }

 

posted @ 2019-07-17 13:03  Chuhanjing  阅读(107)  评论(0编辑  收藏  举报