#include<bits/stdc++.h>
#define task "B"
using namespace std;
#define ll long long
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int,int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define pf push_front
#define For(i,a,b) for(int i=a; i<=b; ++i)
#define rof(i,b,a) for(int i=b; i>=a; --i)
const int N=1e5+100;
int n,q;
ll a[N+10],b[N+10],f1[N+10],f2[N+10];
void up(int id, ll x, ll f[])
{
for(++id; id<N; id+= id & -id) f[id]+=x;
}
ll kq(int l, int r, ll f[]){
ll res=0;
for(++r;r;r-= r & -r){
res+=f[r];
}
for(; l; l-= l & -l) res-=f[l];
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>q;
For(i,1,n)
{
cin>>a[i];
}
For(i,1,n) up(i,a[i],f1);
int l=n, r=0;
while(q--)
{
char c;
cin>>c;
if(c=='A'){
ll x;
cin>>x;
a[++l]=x;
up(l,x,f1);
}
else if(c=='D'){
up(l,-a[l],f1);
--l;
}
else if(c=='L'){
up(l,-a[l],f1);
b[++r]=a[l];
--l;
up(r,b[r],f2);
}
else if(c=='R'){
up(r,-b[r],f2);
a[++l]=b[r];
--r;
up(l,a[l],f1);
}
else{
int L, R;
cin>>L>>R;
if(R<=l){
cout<<kq(L,R,f1)<<'\n';
}
else{
cout<<kq(L,l,f1)+kq(l+r-R+1,r,f2)<<'\n';
}
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSB0YXNrICJCIgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGxkIGxvbmcgZG91YmxlCiNkZWZpbmUgdmkgdmVjdG9yPGludD4KI2RlZmluZSB2bCB2ZWN0b3I8bGw+CiNkZWZpbmUgcGlpIHBhaXI8aW50LGludD4KI2RlZmluZSBwbGwgcGFpcjxsbCwgbGw+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBlcCBlbXBsYWNlX2JhY2sKI2RlZmluZSBwZiBwdXNoX2Zyb250CiNkZWZpbmUgRm9yKGksYSxiKSBmb3IoaW50IGk9YTsgaTw9YjsgKytpKQojZGVmaW5lIHJvZihpLGIsYSkgZm9yKGludCBpPWI7IGk+PWE7IC0taSkKY29uc3QgaW50IE49MWU1KzEwMDsKaW50IG4scTsKbGwgYVtOKzEwXSxiW04rMTBdLGYxW04rMTBdLGYyW04rMTBdOwoKdm9pZCB1cChpbnQgaWQsIGxsIHgsIGxsIGZbXSkKewogICAgZm9yKCsraWQ7IGlkPE47IGlkKz0gaWQgJiAtaWQpIGZbaWRdKz14Owp9CmxsIGtxKGludCBsLCBpbnQgciwgbGwgZltdKXsKICAgIGxsIHJlcz0wOwogICAgZm9yKCsrcjtyO3ItPSByICYgLXIpewogICAgICAgIHJlcys9ZltyXTsKICAgIH0KICAgIGZvcig7IGw7IGwtPSBsICYgLWwpIHJlcy09ZltsXTsKICAgIHJldHVybiByZXM7Cn0KCmludCBtYWluKCl7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CiAgICBjaW4+Pm4+PnE7CiAgICBGb3IoaSwxLG4pCiAgICB7CiAgICAgICAgY2luPj5hW2ldOwogICAgfQogICAgRm9yKGksMSxuKSB1cChpLGFbaV0sZjEpOwogICAgaW50IGw9biwgcj0wOwogICAgd2hpbGUocS0tKQogICAgewogICAgICAgIGNoYXIgYzsKICAgICAgICBjaW4+PmM7CiAgICAgICAgaWYoYz09J0EnKXsKICAgICAgICAgICAgbGwgeDsKICAgICAgICAgICAgY2luPj54OwogICAgICAgICAgICBhWysrbF09eDsKICAgICAgICAgICAgdXAobCx4LGYxKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihjPT0nRCcpewogICAgICAgICAgICB1cChsLC1hW2xdLGYxKTsKICAgICAgICAgICAgLS1sOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKGM9PSdMJyl7CiAgICAgICAgICAgIHVwKGwsLWFbbF0sZjEpOwogICAgICAgICAgICBiWysrcl09YVtsXTsKICAgICAgICAgICAgLS1sOwogICAgICAgICAgICB1cChyLGJbcl0sZjIpOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKGM9PSdSJyl7CiAgICAgICAgICAgIHVwKHIsLWJbcl0sZjIpOwogICAgICAgICAgICBhWysrbF09YltyXTsKICAgICAgICAgICAgLS1yOwogICAgICAgICAgICB1cChsLGFbbF0sZjEpOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBpbnQgTCwgUjsKICAgICAgICAgICAgY2luPj5MPj5SOwogICAgICAgICAgICBpZihSPD1sKXsKICAgICAgICAgICAgICAgIGNvdXQ8PGtxKEwsUixmMSk8PCdcbic7CiAgICAgICAgICAgIH0KICAgICAgICAgICAKICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgIGNvdXQ8PGtxKEwsbCxmMSkra3EobCtyLVIrMSxyLGYyKTw8J1xuJzsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==