详细解读100741AQueries

简介: 详细解读100741AQueries

传送门

题目

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

#include

#include

#include[span style="color: rgba(0, 0, 255, 1)">string

#include

#include

#include

#include

#include

#include

#include

#include[span style="color: rgba(0, 0, 255, 1)">set

#include

#include

using namespace std;

long long n,m,sum,block,all【11000】【20】,beg【11000】,a【11000】,q,r【11000】,l【11000】;

inline long long read(){

long long x=0,f=1;char s=getchar();

while(s[span style="color: rgba(128, 0, 0, 1)">'0'||s>'9'){if(s=='-')f=-1;s=getchar();}

while(s>='0's<='9'){x=(x[3)+(x[1)+(s-'0');s=getchar();}

return x*f;

}//代码效果参考:http://www.ezhiqi.com/bx/art_3125.html

inline void init(){

long long i,j,k;

block=sqrt(n);

sum=n%block==0?n/block:n/block+1;

for(i=1;i<=sum;i++)

l【i】=r【i-1】+1,r【i】=r【i-1】+block;

r【sum】=n;

for(i=1;i<=n;i++){

beg【i】=(i-1)/block+1;

all【beg【i】】【a【i】%m】+=a【i】;

}

}

inline void go(long long x,long long y){

if(a【x】+y[span style="color: rgba(128, 0, 128, 1)">0){

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=0;

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】+1;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=1;i<=n;i++)a【i】=read();

init();

q=read();

for(i=1;i<=q;i++){

char c;

cinc;

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 0;

}//代码效果参考:http://www.ezhiqi.com/zx/art_2404.html

相关文章
|
4天前
|
JavaScript 前端开发 Java
驼峰命名规范及其在代码可读性中的重要性
驼峰命名规范及其在代码可读性中的重要性
|
4天前
|
机器学习/深度学习 人工智能 算法
人工智能平台PAI产品使用合集之在执行shade打包后遇到“类找不到”的错误,是什么原因
阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。
|
4天前
|
负载均衡 Java 开发者
Spring Cloud微服务架构中的配置管理与服务发现
Spring Cloud微服务架构中的配置管理与服务发现
|
4天前
|
PHP 开发者
PHP中的异常处理与错误调试
【6月更文挑战第29天】在PHP开发过程中,异常处理和错误调试是保证代码质量与应用稳定性的重要环节。本文将深入探讨PHP的异常处理机制,介绍如何通过try-catch语句捕获异常,以及自定义异常类的创建和使用。同时,文章还将讨论PHP错误类型、错误日志记录和有效的错误调试方法,帮助开发者提高问题诊断与解决能力。
|
4天前
|
运维 Serverless API
Serverless 应用引擎产品使用合集之通过 API 调用 /tagger/v1/interrogate 时,出现unsupported protocol scheme "" 错误,如何处理
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
4天前
|
机器学习/深度学习 人工智能 对象存储
人工智能平台PAI产品使用合集之大文件如何下载下来
阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。
|
4天前
|
机器学习/深度学习 人工智能 前端开发
人工智能平台PAI产品使用合集之创建了实时特征视图,里面的数据是通过什么传入的
阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。
|
4天前
|
算法 前端开发 计算机视觉
一篇文章讲明白Harris角点
一篇文章讲明白Harris角点
12 3
|
4天前
|
Java API 网络架构
Spring Cloud Gateway的高级配置与实践
Spring Cloud Gateway的高级配置与实践
|
4天前
常见的并联谐振应用案例
并联谐振电路在音频处理中提升音质,振动检测中评估设备状态,电磁波检测中测量频率,电力电子及无功补偿中的优化,通信信号的滤波与放大,无线电接收发射,及家用电器如电视、洗衣机的信号控制。应用广泛,从通信到家电,发挥着关键作用。

热门文章

最新文章