ʕ·͡ˑ·ཻ ʕ•̫͡• ʔ•̫͡•ཻʕ•̫͡•ʔ•͓͡•ʔ

题目 - A Simple Problem with Integers

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

1
2
3
4
5
6
7
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

1
2
3
4
4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

题目大意:

​ 给出N个数,进行Q个操作,1<=N,Q<=100000。有两种操作:

​ “C a b c”,对区间[a,b]的每个数字加c

​ ”Q a b c”,查询区间[a,b]的数字和

解题:

区间修改模板,TLE了一次。

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<string.h>
using namespace std;

const int MAXN = 1e5 + 10;

long long sum[MAXN << 2], add[MAXN << 2];

void push_down(int u, int m) {
if(add[u]) {
add[u << 1] += add[u];
add[u << 1 | 1] += add[u];
sum[u << 1] += (m - (m >> 1)) * add[u];
sum[u << 1 | 1] += (m >> 1) * add[u];
add[u] = 0;
}
}

void BuildTree(int l, int r, int u) {
if(l == r) {
cin>>sum[u];
return;
}
int mid = (l + r) >> 1;
BuildTree(l, mid, u << 1);
BuildTree(mid + 1, r, u << 1 | 1);
sum[u] = sum[u << 1] + sum[u << 1 | 1];
}

void Update(int a, int b, long long int c, int l, int r, int u) {
if(a <= l && b >= r) {
sum[u] += (r - l + 1) * c;
add[u] += c;
return;
}
push_down(u, r - l + 1);
int mid = (r + l) >> 1;
if(a <= mid) Update(a, b, c, l, mid, u << 1);
if(b > mid) Update(a, b, c, mid + 1, r, u << 1 | 1);
sum[u] = sum[u << 1] + sum[u << 1 | 1];
}

long long Query(int a, int b, int l, int r, int u) {
if(a <= l && b >= r) return sum[u];
push_down(u, r - l + 1);
int mid = (l + r) >> 1;
long long ans = 0;
if(a <= mid) ans += Query(a, b, l, mid, u << 1);
if(b > mid) ans += Query(a, b, mid + 1, r, u << 1 | 1);
return ans;
}

int main(){
int n,m;
//之前没有加这句话所以TLE了
while(~scanf("%d %d",&n,&m)){
memset(add, 0, sizeof(add));
memset(sum, 0, sizeof(sum));
BuildTree(1,n,1);
while(m--){
char str[2];
int a,b;
long long int c;
cin>>str;
if(str[0] == 'Q') {
cin>>a>>b;
cout<<Query(a,b,1,n,1)<<endl;
}
else if(str[0] == 'C') {
cin>>a>>b>>c;
Update(a,b,c,1,n,1);
}
}
}
return 0;
}