Basic

Ono Node Interval Query

Luogu P3374

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
#include <iostream>
#define ll long long
#define fast \
std::ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
const int maxn = 500010;
int n, m;
int bit[maxn];

void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}

ll sum(int i) {
ll ans = 0;
while (i) {
ans += bit[i];
i -= i & -i;
}
return ans;
}

int num;

int main() {
fast
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> num;
add(i, num);
}
int opt, x, y;
while (m--) {
cin >> opt >> x >> y;
if (opt == 1) add(x, y);
else
cout << sum(y) - sum(x - 1) << "\n";
}
}

Interval Update One Node Query

Luogu P3368

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
#include <iostream>
#define fast \
std::ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long
using namespace std;
const int maxn = 5010000;
int n, m;
int bit[maxn], a[maxn], b[maxn];

void update(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}

ll sum(int i) {
ll ans = 0;
while (i > 0) {
ans += bit[i];
i -= i & -i;
}
return ans;
}

int main() {
fast
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
update(i, a[i] - a[i - 1]);
}
int opt, x, y, k;
while (m--) {
cin >> opt;
if (opt == 1) {
cin >> x >> y >> k;
update(x, k);
update(y + 1, -k);
} else {
cin >> x;
cout << sum(x) << "\n";
}
}
return 0;
}

Interval Update & Query

Luogu P3372

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
#include <iostream>
#define ll long long
#define fast std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
using namespace std;
const int maxn = 100010;
int a[maxn];
ll bit[2][maxn], sum[maxn];
int n, m;

void update(int k, int i, int x) {
while (i <= n) {
bit[k][i] += x;
i += i & -i;
}
}

ll query(int k, int i) {
long long ans = 0;
while (i > 0) {
ans += bit[k][i];
i -= i & -i;
}
return ans;
}

int main() {
fast
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
int opt, x, y, k;
while (m--) {
cin >> opt >> x >> y;
if (opt == 1){
cin >> k;
update(0,x,k);
update(0,y+1,-k);
update(1,x,x*k);
update(1,y+1,-(y+1)*k);
}
else cout << (ll)sum[y] +(y+1)*query(0,y)-query(1,y)-(ll)(sum[x-1]+x*query(0,x-1)-query(1,x-1)) << "\n";
}
}