A Simple Problem with Integers
Time limit | 5000 ms |
---|---|
Case time limit | 2000ms |
Memory limit | 131072 kB |
OS | Linux |
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.
你有n个整数,A1,A2,…An。你需要处理两种操作,一种操作是在给定的区间内为每个数字添加一个给定的数字,另一种是求给定区间内的数字之和。
- 输入
第一行包含两个数字n和q. 1 ≤ n,q ≤ 100000
第二行包含n个数字,A1,A2,…An. -100000000 ≤ AI ≤ 100000000
下一个q行中的每一行表示一个操作
“C a b c”是指在Aa、Aa+1,…,Ab. -10000 ≤ c ≤ 10000
“Q a b”表示查询区间Aa,Aa+1,…Ab的和.
- 输出
您需要按顺序回答所有Q命令, 一行一个答案。
- 提示
总和可能超过32位整数的范围。
仍仍仍然是一道板子题
我天…这两天就在疯狂练打模板
一道区间修改的题
Time | 2891ms |
---|---|
Memory | 9248kB |
Length | 2257 |
Lang | G++ |
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll maxa = 0x3f3f3f;
int num[100005];
struct Btree {
ll l, r, sum, lazy;
}tree[400005];
inline void pushup(int k) {
tree[k].sum = max(tree[k << 1].sum , tree[k << 1 | 1].sum);
}
void pushdown(int now) {
if(tree[now].l == tree[now].r) {
tree[now].lazy = 0;
return;
}
tree[now << 1].sum += (tree[now << 1].r - tree[now << 1].l + 1) * tree[now].lazy;
tree[now << 1 | 1].sum += (tree[now << 1 | 1].r - tree[now << 1 | 1].l + 1) * tree[now].lazy;
tree[now << 1].lazy += tree[now].lazy;
tree[now << 1 | 1].lazy += tree[now].lazy;
tree[now].lazy = 0;
}
void build(int now, int l, int r) {
tree[now].lazy = 0;
tree[now].l = l, tree[now].r = r;
if(l == r) {
tree[now].sum = num[l];
return;
}
int mid = (l + r) >> 1;
build(now << 1, l, mid);
build(now << 1 | 1, mid + 1, r);
pushup(now);
}
void updateSeg(int now, int l, int r, int x){
if (tree[now].l == l && tree[now].r == r) {
tree[now].sum += 1ll * (tree[now].r - tree[now].l + 1) * x;
tree[now].lazy += x;
return;
}
int mid = (tree[now].l + tree[now].r) >> 1;
if(tree[now].lazy) pushdown(now);
if(r <= mid) updateSeg(now << 1, l, r, x);
else if(l > mid) updateSeg(now << 1 | 1, l, r, x);
else{
updateSeg(now << 1, l, mid, x);
updateSeg(now << 1 | 1, mid + 1, r, x);
}
pushup(now);
}
ll query(int now, int l, int r){
if(l == tree[now].l && r == tree[now].r) return tree[now].sum;
if(tree[now].lazy) pushdown(now);
int mid = (tree[now].r + tree[now].l) >> 1;
if(r <= mid) return query(now << 1, l, r);
else if(l > mid) return query(now << 1 | 1, l, r);
else return query(now << 1, l, mid) + query(now << 1 | 1, mid + 1, r);
}
int main(int argc, char const *argv[]) {
int n, q, i, j, k;
char s;
while (~scanf("%d %d", &n, &q)) {
for (int h = 1; h <= n; h++) {
scanf("%d", &num[h]);
}
build(1, 1, n);
for (int g = 0; g < q; g++) {
scanf(" %c", &s);
if(s == 'Q') {
scanf("%d %d", &i, &j);
printf("%lld\n", query(1, i, j));
}
if(s == 'C') {
scanf("%d %d %d", &i, &j, &k);
updateSeg(1, i, j, k);
}
}
}
return 0;
}