codeforce GYM 100741 A Queries
A. Queries
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
3 4
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
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的更多相关文章
- Codeforce Gym 100015I Identity Checker 暴力
Identity Checker 题目连接: http://codeforces.com/gym/100015/attachments Description You likely have seen ...
- codeforce gym/100495/problem/K—Wolf and sheep 两圆求相交面积 与 gym/100495/problem/E—Simple sequence思路简述
之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- codeforce gym/100495/problem/F Snake++——DFS应用
emmmm.... 在被新生暴打后,我花了很久才补出这道DFS.由于WA1检查了半天,最后竟然是输出少了一个: ,心态小崩. 这里普通的dfs算出的连通区域并不能直接当做最后的答案.所以需要类似模 ...
- codeforce Gym 100425E The Street Escalator(期望,线性递推)
算数学期望,每个人都可以分开来考虑.Xi表示第i个人跑到另外一边的次数. Xi服从二项分布.概率的和是个二项式,(p+1-p)^T,把二项式展开,p的偶次项是留在原来那一边的概率. 可以用((a+b) ...
- codeforce Gym 100418K Cards (概率,数学)
题意:麦田的故事,n张牌,取x张牌,记住前x张牌最大的值m,继续往后取,遇到第一张比m大的牌就停下来.求一个x使得最后的牌在整副牌里是最大的期望最大. 假设最大的牌是A,A在各种位置出现的概率就是相等 ...
- codeforce Gym 100342H Hard Test (思考题)
题意:构造让Dijkstra单源最短路算法有效松弛次数最多的数据... 题解:构造,题意换种说法就是更新晚的路径要比更新早的路径短.因为所有点都会更新一次,那么按照更新时间形成一条链,即到最后一个点的 ...
- codeforce Gym 100342J Triatrip (bitset)
傻逼题,但是为什么别人的O(n^3)不会T?只是因为用了bitset优化... 附上一张bitset基本操作的表 #include<bits/stdc++.h> using namespa ...
- codeforce Gym 100685F Flood (topo排序)
如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...
随机推荐
- JMeter测试TCP服务器遇到的一个奇怪问题
今天工作需要测TCP服务器的压力,因为tsung测试TCP需要写的脚本实在头大,于是换了JMETER来搞压力测试.在实际测试的过程中,遇到了一个很奇怪的问题,就是发了数据包以后,JMeter不停地报5 ...
- 使用Set去除String中的重复字符
使用Set去除String中的重复字符 public class test14 { public static void main(String[] args) { String str = &quo ...
- gitk更改主题设置打不开
➜ project git:(master) gitk Error in startup script: unknown color name "lime" (processing ...
- php 05
流程控制 一.流程控制 1.顺序结构 //自上而下 从左到右 2.条件分支结构 a. 单向分支结构 if() 只能管理一条指令 这条指令是和他紧跟着的指令 if(){} 只能管理整个花括号里面的代码 ...
- python decorator 用法
https://www.zhihu.com/question/31265857 http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html ...
- 文件IO——将文件dfs的文件内容第三个字节之后的内容复制到文件dfd中
/* 使用文件IO将文件fds中的内容复制到文件fdd中去 1.创建两个文件描述符 2.使用open()方法分别以只读只写方式将文件描述符符文件连接 3.将读位置后移三位 4.将fds内容存储到缓冲区 ...
- POJ2112 Optimal Milking---二分+Floyd+网络流
题目链接: https://vjudge.net/problem/POJ-2112 题目大意: k个机器,每个机器最多服务m头牛. c头牛,每个牛需要1台机器来服务. 告诉你牛与机器每个之间的直接距离 ...
- 关于explain
> db.imooc_2.find({x:}).explain() { "queryPlanner" : { , "namespace" : " ...
- 将bat批处理文件注册成windows服务
C:\Users\lenovo>sc create MyService binPath= "C:\Program Files\restartOracle.bat" type ...
- NET_Framework_4.0installer.rar
部署提示: 1.首先下载有关的安装程序 NET_Framework_4.0installer.rar 这是我整理好的四个软件(大致一共10MB),分别如下 WindowsInstaller-KB893 ...