## solution

• TLE $5$秒
• $N \le 2\times 10^4$
• $Q \le 2\times 10^4$

## implementation

#include <iostream>
#include <vector>
#include <functional>
#include <cmath>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
typedef long long ll;
using namespace std;

template <typename T>
struct segment_tree { // on monoid
int n;
vector<T> a;
function<T (T,T)> append; // associative
T unit; // unit
segment_tree() = default;
template <typename F>
segment_tree(int a_n, T a_unit, F a_append) {
n = pow(2,ceil(log2(a_n)));
a.resize(2*n-1, a_unit);
unit = a_unit;
append = a_append;
}
void point_update(int i, T z) {
a[i+n-1] = z;
for (i = (i+n)/2; i > 0; i /= 2) {
a[i-1] = append(a[2*i-1], a[2*i]);
}
}
T range_concat(int l, int r) {
return range_concat(0, 0, n, l, r);
}
T range_concat(int i, int il, int ir, int l, int r) {
if (l <= il and ir <= r) {
return a[i];
} else if (ir <= l or r <= il) {
return unit;
} else {
return append(
range_concat(2*i+1, il, (il+ir)/2, l, r),
range_concat(2*i+2, (il+ir)/2, ir, l, r));
}
}
T point_concat(int l) {
return range_concat(l, l+1);
}
};

int main() {
int n, q; cin >> n >> q;
segment_tree<ll> l(n + q, 0, plus<ll>());
segment_tree<ll> r(n + q, 0, plus<ll>());
repeat (t,q) {
char c; int y, z; cin >> c >> y >> z;
switch (c) {
case 'L':
l.point_update(y+t, l.point_concat(y+t) + z);
break;
case 'R':
r.point_update((n-y-1)+t, r.point_concat((n-y-1)+t) + z);
break;
case 'C':
cout << l.range_concat(y+t, z+t) + r.range_concat((n-z)+t, (n-y)+t) << endl;
break;
}
r.point_update(t+n, l.point_concat(t));
l.point_update(t+n, r.point_concat(t));
}
return 0;
}