单点更新

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

最新文章

  1. Git的使用
  2. C#中的多线程 - 基础知识
  3. Linux源代码情景分析读书笔记 物理页面的分配
  4. 找出文件正在被哪个windows进程使用的方法
  5. php实例-正则获取网站音频地址的实例(Listen to this 1)
  6. linux下使用vim替换文件中的^M换行符
  7. win32 sdk树形控件的项拖拽实现
  8. MySQL中同一时候存在创建和上次更新时间戳字段解决方法浅析
  9. bzoj2876 [Noi2012]骑行川藏
  10. Struts2框架入门
  11. QQ浏览器等window.innerHeight首次读取的高度不正确的解决办法
  12. 【Android 应用开发】Android 开发错误集锦
  13. VS.NET C# 开发ArcGis插件无法进入断点调试的解决方法
  14. Tensoflow API笔记(N) 设备指定
  15. 使用webstrom开发react-native时react-native代码会出现红色下划线的解决方法
  16. Redis学习笔记(二)解析dump.rdb文件工具之redis-rdb-tools
  17. 基于word2vec训练词向量(一)
  18. 2018.12.14 codeforces 922E. Birds(分组背包)
  19. mysql进程文件
  20. web本地存储(localStorage、sessionStorage)

热门文章

  1. phpcms v9 源码解析-1 index.php
  2. 【转】Linux Soclet编程
  3. void *p 类型,illegal indirection错误
  4. 使用Moses中tokenizer.perl无法正常工作:纠结的&quot;&lt;&quot; 和&quot;&gt;&quot;(已解决)
  5. SQL Server 一些关键字详解(二)
  6. Reverse Vowels of a String
  7. ANT编译build.xml
  8. jquery 源码学习(*)
  9. 获取 UIWebView中用户所点击的图片URL
  10. scjp考试准备 - 5 - 重载和重写