Codeforces_GYM_100741 A

http://codeforces.com/gym/100741/problem/A

A. Queries

time limit per test
0.25 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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). () ()
Input

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

Output q lines - the answers to the queries.

Examples
input
3 4
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
output
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 }

 

转载于:https://www.cnblogs.com/Shy-key/p/7245653.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值