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,得值。

 #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的更多相关文章

随机推荐

  1. 好吧,左小波出山了——ie8兼容indexOf问题

    我,还是一个不懂世事的毛头小子,第一次写博.万事开头难,没事咱慢慢来.咳,练文笔吗.我觉得写东西最锻炼逻辑思维,我是一个不善于表达的人,可能是程序员的通病,但你看看人家王小波,八九十年代的作家兼职程序 ...

  2. 学习《人工智能一种现代的方法(第3版)》中文PDF+英文PDF

    学习人工智能概论时,推荐看看<人工智能:一种现代的方法(第3版)>,最权威.最经典的人工智能教材,已被全世界100多个国家的1200多所大学用作教材. 全面性以及结构的安排还是不错的,值得 ...

  3. 两种方法解决 "The License CNEKJPQZEX- has been cancelled..." 问题

    今天在使用 2017 的 IDEA 和 Pycharm 等IDE的时候,提示了如题的问题.之前实在 http://idea.lanyus.com/ 网站点击生成注册码,复制粘贴到 IDEA 中就好了, ...

  4. mysql InnoDB加锁分析

    文章转载自:http://www.fanyilun.me/2017/04/20/MySQL%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/ 以下实验数据基于MySQL 5.7 ...

  5. 学习——HTML5中事件

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  6. 关于Java的10个谎言

    以下的这些都算是比較高级的问题了.面试中一般也非常少问到.由于它们可能会把面试者拒之门外.只是你能够自己找个时间来实践一下. System.exit(0)会跳过finally块的运行 System.s ...

  7. Elasticsearch之源码分析(shard分片规则)

    前期博客是 Elasticsearch之源码编译 (1)elasticsearch在建立索引时,根据id或(id,类型)进行hash,得到hash值之后再与该索引的分片数量取模,取模的值即为存入的分片 ...

  8. 在Red Hat Linux服务器端假设NSF Server来进行Linux系统安装全过程

            本教程讲述了通过在Red Hat Linux服务器端假设NSF Server来进行Linux系统安装的过程,并详细介绍了如何制作网络启动盘的细节.演示直观,讲解通俗易懂,特别适合初学者 ...

  9. 使用无线局域网(WLAN)更需要注意加强安全防范

           下面链接介绍对WLAN的安全加密部分的内容,主要对WinAircrackPack工具的在Wlan方面的应用分析,介绍常见几种的加密方式,以及再使用无线设备时候的注意事项.650) thi ...

  10. sql server 内置MD5加密函数

    http://blog.csdn.net/rookie_liu_ToFly/article/details/53116932 select right(sys.fn_VarBinToHexStr(HA ...