NENU_CS_segment_tree
单点更新
http://acm.hdu.edu.cn/showproblem.php?pid=1166
题意:单点更新加减,区间查询求和。
#include<cstdio>
#define lrrt int L,int R,int rt
#define iall 1,n,1
#define imid int mid=(L+R)>>1
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
const int M=5e4+;
int a[M];
char op[];
int tree[M<<];
void pushup(int rt){
tree[rt]=tree[rt<<]+tree[rt<<|];
}
void build(lrrt){
if(L==R){
tree[rt]=a[L];
return ;
}
imid;
build(lson);
build(rson);
pushup(rt);
}
void update(int x,int y,lrrt){
if(L==R){
tree[rt]+=y;
return ;
}
imid;
if(mid>=x) update(x,y,lson);
else update(x,y,rson);
pushup(rt);
}
int query(int x,int y,lrrt){
if(x<=L&&R<=y) return tree[rt];
imid;
int ans=;
if(mid>=x) ans+=query(x,y,lson);
if(mid<y) ans+=query(x,y,rson);
return ans;
}
int main(){
int t,n,x,y;
while(~scanf("%d",&t)){
int cas=;
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(iall);
printf("Case %d:\n",cas++);
while(true){
scanf("%s",op);
if(op[]=='E') break;
scanf("%d%d",&x,&y);
if(op[]=='Q'){
printf("%d\n",query(x,y,iall));
continue;
}
if(op[]=='S') y=-y;
update(x,y,iall);
}
}
}
return ;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1754
题意:单点赋值,区间查询最大值。
#include<cstdio>
#include<algorithm>
#define lrrt int L,int R,int rt
#define iall 1,n,1
#define imid int mid=(L+R)>>1
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
using namespace std;
const int M=2e5+;
int a[M];
int tree[M<<];
void pushup(int rt){
tree[rt]=max(tree[rt<<],tree[rt<<|]);
}
void build(lrrt){
if(L==R){
tree[rt]=a[L];
return ;
}
imid;
build(lson);
build(rson);
pushup(rt);
}
int query(int x,int y,lrrt){
if(x<=L&&R<=y) return tree[rt];
imid;
int ans=;
if(mid>=x) ans=max(ans,query(x,y,lson));
if(mid<y) ans=max(ans,query(x,y,rson));
return ans;
}
void update(int x,int y,lrrt){
if(L==R){
tree[rt]=y;
return ;
}
imid;
if(mid>=x) update(x,y,lson);
else update(x,y,rson);
pushup(rt);
}
int main(){
int n,m,x,y;
char op[];
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(iall);
while(m--){
scanf("%s%d%d",op,&x,&y);
if(op[]=='Q'){
printf("%d\n",query(x,y,iall));
continue;
}
update(x,y,iall);
}
}
return ;
}
成段更新
end
NENU_CS_segment_tree的更多相关文章
随机推荐
- A-Frame_简单介绍
VR框架A-Frame: https://aframe.io 版本: 0.2.0 30/6/2016 A-Frame 是一个能够实现在web网页上实现3D和VR体验的开源框架,它是由 MozVR ...
- Maven的HTTP代理设置
http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html 第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...
- kettle的job
1.首先创建一个job 2.拖拽组件形成下面的图 这里需要注意,在作业中的连线分为三类: 黄色锁的线:这个步骤执行之后,无论失败与否都会执行下一个步骤 绿色对号线:步骤执行成功了,才会执行下一个步骤. ...
- source insight用于C语言编程的工具脚本
简单来说,source insight提供的功能功能还不够傻瓜,用起来还不够方便,所以写了此脚本,提高开发效率. 部分source insight提供的功能也包含了进来,主要是因为我不喜欢使用太多的快 ...
- php简单缓存类
<?phpclass Cache { private $cache_path;//path for the cache private $cache_expire;//seconds ...
- 网站瓶颈分析—MYSQL性能分析
一.关于慢查询设置和分析 查找慢查询参数 mysql> show variables like 'long%'; +-----------------+----------+ | Variabl ...
- 生日蛋糕 (codevs 1710) 题解
[问题描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1<=i<=M)层蛋糕是半径为Ri,高度为Hi的圆柱 ...
- 02-线性结构3 Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- STM32F0xx_看门狗(独立+窗口)配置详细过程
Ⅰ.概述 对于看门狗,我觉得做单片机或者嵌入式开发的人员来说并不陌生,今天总结STM32F0看门狗的功能,F0的看门狗有两种:独立和窗口看门狗. 今天提供两种看门狗的软件工程实例,供大家下载. 两种看 ...
- python2.7抓取豆瓣电影top250
利用python2.7抓取豆瓣电影top250 1.任务说明 抓取top100电影名称 依次打印输出 2.网页解析 要进行网络爬虫,利用工具(如浏览器)查看网页HTML文件的相关内容是很有必要,我使用 ...