3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
1 1 1 3 2 1
//734MS 4340K #include<stdio.h> struct Tree { int l,r,mid,num; }tree[300007]; int count; void build(int left,int right,int i) { tree[i].l=left;tree[i].r=right;tree[i].num=0; tree[i].mid=(left+right)>>1; if(left==right)return; build(left,tree[i].mid,2*i); build(tree[i].mid+1,right,2*i+1); } void insert(int left,int right,int i) { if(tree[i].l==left&&tree[i].r==right){tree[i].num++;return;} if(tree[i].mid<left)insert(left,right,2*i+1); else if(tree[i].mid>=right)insert(left,right,2*i); else { insert(left,tree[i].mid,2*i); insert(tree[i].mid+1,right,2*i+1); } } void search(int id,int i) { count+=tree[i].num; if(tree[i].l==tree[i].r&&tree[i].l==id)return; if(tree[i].mid>=id)search(id,i*2); else search(id,i*2+1); } int main() { int n,a,b; while(scanf("%d",&n),n) { build(1,n,1); for(int i=1;i<=n;i++) { scanf("%d%d",&a,&b); insert(a,b,1); } for(int i=1;i<n;i++) { count=0; search(i,1); printf("%d ",count); } count=0; search(n,1); printf("%d\n",count); } return 0; }
HDU 1556 Color the ball 线段树更新区间查点,布布扣,bubuko.com
HDU 1556 Color the ball 线段树更新区间查点
原文:http://blog.csdn.net/crescent__moon/article/details/38336709