poj3468
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100005
typedef long long ll;
ll sum[maxn<<];
ll lazy[maxn<<];
inline void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
}
inline void pushdown(int rt,int m){
if(lazy[rt]){
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
sum[rt<<]+=(m-(m>>))*lazy[rt];
sum[rt<<|]+=(m>>)*lazy[rt];
lazy[rt]=;
}
}
void build(int l,int r,int rt){
lazy[rt]=;
if(l==r){
scanf("%lld",&sum[rt]);
return;
}
int m=l+r>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int val,int l,int r,int rt){
if(L<=l && R>=r){
lazy[rt]+=val;
sum[rt]+=(r-l+)*val;
return;
}
pushdown(rt,r-l+);
int m=l+r>>;
if(L<=m) update(L,R,val,lson);
if (R>m) update(L,R,val,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt){
if(L<=l && R>=r){
return sum[rt];
}
pushdown(rt,r-l+);
int m=l+r>>;
ll ret=;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}
int main(){
int n,q,a,b,c;
while(scanf("%d%d",&n,&q)==){
build(,n,);
char op[];
while(q--){
scanf("%s",op);
if(op[]=='Q'){
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,,n,));
}
else {
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,,n,);
}
}
}
return ;
}
poj3468的更多相关文章
- 【poj3468】 A Simple Problem with Integers
http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...
- poj3468,poj2528
其实这两题都是基础的线段树,但对于我这个线段树的初学者来说,总结一下还是很有用的: poj3468显然是线段树区间求和,区间更改的问题,而poj2528是对区间染色,问有多少种颜色的问题: 线段树的建 ...
- poj3468 线段树的懒惰标记
题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- 洛谷P3372/poj3468(线段树lazy_tag)(询问区间和,支持区间修改)
洛谷P3372 //线段树 询问区间和,支持区间修改 #include <cstdio> using namespace std; struct treetype { int l,r; l ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
- poj3468 A Simple Problem with Integers(zkw区间修改模板)
此题是一道线段树的裸题,这里只是为了保存我的zkw线段树模板 #include <cstdio> #include <cstring> #include <iostrea ...
- [POJ3468] A Simple Problem with Integers (Treap)
题目链接:http://poj.org/problem?id=3468 这题是线段树的题,拿来学习treap. 不旋转的treap. #include <cstdio> #include ...
随机推荐
- pubmed_cookie 自动获取
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...
- 深入分析HashMap
本文基于jdk1.8 HashMap特点: HashMap具体方法分析: put方法分析: 执行流程图: public V put(K key, V value) { return putVal(ha ...
- JAVA记录-SpringMVC+Mybatis几个核心注意的地方
1.DispatcherServlet -- 前置控制器 DispatcherServlet是一个Servlet,所以可以配置多个DispatcherServlet. DispatcherServ ...
- .Net进阶系列(11)-异步多线程(委托BeginInvoke)(被替换)
一. BeginInvoke最后两个参数的含义 倒数第二个参数:指该线程执行完毕后的回调函数:倒数第一个参数:可以向回调函数中传递参数. 下面以一段代码说明: /// <summary> ...
- springboot+rabbitmq整合示例程
关于什么是rabbitmq,请看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一.新建maven工程:springboot-rabbit ...
- MyBatis第一个案例-----永远的HelloWorld 含所有代码
1.创建表emp CREATE DATABASE mybatis; USE mybatis; CREATE TABLE emp( id INT(11) PRIMARY KEY AUTO_INCREME ...
- C# 批量修改文件名
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- linux space/mark设置
参考链接: http://blog.csdn.net/zhaozh2000/article/details/6460223 串口数据的发送: 数据的发送: 数据位+检验位+停止位, 进行数据的发送.( ...
- sizeof strlen区别于联系
http://www.cnblogs.com/carekee/articles/1630789.html
- Linux下创建C函数库
http://blog.163.com/hitperson@126/blog/static/130245975201151552938133 http://blog.sina.com.cn/s/blo ...