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的更多相关文章
随机推荐
- rm---删除目录huo文件
rm命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉.对于链接文件,只是删除整个链接文件,而原有文件保持不变. 注意:使用rm命令要格外小心.因为一旦 ...
- 【Uva 12105】Bigger is Better
[Link]: [Description] 让你用最多n根棍子,组成一个数字,使得它能够被m整除; 数字1..9分别需要用-根棍子. 要求这个数字尽可能地大; 然后输出这个数字. [Solution] ...
- FragmentTransaction的commit和commitAllowingStateLoss的差别
1.什么是FragmentTransaction? 使用Fragment时.能够通过用户交互来运行一些动作.比方添加.移除.替换等. 全部这些改变构成一个集合,这个集合被叫做一个transaction ...
- Erlang简单并行server
Erlang简单并行服务器 (金庆的专栏) Erlang并行服务器为每一个Tcp连接创建相应的连接进程,处理client数据. 參考 Erlang程序设计(第2版)17.1.3 顺序和并行服务器 并行 ...
- [c++]基类对象作为函数參数(赋值兼容规则)
编程处理教师的基本情况. 要求: 1.定义一个"person"类.用来存储及处理人的姓名.性别.年龄,成员函数自定: 2.定义"teacher"类,公有继承&q ...
- js---10时间类
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Python 面向对象与 C++、Java 的异同
1. 子类是否自动调用父类的构造方法 C++.Java 会在子类对象的构造中自动首先调用父类的构造: Python 则相对啰嗦一点: 如果子类不覆盖父类的__init__()方法,则子类默认将执行与父 ...
- 窗体是不出现在Alt+Tab中(窗体不出现在任务管理器中的应用程序列中)
窗体是不出现在Alt+Tab中和不出现在任务管理器中的应用程序中 重写 CreateParams即可: public class MyForm : Form{ protected override C ...
- 【2017 Multi-University Training Contest - Team 5】Rikka with Subset
[Link]: [Description] 给你a数组的n个数的所有2^n个子集的2^n个子集元素的和; 子集元素的和最大为m; 告诉你各个子集元素的和出现的次数; 如 1 2 则0出现1次,1出现1 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...