线段树专题—ZOJ1610 Count the Colors(涂区间,直接tag标记)

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.


<b< dd="">

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.


<b< dd="">

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

题意:就是在一段区间涂成一种颜色,然后最后问你,每种颜色涂成了多少种不同颜色。

题解:注意一下范围所有都是8000,所以和hdu1698差不了太多,也是区间标记,根

的优先值大于儿子的,这样就是这道题的解法,最后一次总的询问,将color 1-8000染好

颜色,然后就处理一下就好了。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<climits>
 6 using namespace std;
 7 const int MAXN=8007;
 8 bool a[MAXN]={0};
 9 int b[MAXN]={0},n,num=0,color[MAXN],tree[MAXN*4]={0},x,y,z;
10 void change(int l,int r,int p,int x,int y,int zhi)
11 {
12     if (l>r) return;
13     if (l==x&&r==y) tree[p]=zhi;
14     else
15     {
16         if (tree[p]!=-1)
17         {
18             tree[p*2]=tree[p*2+1]=tree[p];
19             tree[p]=-1;
20         }
21         int mid=(l+r)>>1;
22         if (y<=mid) change(l,mid,p*2,x,y,zhi);
23         else if (x>=mid+1) change(mid+1,r,p*2+1,x,y,zhi);
24              else
25              {
26                  change(l,mid,p*2,x,mid,zhi);
27                  change(mid+1,r,p*2+1,mid+1,y,zhi);
28              }
29     }
30 }
31 void query(int l,int r,int p){
32     if (l>r) return;
33     if (tree[p]!=-1)
34     {
35         for (int i=l;i<=r;i++)
36             color[i]=tree[p];
37     }
38     else
39     {
40         if (l==r) return;
41         int mid=(l+r)>>1;
42         query(l,mid,p*2);
43         query(mid+1,r,p*2+1);
44     }
45 }
46 int main()
47 {
48     while (~scanf("%d",&n))
49     {
50         num=0;
51         for (int i=0;i<MAXN*4;i++)
52             tree[i]=-1;
53         for (int i=0;i<MAXN;i++)
54             a[i]=b[i]=0,color[i]=MAXN-1;
55         for (int i=1;i<=n;i++)
56         {
57             scanf("%d%d%d",&x,&y,&z);
58             if (x!=y) change(1,8000,1,x+1,y,z);
59         }
60         query(1,8000,1);
61         for (int i=1;i<=8000;i++)
62             if (color[i]!=color[i-1]) a[color[i]]=1,b[color[i]]++;
63         for (int i=0;i<MAXN-2;i++)
64         {
65             if (a[i]) cout<<i<<" "<<b[i]<<endl;
66         }
67         cout<<endl;
68     }
69 }
原文地址:https://www.cnblogs.com/fengzhiyuan/p/7535543.html