传送门

题目

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. 数据库迁移到Azure SQL Database用户无法登陆的问题

    业务情景:数据库是运维迁移上去的,好像使用了一个工具叫做Microsoft Data Migration Assistant,迁移之后,我的web应用无法连接数据库. 迁移之后的数据库内有User,但 ...

  2. html 常用代码块

    解决外边框不计入div尺寸的代码-moz-box-sizing: border-box;box-sizing: border-box;-webkit-box-sizing: border-box; 手 ...

  3. idea 创建maven工程(入门)

    转:http://blog.csdn.net/qq_32588349/article/details/51461182 1. 下载Maven 官方地址:http://maven.apache.org/ ...

  4. 六、python沉淀之路--int str list tuple dict 重点总结

    一.数字int(..)二.字符串replace/find/join/strip/startswith/split/upper/lower/formattempalte = "i am {na ...

  5. Eclipse之Web工程探究以及格式化

    1. 关于部署 只要配置了Web Deployment Assembly,可以不需要手工拷贝引用jar到/WEB-INF/lib里面了,之前失败是因为引用工程的output路径有问题导致的,修改完成后 ...

  6. Python 修改ha配置文件

    任务要求: 1.用户输入字符串 {"backend": "test.oldboy.org","record":{"server&q ...

  7. Hibernate检索方式(转载)

    我们在项目应用中对数据进行最多的操作就是查询,数据的查询在所有ORM框架中也占有极其重要的地位. 那么,如何利用Hibernate查询数据呢?Hibernate为我们提供了多种数据查询的方式,又称为H ...

  8. mysql 自增id

    在开发的时候遇到了 自增id变成2147483647 莫名其妙 然后发现是自己没把自增id改为 无符号的原因 把无符号勾上就ok了

  9. 蓝桥杯 算法训练 ALGO-156 表达式计算

    算法训练 表达式计算   时间限制:1.0s   内存限制:256.0MB 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输 ...

  10. java代码实现通讯录实例,我不知道这有什么用。,

    运行显示: Friend:zl,Address:武大樱花美Colleagues:蔡依林,Department:麻城市人民政府 题目: 1.任务描述 完善上面通讯录名片的例子. 2.技能要点 掌握类继承 ...