记录编号 587664 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 数列操作C 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.144 s
提交时间 2024-04-12 19:59:34 内存使用 6.50 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+10,M = 330;
const ll inf = 1e17;
int n,m;
ll a[N],sum[M],la[M];
int L[N],R[N],bl[N],B,t; 
void prework(){
    B = sqrt(n);t = (n-1)/B+1;
    for(int i = 1;i <= t;i++)L[i] = (i-1)*B+1,R[i] = min(i*B,n);
    for(int i = 1;i <= n;i++)bl[i] = (i-1)/B+1,sum[bl[i]] += a[i];
}
void modify(int l,int r,ll x){
    int p = bl[l],q = bl[r];
    if(p == q){
        for(int i = l;i <= r;i++)a[i] += x,sum[p] += x;
        return; 
    }
    for(int i = l;i <= R[p];i++)a[i] += x,sum[p] += x;
    for(int i = L[q];i <= r;i++)a[i] += x,sum[q] += x;
    for(int i = p+1;i <= q-1;i++)la[i] += x; 
}
ll ask(int l,int r){
    int p = bl[l],q = bl[r];
    if(p == q){
        ll ans = 0;
        for(int i = l;i <= r;i++)ans += a[i] + la[p];
        return ans;
    }
    ll ans = 0;
    for(int i = l;i <= R[p];i++)ans += a[i] + la[p];
    for(int i = L[q];i <= r;i++)ans += a[i] + la[q];
    for(int i = p+1;i <= q-1;i++)ans += la[i] * (R[i] - L[i] + 1) + sum[i];
    return ans;
}
int main(){
    freopen("shuliec.in","r",stdin);
    freopen("shuliec.out","w",stdout);
    scanf("%d",&n);
    for(int i = 1;i <= n;i++)scanf("%lld",&a[i]);
    prework();
    scanf("%d",&m);
    for(int i = 1;i <= m;i++){
        int l,r;ll x;char op[10];
        scanf("%s%d%d",op,&l,&r);
        if(op[0] == 'A'){
            scanf("%lld",&x);
            modify(l,r,x);
        }
        else printf("%lld\n",ask(l,r));
    }
    

	return 0;

}