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 & ...
随机推荐
- Python 3基础教程30-sys模块
本文介绍sys模块,简单打印两个重定向输出. 目前使用机会没有,以后实际用到了,再去研究和学习.
- 《python核心编程第二版》第8章习题
8–1. 条件语句. 请看下边的代码 # statement Aif x > 0:# statement Bpasselif x < 0:# statement Cpasselse:# s ...
- Leetcode代码补全——二叉树
在刷leetcode的过程中发现,在原网页输入答案是不需要自己构筑树和链表的,虽然便于直接思考算法,但是久而久之类似过于依赖编辑器,反而不知道如何创建树和链表,因此总结了该网页省略的部分,以其中题为例 ...
- 小程序js脚本模块化调用
可以将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块.模块只有通过 module.exports 或者 exports 才能对外暴露接口. 1. common.js // common.j ...
- Go基础篇【第8篇】: 内置库模块 bytes [二]
type Reader ¶ type Reader struct { // 内含隐藏或非导出字段 } Reader类型通过从一个[]byte读取数据,实现了io.Reader.io.Seeker.io ...
- 【转】V8 之旅: 垃圾回收器
垃圾回收器是一把十足的双刃剑.其好处是可以大幅简化程序的内存管理代码,因为内存管理无需程序员来操作,由此也减少了(但没有根除)长时间运转的程序的内存泄漏.对于某些程序员来说,它甚至能够提升代码的性能. ...
- 【Autofac】- 创建的类的生命周期
1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so tha ...
- Chrome Extension & Dark Theme
Chrome Extension & Dark Theme https://chrome.google.com/webstore/detail/eimadpbcbfnmbkopoojfekhn ...
- iMuseum
iMuseum 每日环球展览 iMuseum https://itunes.apple.com/cn/app/%E6%AF%8F%E6%97%A5%E7%8E%AF%E7%90%83%E5%B1%95 ...
- [剑指Offer] 11.二进制中1的个数
[思路]如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话).其余所有位将不会 ...