传送门

题目

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). () ()
Input

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

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的更多相关文章

  1. Codeforces GYM 100741A . Queries

    time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input output stan ...

  2. GYM 100741A Queries(树状数组)

    A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...

  3. GYM 100741A Queries

    传送门 题目大意: 一个长度为n的序列,q次三种操作 +p  r:下标为p的数+r -p r:下标为p的数-r s l r mod [L,R]中有多少数%m=mod,m已经给出 题解: 开十个树状数组 ...

  4. 实践 HTML5 的 CSS3 Media Queries

    先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...

  5. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  6. CSS3 Media Queries 实现响应式设计

    在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...

  7. 使用CSS3 Media Queries实现网页自适应

    原文来源:http://webdesignerwall.com 翻译:http://xinyo.org 当今银屏分辨率从 320px (iPhone)到 2560px (大屏显示器)或者更大.人们也不 ...

  8. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

  9. Media Queries 详解

    Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码:  <link href="css/reset.css" rel ...

随机推荐

  1. jdbc connection为什么放在webINF的lib里面

    jdbc connection为什么放在webINF的lib里面

  2. CANopenNode drvTemplate/CO_driver.h hacking

    /************************************************************************ * CANopenNode drvTemplate/ ...

  3. AJAX的最小单元

    $(function(){ $('#send').click(function(){ $.ajax({ type: "GET", url: "test.json" ...

  4. Linux sed 批量替换多个文件中的字符串【转载】

    原文网址:http://blog.sina.com.cn/s/blog_730edb930100qzz5.html     比如,要将目录/modules下面所有文件中的zhangsan都修改成lis ...

  5. Angular5学习笔记 - 配置NG-ZORRO(八)

    一.在项目中集成组件 $ cd PROJECT_NAME $ npm install ng-zorro-antd --save 二.在项目中导入组件 直接用下面的代码替换 /src/app/app.m ...

  6. mvn + idea jar包配置 错误记录

    1.创建项目方法和步骤,网上一搜一大把 2.主要出现了一个配置上的错误,java_home的配置 发现idea和eclipse有一个地方配置不同,就是java_home,在eclipse中 mvn的配 ...

  7. CodeForces - 510B Fox And Two Dots (bfs或dfs)

    B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. 天梯L2-001. 紧急救援(25分)

    L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国 ...

  9. 数据库:ubantu下MySQL数据库备份方法

    1.编辑/etc/crontab文件设定定时任务,在制定时间执行backup_databases.sh vi /etc/crontab # /etc/crontab: system-wide cron ...

  10. 自定义ASP.NET MVC Html辅助方法

    在ASP.NET MVC中,Html辅助方法给我们程序员带来很多方便,其重要性也就不言自明.有时候,我们不想重复地写一些HTML代码,或者MS没有提供我们想要的那个HTML标签的Html辅助方法,那么 ...