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. Centos7.6下安装Python3.7

    前言 话说不会开发的运维不是一个好的DBA,所以我要开始学习python了,写博客记录一下我的学习过程,另外别欺负我新来的,那个每天更博的技术流ken是我哥. 不说了,时间宝贵,开整. 1.首先来看一 ...

  2. HDU——T 1251 统计难题

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 Time Limit: 4000/2000 MS (Java/Others)    Memory Limi ...

  3. Windows7下修改pip源

    以下列举三种方式的pip源配置: 1. 设置环境变量PIP_CONFIG_FILE指向pip.ini源配置文件,pip.ini文件内容如下: [global] index-url = http://m ...

  4. [Python] Working with file

    Python allows you to open a file, do operations on it, and automatically close it afterwards using w ...

  5. (诡异Floyd&amp;自环)MZ Training 2014 #15 E题(POJ 2240)

    你们见过这么诡异的FLOYD吗? 先上题. [Description] 货币的汇率存在差异.比如,如果1美元购买0.5英镑,1英镑买10法郎.而1法国法郎买0.21美元.然后,通过转换货币,一个聪明的 ...

  6. 线段树 hdu3642 Get The Treasury

    不得不说,这是一题很经典的体积并.. 然而还是debug了2个多小时... 首先思路:按z的大小排序. 然后相当于扫描面一样,,从体积的最下方向上方扫描,遇到这个面 就将相应的两条线增加到set中,或 ...

  7. vim 技巧之用宏命令批量处理文件

    今天遇到了一种情况,就是我需要同时修改34个文件中的某些字符串的内容,如果一个个打开需改的话,那也太麻烦了.后来就想着能不能通过vim的宏命令来修改呢?现在就总结下关于宏在文件列表中的应用1.首先,我 ...

  8. POJ 1904 思路题

    思路: 思路题 题目诡异地给了一组可行匹配 肯定有用啊-. 就把那组可行的解 女向男连一条有向边 如果男喜欢女 男向女连一条有向边 跑一边Tarjan就行了 (这个时候 环里的都能选 "增广 ...

  9. Linux 时区的修改

    Linux 时区的修改 1. CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 ``` [root@centos7 ...

  10. let---bash中用于计算的工具