Codeforces_GYM_100741 A
http://codeforces.com/gym/100741/problem/A
A. Queries
0.25 seconds
64 megabytes
standard input
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). (
) (
)
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,得值。
#define LL long long
#include <iostream> using namespace std; const int MAXN();
const int N(+);
LL n,m,a[N],q,u,v,w; struct Tree
{
LL mm;
LL val[N];
#define lowbit(x) (x&(-x))
void Update(int now,int x)
{
for(;now<=mm;now+=lowbit(now)) val[now]+=x;
}
LL Query(int x)
{
LL ret=;
for(;x;x-=lowbit(x)) ret+=val[x];
return ret;
}
}tree[]; int main()
{
cin>>n>>m;
for(LL i=;i<m;i++) tree[i].mm=n;
for(LL i=;i<=n;i++)
{
cin>>a[i];
tree[a[i]%m].Update(i,a[i]);
}
cin>>q;
for(char ch[];q--;)
{
cin>>ch>>u>>v;
if(ch[]=='+')
{
tree[a[u]%m].Update(u,-a[u]);
a[u]+=v;
tree[a[u]%m].Update(u,a[u]);
cout<<a[u]<<endl;
} else
if(ch[]=='-')
{
if(a[u]<v) cout<<a[u]<<endl;
else
{
tree[a[u]%m].Update(u,-a[u]);
a[u]-=v;
tree[a[u]%m].Update(u,a[u]);
cout<<a[u]<<endl;
}
} else
if(ch[]=='s')
{
cin>>w;
cout<<tree[w].Query(v)-tree[w].Query(u-)<<endl;
}
}
return ;
}
Codeforces_GYM_100741 A的更多相关文章
随机推荐
- Vim操作的四种模式
Vim的四种模式一.启动Vim1.双击桌面的图标,就可以启动Vim(是图形界面的)2.在开始菜单---点--运行 接着输入 vim 或者gvim,就可以启动Vim或Gvim了.二.Vim的模式1.Vi ...
- 从零使用qemu模拟器搭建arm执行环境
为什么会有这篇文章 早在2011年的时候,跟当时同事一起讨论,做Linux系统开发正处于整个Linux开发中间层,没有上层的C/C++业务和数据库的开发经验.也没有底层的内核和驱动开发经验,究竟路该怎 ...
- 【剑指Offer学习】【面试题27:二叉搜索树与双向链表】
题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建不论什么新的结点.仅仅能调整树中结点指针的指向. 比方输入图4.12 中左边的二叉搜索树,则输出转换之后的排序现向链表. ...
- Redis学习手冊(事务)
一.概述: 和众多其他数据库一样,Redis作为NoSQL数据库也相同提供了事务机制. 在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基石. 相 ...
- vue中关于prop
组件之间的项目通信在vue中十分常见,父组件的数据传到子组件需要prop的支持,我们来看下prop 1.html的特性名大小写不敏感,浏览器会把所有大写字母解释为小写字母,使用dom模板时,使用等价的 ...
- C# 爬虫总结
static void Main(string[] args) { //WebRequest request = WebRequest.Create("http://www.cnblogs. ...
- Oracel 格式化日期 to_char()
select empno,ename,job,mgr,to_char(HIREDATE,'yyyy-mm-dd') as 入职日期,sal,comm,deptno from emp order by ...
- CString与 char *之间的转换
http://www.cnblogs.com/watsonlong/archive/2011/04/15/2017086.html
- vue prpos
匹配某些值中的一个 type: { validator: function(value) { return ["success", "warning", &qu ...
- android关键组件service服务(一)
一. Service简单介绍 Service是android 系统中的四大组件之中的一个(Activity.Service.BroadcastReceiver.ContentProvider),它跟A ...