线段树模板(hdu1166)
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <bitset>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
const int maxn=5e4+1;
string gao;
int tree[maxn<<2],a[maxn<<2],t,n;
void build(int now,int l,int r){
if(l==r){tree[now]=a[l];return;}//叶子节点直接赋值
int mid=(l+r)/2;
build(now<<1,l,mid);
build(now<<1|1,mid+1,r);
tree[now]=tree[now<<1]+tree[now<<1|1];//向下传递
}
void update(int now,int l,int r,int val,int ne){
if(l==ne&&l==r){
tree[now]+=val;
return;
}else{
int mid=(l+r)/2;
if(ne<=mid)update(now<<1,l,mid,val,ne);
if(ne>mid)update(now<<1|1,mid+1,r,val,ne);
tree[now]=tree[now<<1]+tree[now<<1|1];
}
}
LL que(int now,int l,int r,int dx,int dy){
if(l>=dx&&r<=dy)return tree[now];
else{
int mid=(l+r)/2;
LL sum=0;
if(dx<=mid)sum+=que(now<<1,l,mid,dx,dy);
if(dy>mid) sum+=que(now<<1|1,mid+1,r,dx,dy);
return sum;
}
}
int main(){
ios::sync_with_stdio(false);
cin>>t;
for(int test=1;test<=t;test++){
mem(tree);
cout<<"Case "<<test<<':'<<endl;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
build(1,1,n);
while(1){
cin>>gao;
if(gao=="End")break;
if(gao[0]=='Q'){
int l,r;
cin>>l>>r;
cout<<que(1,1,n,l,r)<<endl;
}else if(gao[0]=='S'){//减
int p,val;
cin>>p>>val;
update(1,1,n,-val,p);
}else{//加
int p,val;
cin>>p>>val;
update(1,1,n,val,p);
}
}
}
return 0;
}
线段树模板(hdu1166)的更多相关文章
- HDU1166:敌兵布阵(线段树模板)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 1166 线段树模板&树状数组模板
HDU1166 上好的线段树模板&&树状数组模板 自己写的第一棵线段树&第一棵树状数组 莫名的兴奋 线段树: #include <cstdio> using nam ...
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- hdu1754 I hate it线段树模板 区间最值查询
题目链接:这道题是线段树,树状数组最基础的问题 两种分类方式:按照更新对象和查询对象 单点更新,区间查询; 区间更新,单点查询; 按照整体维护的对象: 维护前缀和; 维护区间最值. 线段树模板代码 # ...
- P3373 线段树模板
好,这是一个线段树模板. #include <cstdio> using namespace std; ; long long int sum[N],tag1[N],tag2[N],mo; ...
- 线段树模板hdu 1754:I Hate It
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- UESTC - 1057 秋实大哥与花 线段树模板题
http://acm.uestc.edu.cn/#/problem/show/1057 题意:给你n个数,q次操作,每次在l,r上加上x并输出此区间的sum 题解:线段树模板, #define _CR ...
- POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 140120 ...
- hdu 4819 二维线段树模板
/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits ...
- POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149972 ...
随机推荐
- tyvj/joyoi 1043 表达式计算4
这题怎么这么毒瘤... 一开始我想转后缀表达式来计算,后来发现有负数...弃疗. 递归求解又发现会有多余括号,我觉得不行... 实在是毒瘤啊! #include <cstdio> #inc ...
- LOJ#2245 魔法森林
这道题以前zbtrs大佬给我讲过.但是我只知道思想,不知道要lct维护... 这个套路很常见. 题意:给你一个无向图,每条边有a,b两个权值.求1到n号点的一条路径,路径的权值是每条边的最大a与最大b ...
- PHP数组大全
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- 转:mysql分页原理和高效率的mysql分页查询语句
(转自:http://www.jb51.net/article/46015.htm) 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页 ...
- flask学习之路
目录 flask初识 flask模板 flask的session flask路由 flask配置和实例化传参 flask蓝图 更新中
- rownum查询前N条记录
在Oracle中,要按特定条件查询前N条记录,用个rownum就搞定了.——select * from emp where rownum <= 5 而且书上也告诫,不能对rownum用" ...
- SQL Server 经典案例
1.先进先出 例1 WITH [ta] ([商品编号], [批次号], [库存数量]) AS ( UNION ALL UNION ALL UNION ALL ),[tb] ([商品编号], [订货数量 ...
- Java基础super关键字、final关键字、static关键字、匿名对象整理
super关键字 10.1子父类中构造方法的调用 public class Test { public static void main(String[] args) { new Zi(); } } ...
- MySQL入门介绍(mysql-8.0.13)
MySQL入门介绍(mysql-8.0.13单机部署) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL数据库介绍 1>.MySQL是一种开放源代码的关系型数据库 ...
- CentOS 网络设置
CentOS 网络设置有两种方式配置网络可以连接外网: 自动分配与手动设置Ip 一,自动分配ip: 新装的centos6.0,用ifconfig只显示ifcfg-lo的信息,可以用ifup eth0的 ...