记录编号 |
159933 |
评测结果 |
WWWWWWWWWW |
题目名称 |
数列操作C |
最终得分 |
0 |
用户昵称 |
slyrabbit |
是否通过 |
未通过 |
代码语言 |
C++ |
运行时间 |
1.263 s |
提交时间 |
2015-04-23 12:14:12 |
内存使用 |
15.36 MiB |
显示代码纯文本
#include<iostream>
#include<cstdio>
using namespace std;
class student
{
public:
long long x;
long long wait;
int lchild;
int rchild;
int l;
int r;
}tree[400005];
int n,m,p=1;
//***********************************不要浮躁***************************************//
void build(int l,int r)
{
int now=p;
int mid=(l+r)>>1;
tree[now].l=l;tree[now].r=r;tree[now].x=0;tree[now].wait=0;
if(l==r)
return;
tree[now].lchild=++p;build(l,mid);
tree[now].rchild=++p;build(mid+1,r);
}
void updata(int u,int s,int t,int delta)
{
int l=tree[u].l,r=tree[u].r;
if(l>t||r<s) return;
if(s<=l&&r<=t)
{
tree[u].x+=(r-l+1)*delta;
tree[u].wait+=delta;
return;
}
int mid=(l+r)>>1;
tree[tree[u].lchild].wait+=tree[u].wait;
tree[tree[u].lchild].x+=tree[u].wait*(mid-l+1);
tree[tree[u].rchild].wait+=tree[u].wait;
tree[tree[u].rchild].x+=tree[u].wait*(r-mid);
tree[u].wait=0;
updata(tree[u].lchild,s,t,delta);
updata(tree[u].rchild,s,t,delta);
tree[u].x=tree[tree[u].lchild].x+tree[tree[u].rchild].x;
}
long long sum(int u,int s,int t)
{
int l=tree[u].l,r=tree[u].r;
if(l>t||r<s) return 0;
if(s<=l&&r<=t) return tree[u].x;
int mid=(tree[u].l+tree[u].r)>>1;
tree[tree[u].lchild].wait+=tree[u].wait;
tree[tree[u].lchild].x+=tree[u].wait*(mid-l+1);
tree[tree[u].rchild].wait+=tree[u].wait;
tree[tree[u].rchild].x+=tree[u].wait*(r-mid);
tree[u].wait=0;
long long s1=sum(tree[u].lchild,s,mid);
long long s2=sum(tree[u].rchild,mid+1,t);
tree[u].x=tree[tree[u].lchild].x+tree[tree[u].rchild].x;
return s1+s2;
}
void init()
{
int temp;
cin>>n;
build(1,n);
for(int i=1;i<=n;i++)
{
cin>>temp;
updata(1,i,i,temp);
}
}
void work()
{
cin>>m;
string a;
int s,t,x;
while(m--)
{
cin>>a;
if(a=="SUM")
{
cin>>s>>t;
cout<<sum(1,s,t)<<endl;
}
else
{
cin>>s>>t>>x;
updata(1,s,t,x);
}
}
}
int main()
{
freopen("shuliec.in","r",stdin);
freopen("shuliec.out","w",stdout);
init();
work();
return 0;
}