#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll LINF = 1e18; template ostream& operator << (ostream& out,const pair& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template ostream& operator << (ostream& out,const vector V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template ostream& operator << (ostream& out,const vector > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template ostream& operator << (ostream& out,const map mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ template struct BIT0 { // 0-index int N; int nn; vector data; //動的はmapdata BIT0(int n) { N = n + 1; data = vector(n + 1, 0); nn = 1; while (nn * 2 <= N)nn *= 2; } void add(ll i, type w) { // a[i] += w i++; for (int x = i; x < N; x += x & -x) { data[x] += w; } } type sum(ll i) { // iまでの和 [0,i) if(i < 0) return 0; type ret = 0; for (int x = i; x > 0; x -= x & -x) { ret += data[x]; } return ret; } // [l, r) type sum(ll l, ll r) { return sum(r) - sum(l); } }; int N,Q; pll updateL(ll t,ll y,ll z,vector>& bit){ t %= 2*N; y += t; ll ind = -1; if(y < N){ ind = 0; }else if(y < 2*N){ y -= N; y = (N-1) - y; ind = 1; }else{ y -= 2*N; ind = 0; } bit[ind].add(y,z); return pll(ind,y); } pll updateR(ll t,ll y,ll z,vector>& bit){ t %= 2*N; y -= t; ll ind = -1; if(y >= 0){ ind = 1; }else if(y >= -N){ y += N; y = (N-1) - y; ind = 0; }else{ y += 2*N; ind = 1; } bit[ind].add(y,z); return pll(ind,y); } ll query(ll t,ll y,ll z,vector>& bit){ ll ret = 0; z--; { pll p1 = updateL(t,y,0,bit); pll p2 = updateL(t,z,0,bit); if(p1.first == p2.first){ if(p1.first == 0){ ret += bit[p1.first].sum(p1.second,p2.second+1); }else{ ret += bit[p1.first].sum(p2.second,p1.second+1); } }else{ if(p1.first == 0){ ret += bit[p1.first].sum(p1.second,N) + bit[p2.first].sum(p2.second,N); }else{ ret += bit[p1.first].sum(p1.second+1) + bit[p2.first].sum(p2.second+1); } } } { pll p1 = updateR(t,y,0,bit); pll p2 = updateR(t,z,0,bit); if(p1.first == p2.first){ if(p1.first == 0){ ret += bit[p1.first].sum(p2.second,p1.second+1); }else{ ret += bit[p1.first].sum(p1.second,p2.second+1); } }else{ if(p1.first == 0){ ret += bit[p1.first].sum(p1.second+1) + bit[p2.first].sum(p2.second+1); }else{ ret += bit[p1.first].sum(p1.second,N) + bit[p2.first].sum(p2.second,N); } } } return ret; } void solve(){ cin >> N >> Q; vector> bit; bit.push_back(BIT0(N)); bit.push_back(BIT0(N)); while(Q--){ char x; ll t,y,z; cin >> x >> t >> y >> z; if(x == 'L'){ updateL(t,y,z,bit); }else if(x == 'R'){ updateR(t,y,z,bit); }else{ cout << query(t,y,z,bit) << endl; } } } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); solve(); return 0; }