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 ...
随机推荐
- hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)
题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...
- 2018-2019-2 20165210《网络对抗技术》Exp6 信息搜集与漏洞扫描
2018-2019-2 20165210<网络对抗技术>Exp6 信息搜集与漏洞扫描 一.实验目标: 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实验内容: 各种搜索技巧的应用 G ...
- RESTful 组件
1. CBV FBV: url("index/",index) # index(request) url("index/(\d+)",index) # inde ...
- mysql分表和分区实际应用简介
一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具体请看mysql分表的3种方法 什么是分区,分区呢就是把一张表的数据分成N多个区块,这些区块可以在同一个磁盘 ...
- 1.react的基础知识
React 的基础之:JSX 语法 react 使用 JSX 语法,js 代码中可以写 HTML 代码. let myTitle = <h1>Hello, World!</h1> ...
- 前用户sudo免密码
Ubuntu 设置当前用户sudo免密码 方法1 # 备份 /etc/sudoers sudo cp /etc/sudoers . #打开 /etc/sudoers sudo visudo # 在 ...
- 记录启用HTTPS的全过程
因为 https 采用 ssl 加密,所以部署 https 时需要申请证书,证书的作用就是对浏览器和Web服务器双方的身份验证. 步骤1:申请证书 我们采用Let's Encrypt签发的免费证书,虽 ...
- DCloud-HBulder:杂项
ylbtech-DCloud-HBulder:杂项 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 ...
- MySQL mysqldump 备份脚本(按照db.sql)
mysqldump逻辑备份,按照db.sql文件区分,并压缩 #! /bin/bash #35 02 * * * mysql /data/mysqldata/scripts/mysqldump_per ...
- iphone配置实用工具iPhone Configuration Utility
下载地址 http://support.apple.com/kb/DL1466 安装完毕后,在设备->控制台,可以很方便看到报错信息