单点更新

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的更多相关文章

随机推荐

  1. mr的logs的查看

    在map或者reduce函数中使用System.out.println打印的信息沾满查看呢? 步骤1:启动history server /usr/local/hadoop-2.6.0/sbin/mr- ...

  2. Hbase Interface HConnection

    HTablePool 在Hbase 0.94.0.95.0.97被废弃,在0.98中被清除( HTablePool 对比HConnection.getTable),hbase0.98 HTablePo ...

  3. try 返回前执行fianlly

    try catch  finally 语句中 如果try中有返回语句,如果在fianlly代码块中有对这个值修改的话,并不影响其放回值 public class Test { public stati ...

  4. 开源web终端ssh解决方案-gateone简介

    好久都没来写博客,最近忙啥去了呢? 一是忙于saltstack的二次开发,二是云计算的学习研究中,所以就一直没写东西,今天给大家介绍个工具. 1. 首先来说一下为什么要 web ssh? 许多人不是说 ...

  5. DevExpress控件使用经验总结- GridView列表行号显示操作

    DevExpress是一个比较有名的界面控件套件,提供了一系列的界面控件套件的DotNet界面控件.本文主要介绍我在使用DevExpress控件过程中,遇到或者发现的一些问题解决方案,或者也可以所示一 ...

  6. Android LogCat使用详解

    Android的Logcat用于显示系统的调试信息,可在分别以下几个地方查看和调用logcat: 1.eclipse的Debug模式或DDMS模式下的会有一个Logcat窗口,用于显示log日志 只需 ...

  7. python网络画图——networkX

    networkX tutorial 绘制基本网络图 用matplotlib绘制网络图 基本流程: 1. 导入networkx,matplotlib包 2. 建立网络 3. 绘制网络 nx.draw() ...

  8. Jquery制作可以绑定的表格

    //总页数 当前页 可见页 参数 翻页执行后处理的函数 function PageTable(totalPages, currentPage, tableobj, url, where, column ...

  9. 自学Python二 Python中的屠龙刀(续)

    函数 秉承着一切皆对象的理念,函数作为对象,可以为其赋值新的对象名,也可以作为参数传递给其他函数! 正常的诸如空函数,默认参数等等我们就不提了,在这里着重提一下默认参数里面的坑和lambda函数. 当 ...

  10. hdu 1622 Trees on the level

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...