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

The sums may exceed the range of 32-bit integers.
 
这是对区间所有点增固定值类的线段树。
 
代码:
#include <iostream>
#include <cstdio>
#define LL long long using namespace std; int n, q; //线段树
const int maxn = 100000;
struct node
{
int lt, rt;
LL val, add;
}tree[4*maxn]; //建立线段树
void Build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = 0;//每段的初值,根据题目要求
tree[id].add = 0;
if (lt == rt)
{
scanf("%I64d", &tree[id].val);
//tree[id].add = ??;
return;
}
int mid = (lt + rt) >> 1;
Build(lt, mid, id << 1);
Build(mid + 1, rt, id << 1 | 1);
tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
} void PushDown(int id, int pls)
{
tree[id<<1].add += tree[id].add;
//tree[id<<1].val += (pls-(pls>>1))*tree[id].add;
tree[id<<1].val += (tree[id<<1].rt-tree[id<<1].lt+1)*tree[id].add;
tree[id<<1|1].add += tree[id].add;
//tree[id<<1|1].val += (pls>>1)*tree[id].add;
tree[id<<1|1].val += (tree[id<<1|1].rt-tree[id<<1|1].lt+1)*tree[id].add;
tree[id].add = 0;
} //增加区间内每个点固定的值
void Add(int lt, int rt, int id, int pls)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
{
tree[id].add += pls;
tree[id].val += pls * (tree[id].rt-tree[id].lt+1);
return;
}
if (tree[id].add != 0)
{
PushDown(id, tree[id].rt-tree[id].lt+1);
}
int mid = (tree[id].lt + tree[id].rt) >> 1;
if (lt <= mid)
Add(lt, rt, id<<1, pls);
if (rt > mid)
Add(lt, rt, id<<1|1, pls);
tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
} LL Query(int lt, int rt, int id)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
return tree[id].val;
if (tree[id].add != 0)
{
PushDown(id, tree[id].rt-tree[id].lt+1);
}
int mid = (tree[id].lt + tree[id].rt) >> 1;
LL ans = 0;
if (lt <= mid)
ans += Query(lt, rt, id<<1);
if (rt > mid)
ans += Query(lt, rt, id<<1|1);
return ans; } int main()
{
//freopen("in.txt", "r", stdin);
char op;
int a, b, k;
while (scanf("%d%d", &n, &q) != EOF)
{
Build(1, n, 1);
for (int i = 0; i < q; ++i)
{
getchar();
op = getchar();
getchar();
scanf("%d%d", &a, &b);
if (op == 'Q')
printf("%I64d\n", Query(a, b, 1));
else
{
scanf("%d", &k);
Add(a, b, 1, k);
}
}
}
return 0;
}

ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)的更多相关文章

  1. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  2. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  3. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  4. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  5. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  6. [poj3468]A Simple Problem with Integers_线段树

    A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...

  7. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  8. 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 ...

  9. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

随机推荐

  1. Visual Studio 2017 for Mac Preview

    Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo 目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4 ...

  2. erlang中遍历取出某个位置的最大值

    例:有这么一个列表,A = [["abc","bds",3],["ssdss","dddx",2],["sfa ...

  3. 前端编程提高之旅(三)----浏览器兼容之IE6

    在爱奇艺实习期间,乐帝主要负责移动端活动页面的制作,因为移动浏览器是随着智能手机兴起的,这就决定了移动端不会重蹈浏览器兼容问题的覆辙.一開始就比較好的支持web标准,而纵观整个互联网行业,移动web开 ...

  4. win732 安装hadoop

    windows下安装hadoop http://www.cnblogs.com/coder2012/archive/2013/05/25/3096631.html硬盘格式应为NTFS1 下载安装 Cy ...

  5. Android_YouthArea之ApeendTextView

    这次给我自己的项目打个广告:http://sj.qq.com/myapp/detail.htm?apkName=com.youthcommunity 这款APP 不同于SoHOT是积极的,是年轻人的信 ...

  6. ClassNotFoundException Log

    Studio 运行时异常: Error:Execution failed for task ':app:compileDebugJavaWithJavac'.> Compilation fail ...

  7. JS实现图片无缝滚动特效;附addEventListener()方法、offsetLeft和offsetWidth属性。

    一:html部分 <body> <input id="btn1" type="button" value="向左"> ...

  8. 利用python进行数据分析之pandas入门

    转自https://zhuanlan.zhihu.com/p/26100976 目录: 5.1 pandas 的数据结构介绍5.1.1 Series5.1.2 DataFrame5.1.3索引对象5. ...

  9. Linq Group By 多个字段

    var counts = dal.QueryStatisticsCount(condition); var result = from p in counts group p by new { Auc ...

  10. 注册HttpHandler

    How to: Register HTTP Handlers After you have created a custom HTTP handler class, you must register ...