狡猾的分块(板子很好吃(吧?))

· · 个人记录

#include <iostream>
#include <cmath>
#define int long long
using namespace std;
const int N=3e5+5;
int n,a[N],l[N],r[N],pos[N],lazy[N],cnt;
void build(){
    int k=sqrt(n);
    while(1){
        cnt++;
        l[cnt]=(cnt-1)*k+1;
        r[cnt]=k*cnt;
        if(r[cnt]>=n){
            r[cnt]=n;
            break;
        }
    }
    for (int i=1;i<=cnt;i++){
        for (int j=l[i];j<=r[i];j++){
            pos[j]=i;
        }
    }
}
void upd(int L,int R,int x){
    if(pos[L]==pos[R]){
        for (int i=L;i<=R;i++){
            a[i]+=x;
        }
        return;
    }
    for (int i=L;i<=r[pos[L]];i++){
        a[i]+=x;
    }
    for (int i=l[pos[R]];i<=R;i++){
        a[i]+=x;
    }
    for (int i=pos[L]+1;i<pos[R];i++){
        lazy[i]+=x;
    }
    return;
}
int query(int x){
    return a[x]+lazy[pos[x]];
}
signed main()
{
    cin >> n;
    for (int i=1;i<=n;i++){
        cin >> a[i];
    }
    build();
    for (int i=1;i<=n;i++){
        int op,p,q,c;
        cin >> op >> p >> q >> c;
        if(op==0){
            upd(p,q,c);
        }
        else {
            cout << query(q) << endl;
        }
    }
    return 0;
}