A. Queries

time limit per test:0.25 s
memory limit per test:64 MB
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

题意

一个长度为 n 的序列,q 次三种操作,

+ p r: 下标为 p 的数加 r.
- p r: 下表为 p 的数减 r.
s l r mod: 询问在区间[l,r]中模 m 等于 mod 的所有数的和。

分析

可以建立m个树状数组,然后询问就好处理了,加减要现在原来的树状数组中减掉,然后在之后的树状数组中加上。

code

 #include<cstdio>
#include<algorithm> using namespace std;
typedef long long LL; const int MAXN = ;
LL n,m,q;
LL s[MAXN]; LL read()
{
LL x = ,f = ;char ch = getchar();
while (ch<''||ch>'') {if (ch=='-') f=-; ch = getchar(); }
while (ch>=''&&ch<='') {x = x*+ch-''; ch = getchar(); }
return x*f;
} struct Tree_array{
LL a[MAXN];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int v)
{
for (; x<=n; x+=lowbit(x)) a[x] += v;
}
LL query(int x)
{
LL ret = ;
for (; x; x-=lowbit(x))
ret += a[x];
return ret;
}
}t[]; int main()
{
n = read();m = read();
for (int i=; i<=n; ++i)
{
s[i] = read();
t[s[i]%m].update(i,s[i]);
}
char opt[];
q = read();
LL x,y,mo;
while (q--)
{
scanf("%s",opt);
x = read();y = read();
if (opt[]=='s')
{
mo = read();
printf("%lld\n",t[mo].query(y)-t[mo].query(x-));
}
else if (opt[]=='+')
{
t[s[x]%m].update(x,-s[x]);
s[x] += y;
t[s[x]%m].update(x,s[x]);
printf("%lld\n",s[x]);
}
else
{
if (s[x]<y) printf("%lld\n",s[x]);
else
{
t[s[x]%m].update(x,-s[x]);
s[x] -= y;
t[s[x]%m].update(x,s[x]);
printf("%lld\n",s[x]);
}
}
}
return ;
}

codeforce GYM 100741 A Queries的更多相关文章

  1. Codeforce Gym 100015I Identity Checker 暴力

    Identity Checker 题目连接: http://codeforces.com/gym/100015/attachments Description You likely have seen ...

  2. codeforce gym/100495/problem/K—Wolf and sheep 两圆求相交面积 与 gym/100495/problem/E—Simple sequence思路简述

    之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...

  3. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  4. codeforce gym/100495/problem/F Snake++——DFS应用

    emmmm.... 在被新生暴打后,我花了很久才补出这道DFS.由于WA1检查了半天,最后竟然是输出少了一个:   ,心态小崩. 这里普通的dfs算出的连通区域并不能直接当做最后的答案.所以需要类似模 ...

  5. codeforce Gym 100425E The Street Escalator(期望,线性递推)

    算数学期望,每个人都可以分开来考虑.Xi表示第i个人跑到另外一边的次数. Xi服从二项分布.概率的和是个二项式,(p+1-p)^T,把二项式展开,p的偶次项是留在原来那一边的概率. 可以用((a+b) ...

  6. codeforce Gym 100418K Cards (概率,数学)

    题意:麦田的故事,n张牌,取x张牌,记住前x张牌最大的值m,继续往后取,遇到第一张比m大的牌就停下来.求一个x使得最后的牌在整副牌里是最大的期望最大. 假设最大的牌是A,A在各种位置出现的概率就是相等 ...

  7. codeforce Gym 100342H Hard Test (思考题)

    题意:构造让Dijkstra单源最短路算法有效松弛次数最多的数据... 题解:构造,题意换种说法就是更新晚的路径要比更新早的路径短.因为所有点都会更新一次,那么按照更新时间形成一条链,即到最后一个点的 ...

  8. codeforce Gym 100342J Triatrip (bitset)

    傻逼题,但是为什么别人的O(n^3)不会T?只是因为用了bitset优化... 附上一张bitset基本操作的表 #include<bits/stdc++.h> using namespa ...

  9. codeforce Gym 100685F Flood (topo排序)

    如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...

随机推荐

  1. laydate 显示结束时间不小于开始时间

    jsp: <div class="form-group">    <label >交易时间:</label>        <input ...

  2. 【Mood-14】龙虎榜 活跃在github中的1000位中国开发者

    Last cache created on 2015-01-07 by Github API v3. ♥ made by hzlzh just for fun. Rank Gravatar usern ...

  3. Java中对jsonArray的排序,使用的是Gson

    使用Gson对json解析字符串,转化为json对象. 先上代码: 下面是main方法里面的代码 package testJava; import java.util.ArrayList; impor ...

  4. mybatis 异常处理:Invalid bound statement (not found)

    mybatis 的使用过程中提示错误: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): ...

  5. vue的MVVM模式和生命周期总结(一)

    一.MVVM模式 MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel ...

  6. c++ STL stack容器成员函数

    这是后进先出的栈,成员函数比较简单,因为只能操作栈顶的元素.不提供清除什么的函数. 函数 描述 bool s.empty() 栈是否为空(即size=0).若空,返回true,否则,false. vo ...

  7. python_77_json与pickle序列化3

    #此方法:dump多次,而不可以load多次,只能load一次,否则会出错 只有序列化,无反序列化 import json info={ 'name':'Xue Jingjie', 'age':22, ...

  8. springboot集成shiro的session污染问题

    问题起因是这样的,有两套系统,系统a和系统b.两套系统均使用shiro做的权限管理,之前部署在两台机器上.使用浏览器打开a系统后另开页签打开b系统,互不干扰都能正常使用,后因业务迁移,两套系统部署到了 ...

  9. Python判断一个数是否为小数

    一.判断一个数是否为小数 1.有且仅有一个小数点 2.小数点的左边可能为正数或负数 3.小数点的右边为正数 二.实现代码 def is_float(str): if str.count('.') == ...

  10. Mac 系统 + Chrome浏览器 网页前端出现中文文字反转或顺序错乱

    问题背景 React开发的系统,收到一个BUG反馈,*"号个人统计"文字不正确,应为"个人号统计"*. 收到BUG后,打开浏览器查验是什么情况,难道犯了最基本的 ...