hdu 1556 Color the ball(线段树)
2013-05-04 15:15
363 查看
我的第一道线段树!
说出这句话的时候一阵心酸,之前一直说要学,看看又觉得好难啊,往后推。一直推到今天,咬咬牙,终于学了。虽然只是很简单的做了一道题,甚至线段树的思想还掌握不了,可毕竟是一个开端啊。
说出这句话的时候一阵心酸,之前一直说要学,看看又觉得好难啊,往后推。一直推到今天,咬咬牙,终于学了。虽然只是很简单的做了一道题,甚至线段树的思想还掌握不了,可毕竟是一个开端啊。
#include<stdio.h> #define N 1000005 struct node { int x,y; int count; }a[N*2]; void CreatTree(int t,int x,int y) { int temp=t*2; int mid=(x+y)/2; if(x==y) return ; a[t].x=x; a[t].y=y; a[t].count=0; if(x+1==y) return ; CreatTree(temp,x,mid); CreatTree(temp+1,mid,y); return ; } void InsertTree(int t,int x,int y) { int mid=(a[t].x+a[t].y)/2; int temp=t*2; if(a[t].x==x&&a[t].y==y) a[t].count++; else if(y<=mid) InsertTree(temp,x,y); else if(x>=mid) InsertTree(temp+1,x,y); else { InsertTree(temp,x,mid); InsertTree(temp+1,mid,y); } return ; } int CountTree(int t,int x,int y) { int mid=(a[t].x+a[t].y)/2; int temp=t*2; if(x==a[t].x&&y==a[t].y) return a[t].count; int sum=0; if(x>=a[t].x&&y<=a[t].y) { sum+=a[t].count; if(y<=mid) sum+=CountTree(temp,x,y); else if(x>=mid) sum+=CountTree(temp+1,x,y); } return sum; } int main() { int n,x,y,i; while(scanf("%d",&n),n) { CreatTree(1,1,n+1); for(i=0;i<n;i++) { scanf("%d%d",&x,&y); InsertTree(1,x,y+1); } printf("%d",CountTree(1,1,2)); for(i=2;i<=n;i++) printf(" %d",CountTree(1,i,i+1)); printf("\n"); } return 0; }
相关文章推荐
- hdu 1556 Color the ball 线段树 区间更新
- hdu 1556 Color the ball(线段树 区间更新单点查询)
- hdu 1556 Color the ball (线段树+代码详解)
- HDU 1556 Color the ball 线段树
- hdu 1556 color the ball 线段树区间更新 加lazy标记
- hdu 1556 Color the ball (线段树之扫描线)
- hdu 1556 Color the ball(线段树的另一种应用)
- HDU 1556 Color the Ball 线段树 题解
- hdu 1556 Color the ball(非线段树做法)
- HDU 1556 Color the ball(线段树)
- HDU 1556 Color the ball(线段树区间更新区间查询模板)
- HDU - 1556 Color the ball 线段树
- hdu 1556 Color the ball (树状数组||线段树成段更新)
- HDU 1556 Color the ball(线段树)
- HDU 1556 Color the ball(线段树)
- 【线段树II:区间修改+点查询】hdu 1556 Color the ball
- hdu 1556 Color the ball 线段树
- HDU 1556 Color the ball 线段树入门题
- hdu 1556:Color the ball(线段树,区间更新,经典题)
- HDU 1556 Color the ball(线段树区间更新)