模m意义下
所以,开m个树状数组进行操作
#include<bits/stdc++.h> #define N 10010 #define ll long long using namespace std; struct BIT { ll c[N]; int n; int lowbit(int x) {return x&(-x);} void modify(int x,ll y) {for(; x<=n; x+=lowbit(x)) c[x]+=y;} ll query(int x) {ll ret=0; for(; x; x-=lowbit(x)) ret+=c[x]; return ret;} ll query(int l,int r) {return query(r)-query(l-1);} } bit[20]; int n,m,T; ll a[N]; int main() { scanf("%d%d",&n,&m); for(int i=0; i<m; i++) bit[i].n=n; for(int i=1; i<=n; i++) { scanf("%lld",&a[i]); bit[a[i]%m].modify(i,a[i]); } scanf("%d",&T); while(T--) { int x,y,z; char opt[10]; scanf("%s%d%d",opt,&x,&y); if(opt[0]=='+') { bit[a[x]%m].modify(x,-a[x]); a[x]+=y; bit[a[x]%m].modify(x,a[x]); printf("%lld ",a[x]); } if(opt[0]=='-') { if(a[x]<y) printf("%lld ",a[x]); else { bit[a[x]%m].modify(x,-a[x]); a[x]-=y; bit[a[x]%m].modify(x,a[x]); printf("%lld ",a[x]); } } if(opt[0]=='s') { scanf("%d",&z); printf("%lld ",bit[z].query(x,y)); } } return 0; }