A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 81519   Accepted: 25185
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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.

Source

 
注意lazy标记的更新
/*
* @Author: LinK
* @Date: 2015-10-31 18:34:32
* @Last Modified by: LinK
* @Last Modified time: 2015-10-31 23:11:18
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; struct Node {
ll sum, add;
} tree[maxn << ]; int n, q;
ll num[maxn]; void build(int rt, int l, int r) {
if (l == r) {
tree[rt].sum = num[l];
tree[rt].add = ;
return;
}
int mid = (l + r) >> ;
build(rt << , l, mid);
build(rt << | , mid + , r);
tree[rt].sum = tree[rt << ].sum + tree[rt << | ].sum;
tree[rt].add = ;
} void update(int rt, int l, int r, int L, int R, ll val) {
if (L <= l && R >= r) {
tree[rt].add += val;
return;
}
tree[rt].sum += val * (min(R, r) - max(L, l) + );
int mid = (l + r) >> ;
if (R <= mid) update(rt << , l, mid, L, R, val);
else if (L > mid) update(rt << | , mid + , r, L, R, val);
else {
update(rt << , l, mid, L, R, val);
update(rt << | , mid + , r, L, R, val);
}
} ll query(int rt, int l, int r, int L, int R) {
if (L <= l && R >= r) {
return tree[rt].sum + tree[rt].add * (r - l + );
}
int mid = (l + r) >> ;
if (tree[rt].add) {
tree[rt].sum += (r - l + ) * tree[rt].add;
tree[rt << ].add += tree[rt].add;
tree[rt << | ].add += tree[rt].add;
tree[rt].add = ;
}
if (R <= mid) return query(rt << , l, mid, L, R);
if (L > mid) return query(rt << | , mid + , r, L, R);
return query(rt << , l, mid, L, R) + query(rt << | , mid + , r, L, R);
} int main() {
char op;
int a, b;
ll c;
while (~scanf("%d %d", &n, &q)) {
for (int i = ; i <= n; i ++) scanf("%lld", &num[i]);
build(, , n);
while (q --) {
scanf(" %c %d %d", &op, &a, &b);
if (op == 'Q') printf("%lld\n", query(, , n, a, b));
else {
scanf("%lld", &c);
update(, , n, a, b, c);
}
}
} return ;
}

POJ3468(线段树区间增加,区间求和)的更多相关文章

  1. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

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

  3. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  4. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  5. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  6. HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)

    Fast Arrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. HDU 1754 I Hate It(线段树单点替换+区间最值)

    I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...

  8. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  9. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

  10. hdu1754(线段树单点替换&区间最值模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶- 思路:线段树单点替换&区间最大值查询模板 代码: #include & ...

随机推荐

  1. 孤荷凌寒自学python第七十三天开始写Python的第一个爬虫3

    孤荷凌寒自学python第七十三天开始写Python的第一个爬虫3 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...

  2. MongoDB的复制一:复制的原理

    1.复制的角色 复制有三种角色: primay:主库,执行所有的写操作,并把日志写入oplog里. secondary:复制主库的所有操作.读取主库的oplog,并执行日志里的内容.默认情况下,客户端 ...

  3. HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)

    Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...

  4. UnrealEngine4入门(一) 新建一个c++项目

    epic games宣布ue4免费使用(游戏发布之后,每个季度大于3000美元则收取收益的5%)之后,吸引了大批看好VR和AR前景的游戏开发者.不过国内(中文)ue4教程和资料太少,而且一大部分资料都 ...

  5. css实现div一直旋转

    看到音乐播放器播放界面的唱片一直旋转,突然想到在网页中的一直旋转效果,所有特地查了一下分享出来 这是一个静态的div,然后把它旋转动起来.效果请看右上角的音乐播放按键一样. 代码如下: <htm ...

  6. 为Ubuntu安装SSH服务

    只有当Ubuntu安装了SSH服务后,我们才能够通过ssh工具登陆Ubuntu.我自己喜欢使用x-shell作为终端工具 1.安装Ubuntu缺省安装了openssh-client,所以在这里就不安装 ...

  7. 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp

    题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...

  8. javascript prototype原型链的原理

    javascript prototype原型链的原理 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: <script type="text/javasc ...

  9. BZOJ2115 [Wc2011] Xor 【线性基】

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 3915  Solved: 1633 [Submit][Stat ...

  10. CF893F Subtree Minimum Query 解题报告

    CF893F Subtree Minimum Query 输入输出格式 输入格式: The first line contains two integers \(n\) and \(r\) ( \(1 ...