POJ-3468 A Simple Problem with Integers (区间求和,成段加减)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
区间求和,成段加减
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef long long LL;
const int N = + ;
LL T[N<<],add[N<<]; void PushUP(int rt){
T[rt] = T[rt<<] + T[rt<<|];
} void PushDown(int rt, int m){
if(add[rt]){
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
T[rt<<] += add[rt]*(LL)(m - (m >> ));
T[rt<<|] += add[rt]*(LL)(m >> );
add[rt] = ;
}
}
void Build(int l, int r, int rt){
add[rt] = ;
if(l == r){
scanf("%lld",&T[rt]);
return ;
}
int m = (l + r) >> ;
Build(lson);
Build(rson);
PushUP(rt);
} void Updata(int L,int R, int c,int l, int r, int rt){
if(L <= l && r <= R){
add[rt] += c;
T[rt] += (LL)c*(r - l + );
return ;
}
PushDown(rt, r - l + );
int m = (l + r) >> ;
if(L <= m) Updata(L, R, c, lson);
if(R > m) Updata(L, R, c, rson);
PushUP(rt);
} LL Query(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return T[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,m;
while(scanf("%d %d",&n,&m)==){
Build(, n, );
char ch[];
int a,b,c;
while(m--){
scanf("%s %d %d",ch,&a,&b);
if(ch[] == 'Q')printf("%lld\n",Query(a, b, , n, ));
else{
scanf("%d",&c);
Updata(a, b, c, , n, );
}
}
}
return ;
}
POJ-3468 A Simple Problem with Integers (区间求和,成段加减)的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- webstorm注册码,亲测2016.1.1版
打开webstorm,点击帮助,注册 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog. ...
- Spring Boot文件上传
一.创建一个简单的包含WEB依赖的SpringBoot项目 二.配置文件上传的文件大小限制 # 上传文件总的最大值spring.servlet.multipart.max-request-size=1 ...
- 【noip2016提高组day2T3】【愤怒的小鸟】状压dp转移时的集合包含
(上不了p站我要死了,图来自百度,侵权度娘背锅) 调死我了... 标题就说明了,死在了集合包含上.因为这道题与其他的状压题不同,其他的题基本上都是要求集合不重合,而这道题完全是可以的. 废话不多说,先 ...
- HDU 5919 Sequence ll
Time limit 4500 ms Memory limit 131072 kB OS Windows Source 2016中国大学生程序设计竞赛(长春)-重现赛 中文题意 一个长度为n的序列,里 ...
- 果蝇优化算法(FOA)
果蝇优化算法(FOA) 果蝇优化算法(Fruit Fly Optimization Algorithm, FOA)是基于果蝇觅食行为的仿生学原理而提出的一种新兴群体智能优化算法. 果蝇优化算法(FOA ...
- A Network in a Laptop: Rapid Prototyping for Software-Defined Networks
文章名称:A Network in a Laptop: Rapid Prototyping for Software-Defined Networks 文章来源:Lantz B , Heller B ...
- Java数据结构与算法(4):二叉查找树
一.二叉查找树定义 二叉树每个节点都不能有多于两个的儿子.二叉查找树是特殊的二叉树,对于树中的每个节点X,它的左子树中的所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项. 二叉查找树节点的 ...
- 标签button:点击button按钮时,出现了页面自动刷新的情况
原html: <button class="btn btn-primary" id="btnSubmit" name="btnSubmit&qu ...
- iOS即时通讯之CocoaAsyncSocket源码解析一
申明:本文内容属于转载整理,原文连接 前言: CocoaAsyncSocket是谷歌的开发者,基于BSD-Socket写的一个IM框架,它给Mac和iOS提供了易于使用的.强大的异步套接字库,向上封装 ...
- ASP Loading
ASP 页面加载等待效果.如 显示"请稍后页面正在加载...",加载完成后隐藏这个loading. <%Response.buffer=false%> <html ...