经典 hdu1556 线段树
来源:互联网 发布:java支付接口 编辑:程序博客网 时间:2024/05/09 06:14
http://acm.hdu.edu.cn/showproblem.php?pid=1556
感觉对线段树还不是很熟练,通过此题学到这种结构体设置方法(线段树的关键就是在结构体的设置),如果对区间内所有元素做同样操作,只需对线段树上对应区间操作即可。否则如果对所有元素操作,会超时,但进行query()要计算到叶子节点。
#include<iostream>#include<cstdio>#include<cstring>#define N 100000using namespace std;struct node{int l,r;int count;int mid(){return (l+r)>>1;}};node tree[N*3];void buildtree(int l,int r,int pos){tree[pos].l=l;tree[pos].r=r;tree[pos].count=0;if(l==r)return ;buildtree(l,tree[pos].mid(),pos<<1);buildtree(tree[pos].mid()+1,r,(pos<<1)+1);}void update(int l,int r,int pos){if(tree[pos].l==l&&tree[pos].r==r){tree[pos].count++;return ;}if(r<=tree[pos].mid()){update(l,r,pos<<1);}else if(l>tree[pos].mid()){update(l,r,(pos<<1)+1);}else{update(l,tree[pos].mid(),pos<<1);update(tree[pos].mid()+1,r,(pos<<1)+1);}}void query(int pos,int sum){if(tree[pos].l==tree[pos].r){if(tree[pos].l==1)printf("%d",tree[pos].count+sum);elseprintf(" %d",tree[pos].count+sum);return ;}query(pos<<1,tree[pos].count+sum);query((pos<<1)+1,tree[pos].count+sum);}int main(){int n;while(scanf("%d",&n),n){int i,t1,t2;memset(tree,0,sizeof(tree));buildtree(1,n,1);for(i=0;i<n;i++){scanf("%d%d",&t1,&t2);update(t1,t2,1);}query(1,0);printf("\n");}}
- 经典 hdu1556 线段树
- HDU1556 线段树入门
- HDU1556 线段树
- HDU1556(线段树)
- HDU1556 - 线段树基础
- hdu1556(线段树)
- hdu1556 线段树 区间修改
- hdu1556 线段树更新区间
- hdu1556--刷气球--线段树
- HDU1556 Color the ball(线段树)
- hdu1556-color the ball 线段树
- hdu1556 Color the ball 线段树
- hdu1556 线段树区间更新 求单点
- hdu1556 Color the ball 线段树
- hdu1556树状数组&&线段树2种做法
- HDU1556 Color the ball 解题报告--线段树
- HDU1556:Color the ball(线段树区间更新)
- hdu1556 线段树段更新(简单题)
- MFC 菜单处理及消息截获(利用虚函数的特性实现)
- C专家编程(二)
- 生成金额大写的实例
- Hibernate中Session的操作解释
- MyBatis的动态SQL详解
- 经典 hdu1556 线段树
- UVA 10129 Play on Words
- 如何完全卸载Sql Server 2005
- OGRE 描边效果
- Java就业前景分析
- java transient简介
- Android应用性能测试
- u-boot引导程序
- 调用ANDROID系统“应用程序信息(APPLICATION INFO)”界面