POJ3468(线段树区间增加,区间求和)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 81519 | Accepted: 25185 | |
Case Time Limit: 2000MS |
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
/*
* @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(线段树区间增加,区间求和)的更多相关文章
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 140120 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)
Fast Arrangement Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 1754 I Hate It(线段树单点替换+区间最值)
I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- hdu2795(线段树单点更新&区间最值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...
- hdu1754(线段树单点替换&区间最值模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶- 思路:线段树单点替换&区间最大值查询模板 代码: #include & ...
随机推荐
- Qt QPainter::end: Painter ended whith 2 saced states
在使用Qt QPainter 的时候,有时会遇到“QPainter::end: Painter ended whith 2 saced states” 这时由于我们在使用的QPanter.trans ...
- Java IO学习--File类
一.File类 File类具备一定的误导性,可能容易认为它指代的是文件,实际并非如此,它既能代表一个特定文件的名称,又能表示一个目录下一组文件的名称.简而言之,File类是文件或者目录路径名的抽象表示 ...
- python 网络编程(socketserver,阻塞,其他方法)
重点回顾: (重点)粘包 : 就是因为接收端不知道如何接收数据,造成接收数据的混乱的问题 只发生在tcp协议上. 因为tcp协议的特点是面向数据流形式的传输 粘包的发生主要是因为tcp协议有两个机制: ...
- 杜绝网上压根没测过就乱写之 《oracle mybatis 返回自增主键 》
面试过好多人,包括自己也属于这么一个情况: 遇到问题直接去网上查,一般都可以查到解决方案.其中也包括一些基本的面试资料的答案. 其实有很多答案也都是正确的,但是还是存在一些压根就是胡乱抄来的答案,也不 ...
- Week9 Teamework from Z.XML-Scenario testing
关于场景测试 About Scenario testing 一.关于用户(About Personas) 1 我们如何预期我们的用户对我们的软件的使用 (How do we expect diff ...
- Java 无法初始化Connection的问题
通过断点调试捕获错误消息:The server time zone value '�й���ʱ��' is unrecognized or represents more than one time ...
- CentOS7 php 安装 amqp扩展
继续安装完 rabbitmq后,安装最新 php amqp扩展 http://www.cnblogs.com/8000cabbage/p/7788575.html 参考:carson 1.安装rabb ...
- 【bzoj1257】[CQOI2007]余数之和sum 数论
题目描述 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, 3)=3 m ...
- BZOJ4367 IOI2014holiday假期(整体二分+主席树)
显然最优策略是先走到一边要到达的最远城市,再换方向走到另一边要到达的最远城市(当然也可以直接停止),路上参观景点. 先仅考虑求出只向左走,花费时间i时的最优解.如果能求出这个,类似的就可以求出所有情况 ...
- arc068 E: Snuke Line
首先要知道 (m/1 + m/2 + ... + m/m) 约为 mlogm 还有一个比较明显的结论,如果一个纪念品区间长度大于d,那么如果列车的停车间隔小于等于d,则这个纪念品一定能被买到 然后把区 ...