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. 虚拟现实-VR-UE4-获取UE4

    UE4现在虽然是开源,但是并不是免费的,在你的游戏成功后,回收取5%费用和每个月19美元的费用 所以,第一步,进去UE4官网:https://www.unrealengine.com/zh-CN/wh ...

  2. 第八篇Python基本数据类型之列表、元组与字典

    列表 写在最前,必须要会的:append(),extend(),insert(),索引,切片,循环 list  是一个类,是个对象 列表用 方括号[]括起来的,[]内以逗号分割每个元素,列表中的元素可 ...

  3. CodeForces - 948C(前缀和 + 二分)

    链接:CodeForces - 948C 题意:N天,每天生产一堆雪体积 V[i] ,每天每堆雪融化 T[i],问每天融化了多少雪. 题解:对 T 求前缀和,求每一堆雪能熬过多少天,再记录一下多余的就 ...

  4. python 学习总结----正则表达式

    正则表达式 应用场景 - 特定规律字符串的查找,切割,替换 - 邮箱格式:URl,IP地址等的校验 - 爬虫项目中,特定内容的提取 使用原则 - 只要使用字符串等函数能解决的问题,就不要使用正则 - ...

  5. SPOJ 3978 Distance Query(tarjan求LCA)

    The traffic network in a country consists of N cities (labeled with integers from 1 to N) and N-1 ro ...

  6. BFS搜索

    参考博客:[算法入门]广度/宽度优先搜索(BFS) 适用问题:一个解/最优解 重点:我们怎么运用队列?怎么记录路径? 假设我们要找寻一条从V0到V6的最短路径.(明显看出这条最短路径就是V0-> ...

  7. 《Effective STL》学习笔记

    http://www.cnblogs.com/arthurliu/archive/2011/08/07/2108386.html 作者:咆哮的马甲 出处:http://www.cnblogs.com/ ...

  8. 批处理之FOR命令

  9. 框架开发中的junit单元测试

    首先写一个测试用的公共类,如果要搭建测试环境,只要继承这个公共类就能很容易的实现单元测试,代码如下 import org.junit.runner.RunWith; import org.spring ...

  10. Hibernate常用方法之_删除

    1.使用session的delete方法 public void deleteUser(User user){ Session session = null; Transaction transact ...