Count the Colors(线段树,找颜色段条数)
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
题解:各种错。。。最终发现还是要插a+1,b才可以,因为是颜色段,并不是点,还有,那个n是n条线段,所以查找和建树均要是8000内建,否则会wa;
思路:-1代笔多色,0代表无色;
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; const int INF=0x3f3f3f3f; #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) #define SD(x,y) scanf("%lf%lf",&x,&y) #define P_ printf(" ") #define ll root<<1 #define rr root<<1|1 #define lson ll,l,mid #define rson rr,mid+1,r #define V(x) tree[x] typedef long long LL; const int MAXN=8010; int color[MAXN]; int temp; int tree[MAXN<<2]; void pushdown(int root){ if(V(root)>0){ V(ll)=V(root); V(rr)=V(root); V(root)=-1; } } void build(int root,int l,int r){ int mid=(l+r)>>1; V(root)=0; if(l==r)return; build(lson);build(rson); } void update(int root,int l,int r,int A,int B,int v){ if(l>=A&&r<=B){ V(root)=v; return; } int mid=(l+r)>>1; pushdown(root); if(mid>=A)update(lson,A,B,v); if(mid<B)update(rson,A,B,v); V(root)=-1; } void query(int root,int l,int r){ int mid=(l+r)>>1; if(temp==V(root))return; if(!V(root)){ temp=0;return; } if(V(root)!=-1){ if(temp!=V(root)){ temp=V(root); color[temp]++; return; } return; } if(l==r)return; query(lson); query(rson); } int main(){ int n; while(~scanf("%d",&n)){ mem(color,0); build(1,1,8000); int a,b,c; int t=n; while(t--){ scanf("%d%d%d",&a,&b,&c); update(1,1,8000,a+1,b,c+1); } query(1,1,8000); for(int i=1;i<=8001;i++){ if(color[i])printf("%d %d\n",i-1,color[i]); }puts(""); } return 0; }