100741A 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). (
) (
)
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.
题目大意
给你n和m以及n个数,q次操作,操作有三种:
在第p个数加r,输出现在的值
如果第p个数减r非负则将其减r,输出这个数
查询lr区间内模m余mod的数的和
分析
运用分块,因为m很小,我们可以记录每块中模m的各个可能值各种之和,同时维护每个数的值,修改是要将原来所在的分组减去原来数,再在新分组加上这个数。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
long long n,m,sum,block,all[][],beg[],a[],q,r[],l[];
inline long long read(){
long long x=,f=;char s=getchar();
while(s<''||s>''){if(s=='-')f=-;s=getchar();}
while(s>=''&&s<=''){x=(x<<)+(x<<)+(s-'');s=getchar();}
return x*f;
}
inline void init(){
long long i,j,k;
block=sqrt(n);
sum=n%block==?n/block:n/block+;
for(i=;i<=sum;i++)
l[i]=r[i-]+,r[i]=r[i-]+block;
r[sum]=n;
for(i=;i<=n;i++){
beg[i]=(i-)/block+;
all[beg[i]][a[i]%m]+=a[i];
}
}
inline void go(long long x,long long y){
if(a[x]+y<){
printf("%I64d\n",a[x]);
return;
}
all[beg[x]][a[x]%m]-=a[x];
a[x]+=y;
all[beg[x]][a[x]%m]+=a[x];
printf("%I64d\n",a[x]);
}
inline void work(long long x,long long y,long long mod){
long long i,j,k,ans=;
if(beg[x]==beg[y]){
for(i=x;i<=y;i++)
if(a[i]%m==mod)ans+=a[i];
}else {
for(i=x;i<=r[beg[x]];i++)
if(a[i]%m==mod)ans+=a[i];
for(i=beg[x]+;i<beg[y];i++)
ans+=all[i][mod];
for(i=l[beg[y]];i<=y;i++)
if(a[i]%m==mod)ans+=a[i];
}
printf("%I64d\n",ans);
return;
}
int main(){
long long i,j,k,x,y,mod;
n=read(),m=read();
for(i=;i<=n;i++)a[i]=read();
init();
q=read();
for(i=;i<=q;i++){
char c;
cin>>c;
if(c=='+'){
x=read(),y=read();
go(x,y);
}else if(c=='-'){
x=read(),y=read();
go(x,-y);
}else {
x=read(),y=read(),mod=read();
work(x,y,mod);
}
}
return ;
}
100741A Queries的更多相关文章
- Codeforces GYM 100741A . Queries
time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input output stan ...
- 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已经给出 题解: 开十个树状数组 ...
- 实践 HTML5 的 CSS3 Media Queries
先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...
- CSS3 Media Queries 实现响应式设计
在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...
- 使用CSS3 Media Queries实现网页自适应
原文来源:http://webdesignerwall.com 翻译:http://xinyo.org 当今银屏分辨率从 320px (iPhone)到 2560px (大屏显示器)或者更大.人们也不 ...
- SQL Queries from Transactional Plugin Pipeline
Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...
- Media Queries 详解
Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css" rel ...
随机推荐
- FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask
hive启动后,出现以下异常 hive> show databases; FAILED: Error / failed on connection exception: java.net.Con ...
- Java NIO阻塞式通信
package com.nio.t; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.By ...
- L123
My heart, the bird of the wilderness, has found its sky in your eyes. 我的心是旷野的鸟,在你的双眼中找到了天空.His main ...
- linux shell 学习笔记--内部命令学习
.基本命令 新手必须要掌握的初级命令 ls 基本的列出所有文件的命令.但是往往就是因为这个命令太简单,所以我们总是低估它.比如 ,用 -R 选项,这是递归选项,ls 将会以目录树的形式列出所有文件, ...
- shell变量扩展技巧
SHELL中有一些变量扩展的技巧,做下归纳总结 1.取字符串slice规则一:${变量名:位置起点}含义:由指定的位置起点开始,截取子字符串到字符串结束例如: var="/etc/passw ...
- 学习动态性能表(6)--v$session_wait&v$session_event
学习动态性能表 第六篇-(1)-V$SESSION_WAIT 2007.5.30 这是一个寻找性能瓶颈的关键视图.它提供了任何情况下session在数据库中当前正在等待什么(如果session当前什 ...
- docker镜像的导入和导出
启动命令 docker run -d -p 3000:80 twang2218/gitlab-ce-zh:9.0.3 docker run -d -p 8080:80 gitlab/gitlab-ce ...
- python3之编码
这个符号(#!)的名称,叫做"Shebang"或者"Sha-bang"Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件 ...
- (转)配置ORACLE 11g绿色版客户端和PLSQL环境
本文转载自:http://my.oschina.net/jang/blog/83009 本方法是通过使用ORACLE官方提供的精简版客户端,即绿色免安装的客户端. 下载地址(此处提供的是官方各版本下载 ...
- 环形缓冲区的应用ringbuffer
在嵌入式开发中离不开设备通信,而在通信中稳定性最高的莫过于环形缓冲区算法, 当读取速度大于写入速度时,在环形缓冲区的支持下不会丢掉任何一个字节(硬件问题除外). 在通信程序中,经常使用环形缓冲区作为数 ...