记录编号 |
364345 |
评测结果 |
AAAAAAAAAA |
题目名称 |
数列操作C |
最终得分 |
100 |
用户昵称 |
Sky_miner |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.327 s |
提交时间 |
2017-01-16 09:06:22 |
内存使用 |
6.03 MiB |
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline ll cat_max(const ll &a,const ll &b){return a>b ? a:b;}
inline ll cat_min(const ll &a,const ll &b){return a<b ? a:b;}
const ll maxn = 100010;
ll a[maxn];
struct Splay{
#define Sky (root->ch[1]->ch[0])
struct Node{
Node *ch[2],*fa;
ll w,siz,sum,lazy;
bool tag;
void update(){
siz = ch[0]->siz + ch[1]->siz + 1;
sum = ch[0]->sum + ch[1]->sum + w;
}
};
Node mem[maxn];
Node *tail,*root,*null;
Node* bc[maxn];ll top;
Splay(){
tail = mem;null = tail++;
null->ch[0] = null->ch[1] = null->fa = null;
null->siz = null->w = null->sum = null->lazy = null->tag = 0;
root = null;top = 0;
}
Node* newNode(ll key){
Node *p = top ? bc[top--] : tail++;
p->ch[0] = p->ch[1] = p->fa = null;
p->w = key;p->siz = 1;p->sum = p->lazy = 0;
p->tag = 0;return p;
}
void del(Node* &p){
bc[++top] = p;
p = null;
}
void push_down(Node *p){
if(p->tag){
p->ch[0]->tag ^= 1;
p->ch[1]->tag ^= 1;
swap(p->ch[0],p->ch[1]);
p->tag = 0;
}
if(p->lazy){
if(p->ch[0] != null){
Node *x = p->ch[0];
x->lazy += p->lazy;
x->w += p->lazy;
x->sum += p->lazy*x->siz;
}
if(p->ch[1] != null){
Node *x = p->ch[1];
x->lazy += p->lazy;
x->w += p->lazy;
x->sum += p->lazy*x->siz;
}
p->lazy = 0;
}
}
void rotate(Node* &x,ll k){
Node *y = x->ch[k^1];
x->ch[k^1] = y->ch[k];
x->ch[k^1]->fa = x;
y->ch[k] = x;
if(x->fa->ch[0] == x) x->fa->ch[0] = y;
else if(x->fa->ch[1] == x) x->fa->ch[1] = y;
y->fa = x->fa;x->fa = y;
x->update();y->update();
}
Node* find(ll k){
Node *nw = root;
push_down(nw);
while(nw->ch[0]->siz + 1 != k){
if(nw->ch[0]->siz + 1 > k) nw = nw->ch[0];
else k -= nw->ch[0]->siz + 1,nw = nw->ch[1];
push_down(nw);
}return nw;
}
void splay(Node* p,Node* gl){
while(p->fa != gl){
Node *x = p->fa,*y = x->fa;
push_down(y);push_down(x);
if(y == gl) rotate(x,x->ch[0] == p);
else{
if((y->ch[0] == x) == (x->ch[0] == p)) rotate(y,y->ch[0] == x),rotate(x,x->ch[0] == p);
else rotate(x,x->ch[0] == p),rotate(y,y->ch[0] == p);
}
}p->update();
if(gl == null) root = p;
}
Node* build(ll l,ll r){
if(l > r) return null;
ll mid = l+r >> 1;
Node *p = newNode(a[mid]);
p->ch[0] = build(l,mid-1);
p->ch[1] = build(mid+1,r);
if(p->ch[0] != null) p->ch[0]->fa = p;
if(p->ch[1] != null) p->ch[1]->fa = p;
p->update();return p;
}
void add(ll l,ll r,ll val){
splay(find(l),null);
splay(find(r+2),root);
Sky->lazy += val;
Sky->w += val;
Sky->sum += val*Sky->siz;
}
ll get_sum(ll l,ll r){
splay(find(l),null);
splay(find(r+2),root);
return Sky->sum;
}
}*zs = new Splay();
char s[10];
int main(){
freopen("shuliec.in","r",stdin);
freopen("shuliec.out","w",stdout);
//printf("null : %d\n",zs->null);
ll n;read(n);
for(ll i=2;i<=n+1;++i) read(a[i]);
a[1] = a[n+2] = 0;
zs->root = zs->build(1,n+2);
ll m;read(m);
ll l,r,x;
while(m--){
scanf("%s",s);
read(l);read(r);
if(*s == 'S') printf("%lld\n",zs->get_sum(l,r));
else{
read(x);
zs->add(l,r,x);
}
}
getchar();getchar();
return 0;
}