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
Author: Standlove
Source: ZOJ Monthly, May 2003
题意:
给出l,r点的区间,给这些点的区间染上颜色c
问最后每种颜色有几块(也就是有几个不连续的区间块)
输出按颜色的标号从小到大输出
输出内容为
每行两个整数
第一个整数为颜色标号
第二个整数位该颜色区间块的个数
做法:
和
http://poj.org/problem?id=2528
一样的意思,只不过把区间修改变成点修改,然后去掉离散化,再加上一个统计连续块就好
代码
1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 #include<cstring> 5 #include<map> 6 #include<algorithm> 7 const int MAXN=32000; 8 typedef int LL; 9 int v[MAXN*3]; 10 int ans[MAXN*3]; 11 int zans[MAXN*3]; 12 inline int read() 13 { 14 int k=0; 15 char f=1; 16 char c=getchar(); 17 for(;!isdigit(c);c=getchar() ) 18 if(c==‘-‘) 19 f=-1; 20 for(;isdigit(c);c=getchar() ) 21 k=k*10+c-‘0‘; 22 return k*f; 23 } 24 template<class T> 25 class tree{ 26 public: 27 int ws; 28 T a[150000]; 29 T gg[150000]; 30 int l[150000],r[150000]; 31 void creat(int k,int ll,int rr){ 32 l[k]=ll;r[k]=rr; 33 if(ll>=rr){ 34 a[k]=-1; 35 gg[k]=-1; 36 return; 37 } 38 int mid=(ll+rr)/2; 39 creat(k*2,ll,mid); 40 creat(k*2+1,mid+1,rr); 41 a[k]=a[k*2]+a[k*2+1]; 42 gg[k]=-1; 43 } 44 void change(int k,int ll,int rr,int t){ 45 if(gg[k]!=-1){ 46 a[k]=gg[k]*(r[k]-l[k]+1); 47 if(l[k]!=r[k]){ 48 gg[k*2]=gg[k]; 49 gg[k*2+1]=gg[k]; 50 } 51 gg[k]=-1; 52 } 53 if(ll>r[k]||rr<l[k]) return; 54 if(ll<=l[k]&&rr>=r[k]){ 55 a[k]=(r[k]-l[k]+1)*t; 56 if(l[k]!=r[k]){ 57 gg[k*2]=t; 58 gg[k*2+1]=t; 59 } 60 return; 61 } 62 change(k*2,ll,rr,t); 63 change(k*2+1,ll,rr,t); 64 a[k]=a[k*2]+a[k*2+1]; 65 } 66 LL query(int k,int ll,int rr){ 67 if(gg[k]!=-1){ 68 a[k]=(r[k]-l[k]+1)*gg[k]; 69 if(l[k]!=r[k]){ 70 gg[k*2]=gg[k]; 71 gg[k*2+1]=gg[k]; 72 } 73 gg[k]=-1; 74 } 75 if(ll>r[k]||rr<l[k]) return 0; 76 if(l[k]==r[k]){ 77 return 0; 78 } 79 int ans1=query(k*2,ll,rr); 80 int ans2=query(k*2+1,ll,rr); 81 return 0; 82 } 83 void get_ans(int k){ 84 if(l[k]==r[k]){ 85 // cout<<"data: "<<l[k]<<" "<<a[k]<<endl; 86 zans[l[k]]=a[k]; 87 if(a[k]<0) 88 return; 89 if(v[a[k]]) 90 return; 91 else{ 92 v[a[k]]=1; 93 return; 94 } 95 } 96 get_ans(2*k); 97 get_ans(2*k+1); 98 } 99 }; 100 tree<int> line_tree; 101 int a[MAXN*3]; 102 int b[MAXN*3]; 103 int deal(){ 104 int n; 105 if(scanf("%d",&n)==EOF) 106 return 1; 107 memset(zans,0,sizeof(zans)); 108 int l,r; 109 int maxn,minn; 110 maxn=-1; 111 minn=0x7fffffff; 112 for(int i=0;i<n;i++){ 113 a[i*2]=read();//左区间 114 a[i*2+1]=read();//右区间 115 if(maxn<a[i*2]) 116 maxn=a[i*2]; 117 if(maxn<a[i*2+1]) 118 maxn=a[i*2+1]; 119 if(minn>a[i*2]) 120 minn=a[i*2]; 121 if(minn>a[i*2+1]) 122 minn=a[i*2+1]; 123 b[i]=read();//染色颜色 124 } 125 memset(v,0,sizeof(v)); 126 line_tree.creat(1,minn,maxn); 127 for(int i=0;i<n;i++){ 128 if(a[i*2]>a[i*2+1]) 129 swap(a[i*2],a[i*2+1]); 130 line_tree.change(1,a[i*2],a[i*2+1]-1,b[i]); 131 } 132 line_tree.query(1,minn,maxn); 133 line_tree.get_ans(1); 134 memset(ans,0,sizeof(ans)); 135 for(int i=minn;i<=maxn;i++){ 136 if((i==minn||zans[i]!=zans[i-1])&&v[zans[i]]) 137 ans[zans[i]]++; 138 } 139 for(int i=0;i<=8010;i++){ 140 if(v[i]==0) 141 continue; 142 else 143 printf("%d %d\n",i,ans[i]); 144 } 145 return 0; 146 } 147 int main(){ 148 while(1){ 149 if (deal()) 150 break; 151 putchar(‘\n‘); 152 } 153 return 0; 154 }