Color the ball
Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
当N = 0,输入结束。
Output
Sample Input
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
Sample Output
1 1 1 3 2 1
Author
Source
#include<iostream>
using namespace std;
const int maxn=300000;
struct segtree{
int left,right,key;
}t[maxn];
void build(int l,int r,int dex){
t[dex].left=l,t[dex].right=r,t[dex].key=0;
if(l==r)
return ;
int mid=(l+r)/2;
build(l,mid,dex*2);
build(mid+1,r,dex*2+1);
}
void updata(int l,int r,int dex){
int mid=(t[dex].left+t[dex].right)/2;
if(t[dex].left==l && t[dex].right==r)
t[dex].key++ ;
else if(r<=mid)
updata(l,r,dex*2);
else if(l>mid)
updata(l,r,dex*2+1);
else
updata(l,mid,dex*2),updata(mid+1,r,dex*2+1);
}
int query(int l,int r,int dex){
int mid=(t[dex].left+t[dex].right)/2;
if(t[dex].left==l && t[dex].right==r)
return t[dex].key;
int sum=0;
if(l>=t[dex].left && r<=t[dex].right){
sum+=t[dex].key;
if(r<=mid)
sum+=query(l,r,dex*2);
else if(l>mid)
sum+=query(l,r,dex*2+1);
else
sum=sum+query(1,mid,dex*2)+query(mid+1,r,dex*2+1);
}
return sum;
}
int main(){
int n,a,b;
while(scanf("%d",&n)!=EOF && n){
build(1,n,1);
for(int i=1;i<=n;i++){
scanf("%d%d",&a,&b);
updata(a,b,1);
}
for(int i=1;i<n;i++)
printf("%d ",query(i,i,1));
printf("%d\n",query(n,n,1));
}
return 0;
}