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 ...
随机推荐
- Google,真的要离我们而去吗?
Google,真的要离我们而去吗? 好怀念,真正要解决问题,还得搜google!
- HTML5&CSS挑战
地址:https://www.w3cschool.cn/codecamp/list?pename=html5_and_css_camp 开始学习HTML标签:欢迎来到编程训练营的第一个编程挑战!你可以 ...
- Css文件目录结构
一般一个网站会有这么三个样式: global.css | reset.css(格式化样式) common.css(公共组件样式) layout.css(当前页面样式) global.css | res ...
- NC57访问报错:java.sql.SQLException: Io 异常: Got minus one from a read call
一.报错信息 1. 前端登录界面 2. 后台应用日志 报错信息一致为: $$callid= $$thread=[Service Monitor and Runtime Enroment] $$ho ...
- 微信小程序开发入门首选
推荐一本书吧,直接上图,微信开发,微信网页开发,微信小程序开发,都用得着. 推荐一本书吧,直接上图,微信开发,微信网页开发,微信小程序开发,都用得着. 推荐一本书吧,直接上图,微信开发,微信网页开发, ...
- zabbix文档3.4-7配置
zabbix文档3.4-7配置 1 主机和主机组 典型的Zabbix主机是您希望监视的设备(服务器,工作站,交换机等). 创建主机是Zabbix中首个监控任务之一.例如,如果要监视服务器"x ...
- mybatis 异常处理:Invalid bound statement (not found)
mybatis 的使用过程中提示错误: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): ...
- git入门使用摘录
无论使用github或者gitlab,第一步都是在本地生产ssh-key,ssh-key作为客户端的身份证存放在user用户的.ssh文件夹下.如果之前没有生产过,需要用ssh-keygen命令生成. ...
- IOS 绘制图片水印(封装)
- (void)viewDidLoad { [super viewDidLoad]; // -1.加载图片 // UIImage *image = [UIImage imageNamed:@" ...
- 【BZOJ1972】[SDOI2010] 猪国杀(恶心的大模拟)
点此看题面 大致题意: 让你模拟一个游戏猪国杀的过程. 几大坑点 对于这种模拟题,具体思路就不讲了,就说说有哪些坑点. 题面有锅,反猪是\(FP\). 数据有锅,牌堆中的牌可能不够用,牌堆为空之后需一 ...