1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=100005; int n,m,Belong[maxn],l[maxn],r[maxn],Left[maxn],Right[maxn],BCC=0; LL a[maxn],f[maxn],cnt[325][maxn],sum[maxn],sum_block[maxn]; void Modify(int pos,LL v) { for(int i=1; i<=BCC; i++)f[i]+=(v-a[pos])*cnt[i][pos]; a[pos]=v; for(int i=pos; i<=Right[Belong[pos]]; i++) { if(i==Left[Belong[pos]])sum[i]=a[i]; else sum[i]=sum[i-1]+a[i]; } for(int i=Belong[pos]; i<=BCC; i++)sum_block[i]=sum_block[i-1]+sum[Right[i]]; } LL Sum(int x,int y) { int L=Belong[x],R=Belong[y]; LL ans=0; if(L==R) { if(x==Left[L])return sum[y]; else return sum[y]-sum[x-1]; } else if(L+1==R) { if(x==Left[L])return sum[Right[L]]+sum[y]; else return sum[Right[L]]-sum[x-1]+sum[y]; } else { if(x==Left[L])return sum_block[R-1]-sum_block[L-1]+sum[y]; else return sum[Right[L]]-sum[x-1]+sum_block[R-1]-sum_block[L]+sum[y]; } } LL Query(int x,int y) { int L=Belong[x],R=Belong[y]; LL ans=0; if(L==R)for(int i=x; i<=y; i++)ans+=Sum(l[i],r[i]); else { for(int i=L+1; i<=R-1; i++)ans+=f[i]; for(int i=x; i<=Right[L]; i++)ans+=Sum(l[i],r[i]); for(int i=Left[R]; i<=y; i++)ans+=Sum(l[i],r[i]); } return ans; } int main() { n=Get_Int(); for(int i=1; i<=n; i++)a[i]=Get_Int(); for(int i=1; i<=n; i++) { l[i]=Get_Int(); r[i]=Get_Int(); } int size=sqrt(n); for(int i=1; i<=n; i++) { Belong[i]=(i-1)/size+1; if(!Left[Belong[i]])Left[Belong[i]]=i,BCC++; Right[Belong[i]]=i; if(i==Left[Belong[i]])sum[i]=a[i]; else sum[i]=sum[i-1]+a[i]; } for(int i=1; i<=BCC; i++) { for(int j=Left[i]; j<=Right[i]; j++) { cnt[i][l[j]]++; cnt[i][r[j]+1]--; } for(int j=1; j<=n; j++) { cnt[i][j]+=cnt[i][j-1]; f[i]+=cnt[i][j]*a[j]; } } for(int i=1; i<=BCC; i++)sum_block[i]=sum_block[i-1]+sum[Right[i]]; m=Get_Int(); for(int i=1; i<=m; i++) { int opt=Get_Int(),x=Get_Int(); LL y=Get_Int(); if(opt==1)Modify(x,y); else printf("%lld\n",Query(x,y)); } return 0; }
|