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的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
随机推荐
- WebStorm配置SVN
下载SVN客户端管理工具TortoiseSVN-1.8.5.25224-x64-svn-1.8.8,选择合适的Windows版本 配置项目目录,对应的VCS为Subversion 设置Subversi ...
- 关于include,load的几个问题
参考:http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html 1. include的文件在哪找,找不到会如何? 工具: gcc - ...
- codeforces 665E E. Beautiful Subarrays(trie树)
题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...
- 屏幕适配-使用autoLayout
当遇见xib中无法删除的控件时. 将这个错误的控件拖离本xib(第一个元素.xib文件是有许多元素组成的集合),确保这个xib是正确的.重新创建一个xib文件,将这个正确的xib元素整个复制过去. 在 ...
- Hadoop学习资料(持续更新)
Alex的Hadoop菜鸟教程 Hadoop资料合集 Hadoop平台和应用程序框架
- 在 Ubuntu 系统中有三种设置环境变量 PATH 的方法。(ZT) repost
来源地址: http://blog.csdn.net/jernymy/article/details/6547671 第一种适用于为单一用户设置PATH.第二种是为全局设置 PATH.第三种方法适合于 ...
- bzoj3198
容斥原理+哈希表 恰好k个,那么上容斥原理,我们先2^6枚举相同的位置,用哈希表判断有多少个对应位置相同的元素,然后用容斥原理计算,似乎这里的容斥没有那么简单,详见这里 http://www.cnbl ...
- 使用VirtualBox虚拟机搭建局域网(续)
最近家中Windows 10更新后抽风,最后不得不重装系统,原本配置好的开发环境全部要重装,也包括局域网搭建,但由于是家庭网络并非公司局域网,故旧文<使用VirtualBox虚拟机搭建局域网&g ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 03. 服务注册和管道
ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 03. 服务注册和管道 语雀: https://www.yuque.com/yuejiangliu/dotnet/ ...
- Mr.Jin系统发布报告——WIN7 WIN8双系统下的学习模式系统
Mr.Jin系统发布报告 本文由Mr.Jin原创发表于博客园.CSDN论坛 http://www.cnblogs.com/CHPowerljp-IT/p/3463850.html ...