Codeforces GYM 100741A . Queries
0.25 seconds
64 megabytes
standard input
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). (
) (
)
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 q lines - the answers to the queries.
3 4
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
2
3
1 题目大意:
s 求 l~r中 对m取模==mod 的 和
+ 单点修改
- 单点修改,如果减后小于0直接输出 树状数组
屠龙宝刀点击就送
#include <ctype.h>
#include <cstdio>
#define N 10005
typedef long long LL;
void read(LL &x)
{
x=;bool f=;
char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=;
for(;isdigit(ch);ch=getchar()) x=x*+ch-'';
x=f?(~x)+:x;
}
LL dis[N],n,m,q;
struct node
{
LL tag[N];
int n;
int lowbit(int x) {return x&((~x)+);}
void plus(int x,int y)
{
for(;x<=n;x+=lowbit(x)) tag[x]+=y;
}
LL query(int x)
{
LL ans=;
for(;x;x-=lowbit(x)) ans+=tag[x];
return ans;
}
}a[];
int main()
{
read(n);read(m);
for(int i=;i<m;i++) a[i].n=n;
for(int i=;i<=n;i++)
{
read(dis[i]);
a[dis[i]%m].plus(i,dis[i]);
}
char str[];
read(q);
for(LL x,y,z;q--;)
{
scanf("%s",str+);read(x);read(y);
switch(str[])
{
case 's':
{
read(z);
printf("%lld\n",a[z].query(y)-a[z].query(x-));
break;
}
case '+':
{
a[dis[x]%m].plus(x,-dis[x]);
dis[x]+=y;
a[dis[x]%m].plus(x,dis[x]);
printf("%lld\n",dis[x]);
break;
}
case '-':
{
if(dis[x]<y) {printf("%lld\n",dis[x]);}
else
{
a[dis[x]%m].plus(x,-dis[x]);
dis[x]-=y;
a[dis[x]%m].plus(x,dis[x]);
printf("%lld\n",dis[x]);
}
break;
}
}
}
return ;
}
Codeforces GYM 100741A . Queries的更多相关文章
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- GYM 100741A Queries
传送门 题目大意: 一个长度为n的序列,q次三种操作 +p r:下标为p的数+r -p r:下标为p的数-r s l r mod [L,R]中有多少数%m=mod,m已经给出 题解: 开十个树状数组 ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
随机推荐
- 【Codeforces 664A】 Complicated GCD
[题目链接] 点击打开链接 [算法] gcd(a,a+1) = 1 所以当a = b时,答案为a,否则为1 [代码] #include<bits/stdc++.h> using names ...
- lnmp-详细编译安装步骤
CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14 一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这 ...
- 容器之vector
#include <iostream> #include <vector> #include <string.h> #include <algorithm&g ...
- hdu5822 color
首先考虑假如是树上的做法:考虑dp,f(i)表示对i的子树染色的方案数.用hash可以实现查询两棵子树是否相同.从而根据hash值排序分类,将相同的子树放在一类. (1)f(i)等于每一类的f(p)乘 ...
- POJ3186【区间DP】
题意: 每次只能取两端,然后第 i 次取要val[ i ]*i,求一个最大值 一切都是错觉[读者省略此段] 这道题目一开始想的就是记忆化搜索,然后太天真了?好像是,一开始用一维dp[ i ]直接代表一 ...
- 分布式集群环境下,如何实现session共享四(部署项目测试)
这是分布式集群环境下,如何实现session共享系列的第四篇.在上一篇:分布式集群环境下,如何实现session共享三(环境搭建)中,已经准备好了相关的环境:tomcat.nginx.redis.本篇 ...
- print打印
print打印输出的优点是简单直接粗暴有效,就是用print()把可能有问题的变量打印出来看看缺点是将来还得删掉它,想想程序里到处都是print(),运行结果也会包含很多垃圾信息 __________ ...
- AdventureWorks2012.mdf的使用
AdventureWorks2012.mdf的使用,在数据库管理器界面中,右击数据库,然后附加,然后选择好AdventureWorks2012.mdf,然后删掉log,然后确定即可.
- __slots__ 和 @property
动态非常灵活, 创建一个class后, 给实例绑定一个属性: >>> class Bird: ... pass ... >>> s = Bird() >> ...
- docker监控之cadvisor
docker run -d \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:rw \ --volume=/sys:/sys:ro \ --vo ...