记录编号 |
393186 |
评测结果 |
AAAAAAAAAA |
题目名称 |
数列操作C |
最终得分 |
100 |
用户昵称 |
农场主 |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.725 s |
提交时间 |
2017-04-10 09:34:21 |
内存使用 |
0.30 MiB |
显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
class poi{
public:
ll l,r,sum,add,lc,rc;
};
class seg{
public:
vector<poi> s;
void build(int x){
if (s[x].lc) return;
ll l=s[x].l,r=s[x].r,mid=(l+r)>>1;
s.push_back((poi){l,mid,0,0,0,0});
s[x].lc=s.size()-1;
s.push_back((poi){mid+1,r,0,0,0,0});
s[x].rc=s.size()-1;
}
void pushdown(int x){
build(x);
ll lc=s[x].lc,rc=s[x].rc;
s[lc].add+=s[x].add;
s[lc].sum+=(s[lc].r-s[lc].l+1)*s[x].add;
s[rc].add+=s[x].add;
s[rc].sum+=(s[rc].r-s[rc].l+1)*s[x].add;
s[x].add=0;
}
void updata(int x){
s[x].sum=s[s[x].lc].sum+s[s[x].rc].sum;
}
void add(int x,int l,int r,ll t){
if (l<=s[x].l&&s[x].r<=r){
s[x].sum+=t*(s[x].r-s[x].l+1);
s[x].add+=t;
return;
}
else{
pushdown(x);
if (l<=s[s[x].lc].r){
add(s[x].lc,l,r,t);
}
if (r>=s[s[x].rc].l){
add(s[x].rc,l,r,t);
}
updata(x);
}
}
ll sum(int x,int l,int r){
if (l<=s[x].l&&s[x].r<=r){
return s[x].sum;
}
else{
pushdown(x);
ll tmp=0;
if (l<=s[s[x].lc].r){
tmp+=sum(s[x].lc,l,r);
}
if (r>=s[s[x].rc].l){
tmp+=sum(s[x].rc,l,r);
}
return tmp;
}
}
}tree;
int n;
int m;
ll l,r,t;
ll a;
void read(){
scanf("%d",&n);
tree.s.push_back((poi){1,n,0,0,0,0});
for (int i=1;i<=n;i++){
scanf("%lld",&a);
tree.add(0,i,i,a);
}
scanf("%d",&m);
char ch[5];
for (int i=1;i<=m;i++){
scanf("%s",ch);
if (ch[0]=='S'){
scanf("%lld%lld",&l,&r);
printf("%lld\n",tree.sum(0,l,r));
}
if (ch[0]=='A'){
scanf("%lld%lld%lld",&l,&r,&t);
tree.add(0,l,r,t);
}
}
}
int main(){
freopen("shuliec.in","r",stdin);
freopen("shuliec.out","w",stdout);
read();
return 0;
}