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的更多相关文章
随机推荐
- DevExpress 重编译 替换强命名 修改源码
本文以DevExpress 11.1.8举例 必须满足几个条件 1. 必须有DXperience相应版本的全部源代码SourceCode.把全部源代码复制到X:\Program Files\DevEx ...
- 相比于汇编语言的准确性c语言延时精确度如何提升
只要合理的运用,C还是可以达到意想不到的效果.很多朋友抱怨C效率比汇编差了很多,其实如果对Keil C的编译原理有一个较深入的理解,是可以通过恰当的语法运用,让生成的C代码达到最优化.即使这看起来不大 ...
- STM32F0xx_USART收发配置详细过程
前言 串口对于处理器来说算是一种标配,也是在软件开发中必不可少的,那就是使用串口来调试信息(打印出相应的信息).STM32F0系列的芯片,串口根据型号不同,数量也不同,从1个到8个不等. 今天主要总结 ...
- python二叉树递归算法之后序遍历,前序遍历,中序遍历
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2016-11-18 08:53:45 # @Author : why_not_try ...
- python Django 学习笔记(五)—— Django admin自动管理界面
1,激活管理界面 修改settings.py MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.c ...
- SQLserver查询数据库所有字段-表名
SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT (case when a.colorder=1 t ...
- OSGi在淘宝内部的使用
现在基本不怎么用了,OSGi主要的价值,在实际中体现得不太明显 比如类隔离,用更简单的自定义ClassLoader也可以实现:单机多版本服务,用的场景也很少:热部署也不是很实用 但是,基于OSGi框架 ...
- python小算法(一)
1.长度为m的字符串a,长度为n的字符串b,(m>n) 判断b中的字母是否全在a中? O(n)最小. class Num(object): def getNum(self, m): numLis ...
- iOS进阶学习-数据处理之文件读写
一.沙盒机制 1.什么是沙盒? 每一个iOS应用程序都会为自己创建一个文件系统目录(文件夹),这个独立.封闭.安全的空间,叫做沙盒. 2.沙盒机制(SandBox) 沙盒是一种安全体系. 它规定了应用 ...
- C# SQL增删查改
DBHelper: /// <summary> /// 执行查询 /// </summary> /// <param name="sql">有效 ...