单点更新

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. 深入浅出MongoDB(二)概述

    上次的博文深入浅出MongoDB(一)NoSQL中我们已经简单介绍了一下NoSQL的基本概念,这次我们来了解一下MongoDB的相关概念. 1.简介 MongoDB是一款由C++编写的高性能.开源.无 ...

  2. JavaScript相关知识

    JavaScript的语法规则 l JavaScript区分大小写 比如变量a和变量A是不一样的变量,要严格区分大小写 l JavaScript脚本程序须嵌入在HTML文件中 因为javascript ...

  3. linux iostat命令详解 磁盘操作监控工具

    Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视. 它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况. ...

  4. PHP常用函数和常见疑难问题解答

    PHP常用库函数介绍 一.PHP字符串操作常用函数 1.确定字符串长度  int strlen(string str) 2.比较两个字符串 a. strcmp函数对两个字符串进行二进制安全的比较,并区 ...

  5. 修改后的SQL分页存储过程,利用2分法,支持排序

    /****** Object: StoredProcedure [dbo].[sys_Page_v3] Script Date: 08/13/2014 09:32:28 ******/ SET ANS ...

  6. DELPHI 单元文件结构

    unit Unit1; interface {接口部分开始} uses {引用单元列表,这是可选的,如果包含必须紧跟interface关键字} {接口部分声明常量/类型/变量/过程和函数,这些声明对引 ...

  7. ng-summit and $watch() funciton

    <div ng-app> <form ng-submit="requestFunding()" ng-controller="StartUpContro ...

  8. STM32F4_引领入门

    Ⅰ.概述 该文写给那些想学ST芯片开发(或初级学习)的朋友,文章着重细节,或许有点简单. 笔者想告诉那些刚开始学习ST的朋友,不管你使用哪一个系列(F0.F1.F2),哪一种型号芯片,其实学习的方法和 ...

  9. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...

  10. java路径问题总结

    平时写程序的时候,很多时候提示文件找不到,而抛出了异常,现在整理如下[一 相对路径的获得] 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) St ...