ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
https://vjudge.net/contest/318019#problem/F
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
这题题意不太好懂
题目大意:
在范围[0,8000]内,给n个操作,把[x,y]段染成颜色c,注意是线段不是点,最后按颜色从小到大输出每种颜色连续的线段的个数,有颜色的才输出;
先给出我参考的博客
https://www.cnblogs.com/kuangbin/archive/2012/08/10/2631449.html
https://www.cnblogs.com/thunder-110/p/10302782.html
#include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <math.h> const int INF=0x3f3f3f3f; typedef long long LL; const int mod=1e9+7; //const double PI=acos(-1); const int maxn=1e5+10; using namespace std; //ios::sync_with_stdio(false); // cin.tie(NULL); int n; int last;//flag判断相邻两个区间的颜色是否相同 struct node { int l; int r; int color; }SegTree[8005<<2]; int ans[8005<<2]; void PushDown(int rt) { if(SegTree[rt].color>0) { SegTree[rt<<1].color=SegTree[rt].color; SegTree[rt<<1|1].color=SegTree[rt].color; SegTree[rt].color=-1; } } void Build(int l,int r,int rt) { SegTree[rt].l=l; SegTree[rt].r=r; SegTree[rt].color=0;//多样例时必须加 if(l==r) { return; } int mid=(l+r)>>1; Build(l,mid,rt<<1); Build(mid+1,r,rt<<1|1); } void Update(int L,int R,int C,int rt) { int l=SegTree[rt].l; int r=SegTree[rt].r; if(L<=l&&R>=r) { SegTree[rt].color=C; return ; } PushDown(rt);//向下更新lazy int mid=(l+r)>>1; if(L<=mid)//当寻找一个区间的时候,路径上的值全改成C Update(L,R,C,rt<<1); if(R>mid)//当寻找一个区间的时候,路径上的值全改成C Update(L,R,C,rt<<1|1); SegTree[rt].color=-1;//寻找到了之后,把回头的路径全部改成-1,说明如果顺着这些点下来,一定能找到区间 } void Query(int rt) { if(last==SegTree[rt].color) return; if(SegTree[rt].color==0)//一次也没有被涂过 { last=0; return; } if(SegTree[rt].color>0) { if(last!=SegTree[rt].color)//不是同一块海报 { last=SegTree[rt].color; ans[SegTree[rt].color]++; } return; } //接下来是如果SegTree[rt].color== -1 ,表示顺着这个点 一定能找到区间 Query(rt<<1); Query(rt<<1|1); } int main() { while(~scanf("%d",&n)) { memset(ans,0,sizeof(ans)); Build(0,8000,1); for(int i=1;i<=n;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); Update(a,b-1,c+1,1); } Query(1); for(int i=0;i<=8000;i++) { if(ans[i]) printf("%d %d\n",i-1,ans[i]); } printf("\n"); } return 0; }
暴力解法:
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<queue> 6 #include<math.h> 7 #include<stack> 8 #include<ctype.h> 9 #include<stdlib.h> 10 #include<map> 11 using namespace std; 12 typedef long long ll; 13 const int INF=0x3f3f3f3f; 14 const int M=8005; 15 int main() 16 { 17 int n,t[M]; 18 while(~scanf("%d",&n)) 19 { 20 memset(t,-1,sizeof(t)); 21 int j,i,a,b,c,max1=-1,max2=-1; 22 int ans[M]= {0}; 23 for(i=0; i<n; i++) 24 { 25 scanf("%d %d %d",&a,&b,&c); 26 max1=max(max1,b);//记录染有颜色的最右端 27 max2=max(max2,c);//记录染颜色的最大数值 28 29 for(j=a+1; j<=b; j++)//注意这里,题目中的说的是线段,所以我们每次不标记最左端 30 t[j]=c; 31 } 32 for(i=1; i<max1; i++) 33 { 34 if(t[i]!=t[i+1]&&t[i]!=-1) 35 ans[t[i]]++; 36 } 37 ans[t[max1]]++; 38 for(i=0; i<=max2; i++) 39 { 40 if(ans[i]>0) 41 printf("%d %d\n",i,ans[i]); 42 } 43 printf("\n"); 44 } 45 return 0; 46 }