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的更多相关文章
随机推荐
- 如何建立远程桌面连接(XP、Vista、Win7)
如何建立远程桌面连接(XP.Vista.Win7) 要求: 1:对方即你要连的机器必须要允许远程桌面连接,操作系统一般是winXP(单用户)和win2003server(多用户),具体设置:右击我的电 ...
- js002---- 标准内置对象
1. js全局的对象(全局作用域里的对象,而不是全局对象), 或者叫标准内置对象 2, 全局对象 是一个Global类的对象. 标准内置对象的分类: 1. 值属性 infinity Na ...
- mysql源码安装(包括5.5和5.7)
1.mysql5.5源码安装 yum install -y cmake ncurses-devel ncurses cd /usr/src wget -c https://cdn.mysql.com/ ...
- PHP获取上周五时间简单写法
1.echo date('Y-m-d',strtotime('last friday')); 2.echo date("Y-m-d H:i:s",mktime(0, 0 , 0,d ...
- Android布局之RelativeLayout
RelativeLayout用到的一些重要的属性: 第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:layout_center ...
- Html学习总结(1)——理解Html的head
HTML文档的head部分,通常包括指定页面标题,为搜索引擎提供关于页面本身的信息,加载样式表,以及加载JavaScript文件(出于性能考虑,多数时候放在页面底部</body>标签结束前 ...
- Android Developer:内存分析器
Heap Viewer,Memory Monitor和Allocation Tracker是用来可视化你的app使用内存的补充工具. 使用Memory Monitor Tool来发现是否有不好的内存回 ...
- php中array_merge函数
php中array_merge函数 一.array_merge简介 (PHP 4, PHP 5, PHP 7) array_merge — 合并一个或多个数组 说明¶ array array_merg ...
- 搜寻Linux软件实用指南
搜寻Linux软件实用指南 对于初学者来说,仅仅安装好Linux系统还是不够的,还需要安装大量的应用软件.许多下载网站都提供了诸如装机必备软件的下载,分门别类提供经典的工具软件下载.本文主要针对初学 ...
- Index was out of range
Index was out of range. Must be non-negative and less than the size of the collection. Parameter nam ...