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的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
随机推荐
- 第五届蓝桥杯C++B组省赛
1.啤酒和饮料 2.切面条 3.李白打酒 4.史丰收速算 5.打印图形 6.奇怪的分式 7.六角填数 8.蚂蚁感冒 9.地宫取宝 10.小朋友排队
- mac下载模块时报错OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/chardet'
原文地址:https://www.cnblogs.com/liangyan-1989/p/8143129.html 安装完pip后,使用pip install selenium报以下错 OSError ...
- 【转】IntelliJ IDEA搭建Spring环境
//本来在草稿箱写好了,忘记发就被冲掉了,重新再写一遍. Spring初探 Spring初探 在IntelliJ IDEA中创建Spring项目 一个简单的例子介绍框架的作用 那么什么时候new的对象 ...
- 任务39:Role以及Claims授权
基于Role角色的授权 asp.net core在逐渐淘汰这种基于RoleBase的基于角色的授权.鼓励大家使用基于Claim的授权 在认证的时候Cliam已经加入了Role 注释38节课的 自定义验 ...
- iOS View
创建: 2018/04/19 完成: 2018/04/20 View的创建 创建 storyboard上操作 与代码连接 ● 目的: 通过代码控制view ● 按住option拖动 View的坐 ...
- 瞎比比系列---1st
A - 项目管理HDU4858 /* 题意: 这个项目有n个节点, 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 0的时候:接下来两个数u v表示给项目u的能量值加上v: 1的时候: 这 ...
- Jquery | 基础 | 慕课网 | (*选择器)
原生JS var elements1 = document.getElementsByTagName('*'); JQ var elements2 = $("*"); <!D ...
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
- POM报错Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from
解决方式一: 1.查看.m2\repository\org\apache\maven\plugins\maven-resources-plugin\下maven-resources-plugin- ...
- SqlParameter 操作 image 字段
public static void AddEmployee( string lastName, string firstName, string title, DateTime hireDate, ...