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 ...
随机推荐
- 面试题41:和为s的两个数字VS和为s的连续正数序列
题目一:输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和为s,输出任意一对即可. vector<int> findNumberwWithSu ...
- UVA 11291 Smeech
[来源]https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- CodeForces - 154C:Double Profiles (hash+排序)
You have been offered a job in a company developing a large social network. Your first task is conne ...
- 第四篇 PHP的成长路线
学PHP开发这么久,进步不大,个人进行了分析.认为是我自己没有设定目标,就是对于自己要学成什么样没有清晰的认识. 今天特别了解了一下PHP的成长参考路线,以便自己以后迷失方向.PHP主要应该基于MYS ...
- WPF中DataGrid控件的过滤(Filter)性能分析及优化
DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程: 用户输入过滤条件 ...
- 拨打电话demo
- (IBAction)btnClick:(id)sender { UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil d ...
- IIS调用批处理权限的处理[转]
最近公司希望将Windows 2003升级为Windows 2008,做完安全设置后发现.net调用批处理拒绝访问的情况.网上很多说更改应用程序池的权限,建议不需要修改该权限,我这里强烈建议使用默认的 ...
- Springboot监控之一:SpringBoot四大神器之Actuator之3-springBoot的监控和管理--指标说明
Spring Boot包含很多其他的特性,它们可以帮你监控和管理发布到生产环境的应用.你可以选择使用HTTP端点,JMX或远程shell(SSH或Telnet)来管理和监控应用.审计(Auditing ...
- java.nio.ByteBuffer中flip、rewind、clear方法的区别
对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...
- eclipse项目中将普通文件夹转化成资源文件夹
1.点选该文件夹 2.右键属性Properties 3.选择属性:Build Path 4.点选属性:Use as Source Folder ......等待变成资源文件夹 参考更详细的: ecl ...