0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-30 19:56:28 +00:00
OI-codes/Luogu/P2068/P2068.cpp

73 lines
1.3 KiB
C++
Raw Normal View History

2020-12-02 04:46:20 +00:00
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r;
long long s;
node() {
l = r = 0;
s = 0;
}
2022-05-20 01:23:27 +00:00
2020-12-02 04:46:20 +00:00
node(int _l, int _r) {
l = _l;
r = _r;
s = 0;
}
} tr[1000005 << 2];
2022-05-20 01:23:27 +00:00
2020-12-02 04:46:20 +00:00
long long n, w, a, b;
char op;
void pushup(int u) {
tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s;
}
void build(int u, int l, int r) {
tr[u] = node(l, r);
if (l == r) return;
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
void modify(int u, int x, int d) {
if (tr[u].l == tr[u].r) {
tr[u].s += d;
return;
}
int mid = tr[u].l + tr[u].r >> 1;
2021-11-19 09:01:13 +00:00
if (x <= mid)
modify(u << 1, x, d);
else
modify(u << 1 | 1, x, d);
2020-12-02 04:46:20 +00:00
pushup(u);
}
long long query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) {
return tr[u].s;
}
int mid = tr[u].l + tr[u].r >> 1;
long long s = 0;
if (l <= mid) s += query(u << 1, l, r);
if (r > mid) s += query(u << 1 | 1, l, r);
return s;
}
int main() {
cin >> n >> w;
build(1, 1, n);
while (w--) {
cin >> op >> a >> b;
if (op == 'x') {
modify(1, a, b);
2021-11-19 09:01:13 +00:00
} else if (op == 'y') {
2020-12-02 04:46:20 +00:00
cout << query(1, a, b) << endl;
}
}
return 0;
}