# BZOJ3786: 星系探索 Splay+DFS序

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std;
#define N 200005
#define ll long long
#define ls ch[rt][0]
#define rs ch[rt][1]
#define get(rt) (ch[f[rt]][0]!=rt)
#define PushUp(rt) num[rt]=num[ch[rt][0]]+num[ch[rt][1]]+flag[rt],sum[rt]=sum[ch[rt][0]]+sum[ch[rt][1]]+val[rt]*flag[rt],siz[rt]=siz[ch[rt][0]]+siz[ch[rt][1]]+1
char buf[100000],*p1,*p2;
__attribute__((optimize("-O3")))int rd() {
register int x=0;register char ch=nc();
while(ch<'0'||ch>'9') ch=nc();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=nc();
return x;
}
struct node{int to,next;}e[N];
void dfs(int x)
{
idx[++tims]=x;p[x]=tims;
idx[++tims]=x+n;p[x+n]=tims;
}
void PushDown(int rt)
{
{
}
}
void rotate(int rt)
{
int x=f[rt],y=f[x],k=get(rt);
if(x!=rot)ch[y][ch[y][0]!=x]=rt;else rot=rt;
ch[x][k]=ch[rt][!k];f[ch[x][k]]=x;
ch[rt][!k]=x;f[x]=rt;f[rt]=y;
PushUp(x);PushUp(rt);
}
void Splay(int rt,int y)
{
register int fa;
for(;(fa=f[rt])!=y;rotate(rt))
if(f[fa]!=y)
rotate((get(fa)==get(rt))?fa:rt);
}
void build(int fa,int l,int r)
{
if(l>r)return;int m=(l+r)>>1;
ch[fa][m>fa]=m;f[m]=fa;siz[m]=1;//printf("%d %d %d\n",idx[m],m,val[m]);
build(m,l,m-1);build(m,m+1,r);PushUp(m);
}
int query_k(int rt)
{
int ret=0;
while(rt)
{
// PushDown(rt);
// printf("%d %d %d\n",x,idx[rt],ret);
if(rt==rot)ret+=siz[ls]+1;
else if(get(rt))ret+=siz[ls]+1;
else ret-=siz[rs]+1;
rt=f[rt];
}
return ret;
}
int find(int x)
{
register int rt=rot;
while(1)
{
PushDown(rt);
if(x<=siz[ls])rt=ls;
else
{
x-=siz[ls]+1;
if(!x)return rt;
rt=rs;
}
}
}
void cut(int x)
{
int rt=find(query_k(p[x+n])+1);x=find(query_k(p[x])-1);
Splay(x,0);Splay(rt,rot);tmp=ls;
ls=f[ls]=0;PushUp(rt);PushUp(x);
}
{
int rt=find(query_k(p[x])+1);x=find(query_k(p[x]));
Splay(x,0);Splay(rt,rot);
// printf("%d %d\n",x,rt);
ls=tmp;f[tmp]=rt;PushUp(rt);PushUp(x);
}
void Update(int x,int c)
{
int rt=find(query_k(p[x+n])+1);x=find(query_k(p[x])-1);
Splay(x,0);Splay(rt,rot);
}
int query(int x)
{
int rt=find(query_k(p[x])+1);x=1;
Splay(x,0);Splay(rt,rot);
printf("%lld\n",sum[ls]);
}char s[2];
int main()
{
tims=1;dfs(1);idx[++tims]=tims;idx[1]=0;
for(int i=1;i<=n;i++)a[i]=rd();
for(int i=2;i<=n*2+1;i++)
{
int x=idx[i];
if(x<=n)val[i]=a[x],flag[i]=1;
else val[i]=a[x-n],flag[i]=-1;
}
build(0,1,2*n+2);rot=n+1;int Q=rd();
while(Q--)
{
char op=nc();
while(op!='C'&&op!='Q'&&op!='F')op=nc();
int x=rd();
if(op=='Q')query(x);
else if(op=='C')
{
cut(x);