【算法】树状数组(区间和)
【题解】记得开long long
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<algorithm> #define lowbit(x) (x&(-x)) using namespace std; const int maxn=50010; int n,m; long long a[maxn]; void give(int x,int k) { for(int i=x;i<=n;i+=lowbit(i)) { a[i]+=1ll*k; } } long long ask(int x) { long long ans=0; for(int i=x;i>=1;i-=lowbit(i)) { ans+=a[i]; } return ans; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); give(i,x); } scanf("%d",&m); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); printf("%lld ",ask(x+y-1)-ask(x-1)); } return 0; }