http://codeforces.com/gym/100741/problem/A
A. Queries
Mathematicians are interesting (sometimes, I would say, even crazy) people. For example, my friend, a mathematician, thinks that it is very fun to play with a sequence of integer numbers. He writes the sequence in a row. If he wants he increases one number of the sequence, sometimes it is more interesting to decrease it (do you know why?..) And he likes to add the numbers in the interval [l;r]. But showing that he is really cool he adds only numbers which are equal some mod (modulo m).
Guess what he asked me, when he knew that I am a programmer? Yep, indeed, he asked me to write a program which could process these queries (n is the length of the sequence):
- + p r It increases the number with index p by r. (
,
)
You have to output the number after the increase.
- - p r It decreases the number with index p by r. (
,
) You must not decrease the number if it would become negative.
You have to output the number after the decrease.
- s l r mod You have to output the sum of numbers in the interval
which are equal mod (modulo m). (
) (
)
The first line of each test case contains the number of elements of the sequence n and the number m. (1 ≤ n ≤ 10000) (1 ≤ m ≤ 10)
The second line contains n initial numbers of the sequence. (0 ≤ number ≤ 1000000000)
The third line of each test case contains the number of queries q (1 ≤ q ≤ 10000).
The following q lines contains the queries (one query per line).
Output q lines - the answers to the queries.
3 4
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
2
3
1
m个 树状数组记录a[i]%m,得值。
1 #define LL long long 2 #include <iostream> 3 4 using namespace std; 5 6 const int MAXN(1000000000); 7 const int N(10000+15); 8 LL n,m,a[N],q,u,v,w; 9 10 struct Tree 11 { 12 LL mm; 13 LL val[N]; 14 #define lowbit(x) (x&(-x)) 15 void Update(int now,int x) 16 { 17 for(;now<=mm;now+=lowbit(now)) val[now]+=x; 18 } 19 LL Query(int x) 20 { 21 LL ret=0; 22 for(;x;x-=lowbit(x)) ret+=val[x]; 23 return ret; 24 } 25 }tree[110]; 26 27 int main() 28 { 29 cin>>n>>m; 30 for(LL i=0;i<m;i++) tree[i].mm=n; 31 for(LL i=1;i<=n;i++) 32 { 33 cin>>a[i]; 34 tree[a[i]%m].Update(i,a[i]); 35 } 36 cin>>q; 37 for(char ch[2];q--;) 38 { 39 cin>>ch>>u>>v; 40 if(ch[0]=='+') 41 { 42 tree[a[u]%m].Update(u,-a[u]); 43 a[u]+=v; 44 tree[a[u]%m].Update(u,a[u]); 45 cout<<a[u]<<endl; 46 } else 47 if(ch[0]=='-') 48 { 49 if(a[u]<v) cout<<a[u]<<endl; 50 else 51 { 52 tree[a[u]%m].Update(u,-a[u]); 53 a[u]-=v; 54 tree[a[u]%m].Update(u,a[u]); 55 cout<<a[u]<<endl; 56 } 57 } else 58 if(ch[0]=='s') 59 { 60 cin>>w; 61 cout<<tree[w].Query(v)-tree[w].Query(u-1)<<endl; 62 } 63 } 64 return 0; 65 }