#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
const double eps=1e-11;
typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
typedef long double ld;
#define swap(a,b,tmp) { tmp=b; b=a; a=tmp; }
#define foi(i,s,e) for(i=s;i<=e;i++)
#define fod(i,s,e) for(i=s;i=>e;i--)
#define abs(a)(a<0?-(a):a)
#define iin(n) scanf("%d",&n)
#define iout(n) printf("%d",n)
#define sin(s) scanf("%s",&s)
#define sout(s) printf("%s",s)
#define MAX 1000000
typedef int T; /* type of item to be stored */
#define compLT(a,b) (a < b)
#define compEQ(a,b) (a == b)
/* Red-Black tree description */
typedef enum { BLACK, RED } nodeColor;
typedef struct Node_ {
struct Node_ *left; /* left child */
struct Node_ *right; /* right child */
struct Node_ *parent; /* parent */
nodeColor color; /* node color (BLACK, RED) */
T data; /* data stored in node */
} Node;
#define NIL &sentinel /* all leafs are sentinels */
Node sentinel = { NIL, NIL, 0, BLACK, 0};
Node *root = NIL; /* root of Red-Black tree */
void rotateLeft(Node *x) {
/**************************
* rotate node x to left *
**************************/
Node *y = x->right;
/* establish x->right link */
x->right = y->left;
if (y->left != NIL) y->left->parent = x;
/* establish y->parent link */
if (y != NIL) y->parent = x->parent;
if (x->parent) {
if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
} else {
root = y;
}
/* link x and y */
y->left = x;
if (x != NIL) x->parent = y;
}
void rotateRight(Node *x) {
/****************************
* rotate node x to right *
****************************/
Node *y = x->left;
/* establish x->left link */
x->left = y->right;
if (y->right != NIL) y->right->parent = x;
/* establish y->parent link */
if (y != NIL) y->parent = x->parent;
if (x->parent) {
if (x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
} else {
root = y;
}
/* link x and y */
y->right = x;
if (x != NIL) x->parent = y;
}
void insertFixup(Node *x) {
/*************************************
* maintain Red-Black tree balance *
* after inserting node x *
*************************************/
/* check Red-Black properties */
while (x != root && x->parent->color == RED) {
/* we have a violation */
if (x->parent == x->parent->parent->left) {
Node *y = x->parent->parent->right;
if (y->color == RED) {
/* uncle is RED */
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
} else {
/* uncle is BLACK */
if (x == x->parent->right) {
/* make x a left child */
x = x->parent;
rotateLeft(x);
}
/* recolor and rotate */
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateRight(x->parent->parent);
}
} else {
/* mirror image of above code */
Node *y = x->parent->parent->left;
if (y->color == RED) {
/* uncle is RED */
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
} else {
/* uncle is BLACK */
if (x == x->parent->left) {
x = x->parent;
rotateRight(x);
}
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateLeft(x->parent->parent);
}
}
}
root->color = BLACK;
}
Node *insertNode(T data) {
Node *current, *parent, *x;
/***********************************************
* allocate node for data and insert in tree *
***********************************************/
/* find where node belongs */
current = root;
parent = 0;
while (current != NIL) {
if (compEQ(data, current->data)) return (current);
parent = current;
current = compLT(data, current->data) ?
current->left : current->right;
}
/* setup new node */
if ((x
= malloc (sizeof(*x
))) == 0) { printf ("insufficient memory (insertNode)\n"); }
x->data = data;
x->parent = parent;
x->left = NIL;
x->right = NIL;
x->color = RED;
/* insert node in tree */
if(parent) {
if(compLT(data, parent->data))
parent->left = x;
else
parent->right = x;
} else {
root = x;
}
insertFixup(x);
return(x);
}
void scan_int(int *n){
int tmp=0;
char ch;
int len
=fread(&ch
,sizeof(char),1,stdin
); while((ch
< '0' || ch
> '9') && len
>0 ) len
=fread(&ch
,sizeof(char),1, stdin
); while( ch >= '0' && ch <= '9' && len>0 ){
tmp = (tmp<<3)+(tmp<<1) + ch-'0';
len
=fread(&ch
,sizeof(char),1, stdin
); }
*n=tmp;
}
lli modifysum(Node* t,lli *cng,int c,int d)
{
lli mod=0;
if(t==NIL) return 0;
if(t->data>=c&&t->data<=d){
mod+=cng[t->data];
if(t->data!=d) mod+=modifysum(t->right,cng,c,d);
if(t->data!=c) mod+=modifysum(t->left,cng,c,d);
}else if(t->data<c){
mod+=modifysum(t->right,cng,c,d);
}else{
mod+=modifysum(t->left,cng,c,d);
}
return mod;
}
int main()
{
int n,q;
lli *a;
a
=(lli
*)malloc(MAX
*sizeof(lli
)); lli *cng;
cng
=(lli
*)malloc(MAX
*sizeof(lli
)); int *flg;
flg
=(int*)malloc(MAX
*sizeof(int)); int i=0;
char ch;
int c,d,tmp2;
flg[i]=0;
cng[i]=0;
a[i]=tmp2; i++;
while(i<n){
flg[i]=0;
cng[i]=0;
scan_int(&tmp2);
a[i]=tmp2+a[i-1];
i++;
}
lli sum,tp;
while(q--){
ch='@';
sum=0;
while(ch
!='S'&&ch
!='T'&&ch
!='G') fread(&ch
,sizeof(char),1,stdin
); scan_int(&c); scan_int(&d);
if(ch=='S'){
if(c==0) sum=a[d];
else sum=a[d]-a[c-1];
tp=modifysum(root,cng,c,d);
sum+=tp;
}else if(ch=='G'){
cng[c]+=d;
if(flg[c]!=1){ insertNode(c); flg[c]=1; }
}else{
cng[c]-=d;
if(flg[c]!=1){ insertNode(c); flg[c]=1; }
}
}
return 0;
}