【POJ3468】【zkw线段树】A Simple Problem with Integers
Description
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
Hint
Source
/*
宋代陆游
《临安春雨初霁》 世味年来薄似纱,谁令骑马客京华。
小楼一夜听春雨,深巷明朝卖杏花。
矮纸斜行闲作草,晴窗细乳戏分茶。
素衣莫起风尘叹,犹及清明可到家。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int INF = 0x7fffffff;
const int MAXN = + ;
const int maxnode = ;
using namespace std;
struct Node{
int l, r;
long long sum, d;
}tree[maxnode];
int data[MAXN], M;//M为总结点个数 //初始化线段树
void build(int l, int r){
for (int i = * M - ; i > ; i--){
if (i >= M){
tree[i].sum = data[i - M];
tree[i].l = tree[i].r = (i - M);//叶子节点
}
else {
tree[i].sum = tree[i<<].sum + tree[(i<<)+].sum;
tree[i].l = tree[i<<].l;
tree[i].r = tree[(i<<)+].r;
}
}
}
long long query(int l, int r){
long long sum = ;//sum记录和
int numl = , numr = ;
l += M - ;
r += M + ;
while ((l ^ r) != ){
if (~l&){//l取反,表示l是左节点
sum += tree[l ^ ].sum;
numl += (tree[l ^ ].r - tree[l ^ ].l + );
}
if (r & ){
sum += tree[r ^ ].sum;
numr += (tree[r ^ ].r - tree[r ^ ].l + );
}
l>>=;
r>>=;
//numl,numr使用来记录标记的
sum += numl * tree[l].d;
sum += numr * tree[r].d;
}
for (l >>= ; l > ; l >>= ) sum += (numl + numr) * tree[l].d;
return sum;
} void add(int l, int r, int val){
int numl = , numr = ;
l += M - ;
r += M + ;
while ((l ^ r) != ){
if (~l&){//l取反
tree[l ^ ].d += val;
tree[l ^ ].sum += (tree[l ^ ].r - tree[l ^ ].l + ) * val;
numl += (tree[l ^ ].r - tree[l ^ ].l + );
}
if (r & ){
//更新另一边
tree[r ^ ].d += val;
tree[r ^ ].sum += (tree[r ^ ].r - tree[r ^ ].l + ) * val;
numr += (tree[r ^ ].r - tree[r ^ ].l + );
}
l>>=;
r>>=;
tree[l].sum += numl * val;
tree[r].sum += numr * val;
}
//不要忘了往上更新
for (l >>= ; l > ; l >>= ) tree[l].sum += (numl+numr) * val;
}
int n, m; void init(){
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &data[i]);
M = ;while (M < (n + )) M <<= ;//找到最大的段
build(, M - );
}
void work(){
while (m--){
char str[];
scanf("%s", str);
if (str[] == 'Q'){
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(l, r));
}
else{
int val, l, r;
scanf("%d%d%d", &l, &r, &val);
if (val != ) add(l, r, val);
}
}
} int main(){ init();
work();
return ;
}
【POJ3468】【zkw线段树】A Simple Problem with Integers的更多相关文章
- 线段树---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 (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149972 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- HDU-2126 Buy the souvenirs
数组01背包. http://acm.hdu.edu.cn/showproblem.php?pid=2126 http://blog.csdn.net/crazy_ac/article/details ...
- 通过UIImagePickerController完成照相和相片的选取
UIImagePickerController是用于选取现有照片,或者用照相机现场照一张相片使用的 定义: @interface ShowViewController : UIViewControll ...
- 关于Marsedit和我的163博客
其实我一开始选择的并不是163的博客,感觉没什么新意,勾不起我的兴趣. 第一个我想写的是CSDN的博客,代码嘛,总要找个专业的网站来写,但是我发现了一个及其让我不爽的事情,我竟然在网页上无法新建一篇日 ...
- C# 中的协变和逆变
作为一个从接触 Unity 3D 才开始学习 C# 的人,我一直只了解一些最基本.最简单的语言特性.最近看了<C# in Depth>这本书,发现这里面东西还真不少,即使除去和 Windo ...
- Page Cache buffer Cache
https://www.thomas-krenn.com/en/wiki/Linux_Page_Cache_Basics References Jump up ↑ The Buffer Cache ( ...
- 初学scala1——Option
Scala的Option[T]是容器对于给定的类型的零个或一个元件.Option[T]可完美替代Java中的null,可以是Some[T]或者None. 例如,Scala Map的get方法输出即为O ...
- K倍动态减法游戏
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2580 #include <iostream> #include <string.h> ...
- 跨服务器修改数据 分类: SQL Server 2014-08-21 21:24 316人阅读 评论(0) 收藏
说明: 两个服务器: 192.168.0.22 A 192.168.0.3 B 数据库备份在A上 数据库在B上 在A上写: exec sp_addlinkedserver 'ITSV ...
- 将.lib库文件转换成.a库文件的工具
分享如下两个链接: 微盘: http://vdisk.weibo.com/s/ztzPTJIC52mz2 百度云盘: http://pan.baidu.com/s/11gTOc 使用方法,解压文件mi ...
- Delphi 颜色转换
http://files.cnblogs.com/xe2011/StringToColor.rar unit Unit1; interface uses Windows, Messages, SysU ...