CF GYM 100741 A Queries
2017-07-27 18:52
309 查看
A. Queries
time limit per test0.25 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard 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
time limit per test0.25 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard 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
#include<bits/stdc++.h> #define MAXN 10010 #define LL long long using namespace std; struct BIT { LL c[MAXN]; 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[MAXN]; 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\n",a[x]); } if(opt[0]=='-'){ if(a[x]<y) printf("%lld\n",a[x]); else { bit[a[x]%m].modify(x,-a[x]); a[x]-=y; bit[a[x]%m].modify(x,a[x]); printf("%lld\n",a[x]); } } if(opt[0]=='s'){ scanf("%d",&z); printf("%lld\n",bit[z].query(x,y)); } } return 0; }
相关文章推荐
- Codeforce GYM 100741 A. Queries
- codeforce GYM 100741 A Queries
- 使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录
- 有关启用 'Ad Hoc Distributed Queries' 的详细信息
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 s
- SQL Server 阻止了对组件 \'Ad Hoc Distributed Queries\' 的访问
- (转)Support for COUNT(DISTINCT) in Fetch XML queries
- The Intention Behind Web Queries
- Useful DQL Queries
- “配置选项 'Ad Hoc Distributed Queries' 不存在,也可能是高级选项。 ”的解决办法
- Optimization Tips | Tip #27: AND-queries should match as little as possible as fast as possible
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问
- pivot and unpivot queries in 11g
- hdu 4027 Can you answer these queries? (线段树+__int64)
- Oracle Performance Tuning--Tips for Avoiding Problematic Queries(转载至databasejournal)
- search for queries utilizing a specific field
- Connection is read-only. Queries leading to data modification are not allowe
- SPOJ 6779 Can you answer these queries VII(动态树)
- hdu 3726 Graph and Queries
- ORA-01219:database not open:queries allowed on fixed tables/views only