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的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
随机推荐
- android按压背景
android:background="?android:actionBarItemBackground"
- 各种java生成word解决方案的优缺点对比
解决方案 优点 缺点 Jacob 功能强大 直接调用VBA接口,程序异常复杂:服务器必须是:windows系统+安装Office:服务器端自动化com接口容易产生死进程造成服务器宕机 Apache P ...
- InstallShield 12 豪华版+破解版 下载
InstallShield 12 豪华版+破解版 下载 2009-07-09 19:18:30| 分类: 默认分类|字号 订阅 InstallShield 12 豪华版+破解版 下载 下载方 ...
- B. Simple Molecules
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 为什么linux有足够的内存还进行swap?
1.Linux在内存被用完之前开始交换.这是为了提高性能和响应能力: 性能提高是因为一些内存放磁盘缓存比方内存更合适.因此,最好将一个已经停用了一段时间的程序交换出去,而将经常使用的文件保存在缓存中. ...
- (链接)Tomcat设置Session的失效方式
tomcat 设置session过期时间(四种方式):https://blog.csdn.net/liuxiao723846/article/details/50055075 tomcat里设置ses ...
- JAVA基础-面向对象05
一.面向对象的引入 解决问题的时候,首先要会分析思路,这个思路就是我们生活中解决一个问题的方法步骤: 有了思路之后,再把思路使用java代码来解决: 但是 计算总分的需求变了 分析:原来在一个程序中, ...
- ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(六)学生借阅/预约/查询书籍事务
前言: 本系列文章主要为我之前所学知识的一次微小的实践,以我学校图书馆管理系统为雏形所作. 本系列文章主要参考资料: 微软文档:https://docs.microsoft.com/zh-cn/asp ...
- POJ3258【二分】
题意: 问一个河岸,两岸之间有笔直的n块石头,然后拔起(也可以施展魔法)m个石块,假设两岸也是石块,求处理过的石块的最小距离的最大. 思路: 他让我们求移开m个石块,无非是在n+2-m(已经把两岸看成 ...
- 图论之最小生成树之Kruskal算法
Kruskal算法,又称作为加边法,是配合并查集实现的. 图示: 如图,这是一个带权值无向图我们要求它的最小生成树. 首先,我们发现在1的所有边上,连到3的边的边权值最小,所以加上这条边. 然后在3上 ...