ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)

Count the Colors

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Painting some colored segments on a line, some previously painted segments may be covered by some 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.


Output

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this 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 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


Sample Output

1 1
2 1
3 1

1 1

0 2
1 1

题意有点坑,是n个染色的片段,不是染点,比如 0,3,是染的1,2,3片段。

然后就是建树建8000,因为是n个染色的片段,最后查询,直接单点查询。

维护的是片段的标号,不是点号,写的时候,要l+1,我写的l++,智障了。。。

代码:

  1 //线段树(区间染色+统计间断区间数量)
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<bitset>
  7 #include<cassert>
  8 #include<cctype>
  9 #include<cmath>
 10 #include<cstdlib>
 11 #include<ctime>
 12 #include<deque>
 13 #include<iomanip>
 14 #include<list>
 15 #include<map>
 16 #include<queue>
 17 #include<set>
 18 #include<stack>
 19 #include<vector>
 20 using namespace std;
 21 typedef long long ll;
 22 typedef long double ld;
 23 typedef pair<int,int> pii;
 24 
 25 const double PI=acos(-1.0);
 26 const double eps=1e-6;
 27 const ll mod=1e9+7;
 28 const int inf=0x3f3f3f3f;
 29 const int maxn=1e5+10;
 30 const int maxm=100+10;
 31 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 32 #define lson l,m,rt<<1
 33 #define rson m+1,r,rt<<1|1
 34 
 35 int now;
 36 int col[maxn<<2],num[maxn];
 37 
 38 void pushdown(int rt)
 39 {
 40     if(col[rt]!=-1){
 41         col[rt<<1]=col[rt<<1|1]=col[rt];
 42         col[rt]=-1;
 43     }
 44 }
 45 
 46 void build(int l,int r,int rt)
 47 {
 48     col[rt]=-1;
 49     if(l==r){
 50         return ;
 51     }
 52 
 53     int m=(l+r)>>1;
 54     build(lson);
 55     build(rson);
 56 }
 57 
 58 void update(int L,int R,int c,int l,int r,int rt)
 59 {
 60     if(r<L||l>R) return ;
 61     if(L<=l&&r<=R){
 62         col[rt]=c;
 63         return ;
 64     }
 65 
 66     pushdown(rt);
 67     int m=(l+r)>>1;
 68     if(L<=m) update(L,R,c,lson);
 69     if(R> m) update(L,R,c,rson);
 70 }
 71 
 72 void query(int l,int r,int rt)
 73 {
 74     if(l==r){
 75         if(col[rt]!=-1&&col[rt]!=now) num[col[rt]]++;
 76         now=col[rt];
 77         return ;
 78     }
 79 
 80     pushdown(rt);
 81     int m=(l+r)>>1;
 82     query(lson);
 83     query(rson);
 84 }
 85 
 86 int main()
 87 {
 88     int n;
 89     while(~scanf("%d",&n)){
 90         memset(num,0,sizeof(num));
 91         build(1,8000,1);
 92         for(int i=1;i<=n;i++){
 93             int l,r,k;
 94             scanf("%d%d%d",&l,&r,&k);
 95             if(l<r){
 96                 l++;
 97                 update(l,r,k,1,8000,1);
 98             }
 99         }
100         now=-1;
101         query(1,8000,1);
102         for(int i=0;i<=8000;i++){
103             if(num[i]) cout<<i<<" "<<num[i]<<endl;
104         }
105         cout<<endl;
106     }
107 }
原文地址:https://www.cnblogs.com/ZERO-/p/10639005.html