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 ...
随机推荐
- webpack 的异步组件 生成commonchunks
new webpack.optimize.CommonsChunkPlugin({ async: 'async-common', minChunks: function (module, count) ...
- iOS开发ReactiveCocoa学习笔记(六)
RAC操作方法三. demo地址:https://github.com/SummerHH/ReactiveCocoa.git doNext deliverOn timeout interval del ...
- 面试(Java之IO与NIO)
一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...
- CPU保护模式DPL、CPL简易理解
现代INTEL CPU都有保护模式,实模式这两种CPU运行模式.当CPU加电,CPU初始化时就运行在是模式下,然后现代操作系统会从实模式跳转到保护模式! 为什么需要保护模式? 在最开始编程的汇编时代, ...
- [AngularJS] $location 服务简介
参考博客: https://www.cnblogs.com/gaoruixin/p/6070502.html 简介 $location服务解析在浏览器地址栏中的URL(基于window.locati ...
- ASP.NET的三种开发模式
前言 ASP.NET 是一个免费的Web开发框架,是由微软在.NET Framework框架中所提供的,或者说ASP.NET是开发Web应用程序的类库,封装在System.Web.dll 文件中.AS ...
- java Vamei快速教程21 事件响应
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在GUI中,我们看到了如何用图形树来组织一个图形界面.然而,这样的图形界面是静态的 ...
- idea字体模糊
用jdk1.8的jre替换idea的jre64,但是记得在lib里加上jdk的lib中的tools.jar. 如图: 然后 将原来jre64的TOOLS.jar拷贝到替换后的jre的lib目录下,重启 ...
- IOS 模仿有storyboard的项目控制器的创建
● 先加载storyboard文件(Test是storyboard的文件名) UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@ ...
- python_60_装饰器3
#嵌套函数 def foo(): print('in the foo') def bar(): print('in the bar') bar() #bar()#出错,无法在外边调用,bar函数的作用 ...