#include using namespace std; namespace { typedef double real; typedef long long ll; template ostream& operator<<(ostream& os, const vector& vs) { if (vs.empty()) return os << "[]"; os << "[" << vs[0]; for (int i = 1; i < vs.size(); i++) os << " " << vs[i]; return os << "]"; } template istream& operator>>(istream& is, vector& vs) { for (auto it = vs.begin(); it != vs.end(); it++) is >> *it; return is; } template struct Treap { mt19937 mt; Treap() : root(NULL) { random_device rnd; mt.seed(rnd()); } struct Node { V value; Node *left, *right; int priority; int size; V sum; /**/ Node(V value, int priority) : value(value), priority(priority), size(1), sum(value)/**/ { left = NULL; right = NULL; } }; Node *root; int size(Node *t) { if (t == NULL) return 0; return t->size; } V sum(Node *t) { /**/ if (t == NULL) return 0; V l = (t->left == NULL ? 0 : t->left->sum); V r = (t->right == NULL ? 0 : t->right->sum); return l + r + t->value; } Node* update(Node *v) { v->size = size(v->left) + size(v->right) + 1; v->sum = sum(v->left) + sum(v->right) + v->value; /**/ return v; } Node* merge(Node *l, Node *r) { if (l == NULL) return r; if (r == NULL) return l; assert(l != NULL && r != NULL); if (l->priority > r->priority) { l->right = merge(l->right, r); return update(l); } else { r->left = merge(l, r->left); return update(r); } } pair split(Node *t, int k) { // [0, k) と [k, n)に分割 if (t == NULL) return make_pair((Node*)NULL, (Node*)NULL); if (k <= size(t->left)) { auto s = split(t->left, k); t->left = s.second; return make_pair(s.first, update(t)); } else { auto s = split(t->right, k - size(t->left) - 1); // この-1は今見てる部分木のroot(つまりt)の分 t->right = s.first; return make_pair(update(t), s.second); } } pair split(int k) { return split(root, k); } Node* insert(Node *t, int k, V value) { auto x = split(t, k); auto y = split(x.second, 1); return merge(x.first, merge(new Node(value, mt()), y.second)); } Node* insert(int k, V value) { return root = insert(root, k, value); } Node* erase(Node *t, int k) { auto x = split(t, k); auto y = split(x.second, 1); return merge(x.first, y.second); } Node* erase(int k) { return root = erase(root, k); } Node* find(Node *t, int k) { auto x = split(t, k); auto y = split(x.second, 1); Node *ret = y.first; merge(x.first, merge(y.first, y.second)); return ret; } Node* find(int k) { return find(root, k); } }; void solve() { int N, Q; cin >> N >> Q; Treap l, r; for (int i = 0; i < N; i++) { l.insert(i, 0); r.insert(i, 0); } for (int q = 0; q < Q; q++) { string x; int y, z; cin >> x >> y >> z; //cout << x << " " << y << " " << z << endl; auto ls = l.split(1); auto rs = r.split(N - 1); l.root = l.merge(ls.second, rs.second); r.root = r.merge(ls.first, rs.first); //cout << "L:"; for (int i = 0; i < N; i++) cout << " " << l.find(i)->value; cout << endl; //cout << "R:"; for (int i = 0; i < N; i++) cout << " " << r.find(i)->value; cout << endl; if (x == "L") { auto v = l.find(y); if (v == NULL) l.insert(y, z); else l.insert(y, v->value + z); } else if (x == "R") { auto v = r.find(y); if (v == NULL) r.insert(y, z); else r.insert(y, v->value + z); } else { auto ls = l.split(y); auto lt = l.split(ls.second, z - y); auto rs = r.split(y); auto rt = r.split(rs.second, z - y); cout << l.sum(lt.first) + r.sum(rt.first) << endl; l.merge(ls.first, l.merge(lt.first, lt.second)); r.merge(rs.first, l.merge(rt.first, rt.second)); } } } } int main() { solve(); return 0; }