详细解读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

相关文章
|
消息中间件 监控 数据可视化
一个基于.Net Core 开源的物联网基础平台
一个基于 .Net 6.0 使用C#语言编写的以实现可见与不可见的物理设备数字孪生的物联网平台。用于数据的收集、处理、可视化、设备管理、设备预警、报警的平台。
773 20
一个基于.Net Core 开源的物联网基础平台
|
存储 运维 Serverless
函数计算产品使用问题之如何规避因提高CPU规格而导致的内存规格不必要增加的问题
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
217 0
Vue3+Vite+Pinia+Naive后台管理系统搭建之四:Naive UI 组件库的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之四:Naive UI 组件库的安装和使用
980 1
【Azure Function】Function App启动时出现 Failed to open local port 4001 错误,这是什么情况呢?
【Azure Function】Function App启动时出现 Failed to open local port 4001 错误,这是什么情况呢?
186 0
|
测试技术 UED
软件测试中的自动化与手动测试:一种互补策略
【5月更文挑战第31天】随着软件开发行业的迅速发展,软件测试已经成为确保产品质量和用户体验的关键环节。本文将探讨自动化测试和手动测试在软件测试中的作用,以及如何有效地结合这两种方法以提高测试效率和质量。我们将分析各自的优缺点,并提供一些实用的建议,帮助读者在实际工作中更好地应用这两种测试方法。
如何验证企业微信生成的token是否有效?
如何验证企业微信生成的token是否有效?
268 0
|
编译器 Linux C语言
【C++】C++入门详解 I【C++入门 这一篇文章就够了】(上)
【C++】C++入门详解 I【C++入门 这一篇文章就够了】
165 1
|
Oracle 关系型数据库
ORA-01779: 无法修改与非键值保存表对应的列
ORA-01779: 无法修改与非键值保存表对应的列
ORA-01779: 无法修改与非键值保存表对应的列
|
运维 Kubernetes 中间件
开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口
开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口
367 0
开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口