GYM100741 A Queries
0.25 s
64 MB
standard input
standard output
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.
3 4
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
2
3
1
【题解】
m棵线段树即可
树状数组就够了,但是我就是想写线段树练一练怎么了(唔)
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b)) inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = - x;
} const long long INF = 0x3f3f3f3f;
const long long MAXN = + ; long long n,m,num[MAXN],sum[][MAXN]; void build(long long o = , long long l = , long long r = n)
{
if(l == r)
{
sum[num[l]%m][o] += num[l];
return;
}
long long mid = (l + r) >> ;
build(o << , l, mid);
build(o << | , mid + , r);
for(register long long i = ;i <= ;++ i)
sum[i][o] = sum[i][o << ] + sum[i][o << | ];
return;
} void modify(long long p, long long shu, long long x, long long o = , long long l = , long long r = n)
{
if(l == p && l == r)
{
sum[shu % m][o] += x * shu;
return;
}
long long mid = (l + r) >> ;
if(mid >= p) modify(p, shu, x, o << , l, mid);
else modify(p, shu, x, o << | , mid + , r);
sum[shu % m][o] = sum[shu % m][o << ] + sum[shu % m][o << | ];
} long long ask(long long ll, long long rr, long long rk, long long o = , long long l = , long long r = n)
{
if(ll <= l && rr >= r) return sum[rk][o];
long long mid = (l + r) >> ;
long long ans = ;
if(mid >= ll) ans += ask(ll, rr, rk, o << , l, mid);
if(mid < rr) ans += ask(ll, rr, rk, o << | , mid + , r);
return ans;
} long long q;
char c; int main()
{
read(n), read(m);
for(register long long i = ;i <= n;++ i)
read(num[i]);
build();
read(q);
for(register long long i = ;i <= q;++ i)
{
long long tmp1,tmp2,tmp3;
scanf("%s", &c);
if(c == 's')
{
read(tmp1), read(tmp2), read(tmp3);
printf("%I64d\n", ask(tmp1, tmp2, tmp3));
}
else if(c == '+')
{
read(tmp1), read(tmp2);
modify(tmp1, num[tmp1], -);
num[tmp1] += tmp2;
modify(tmp1, num[tmp1], );
printf("%I64d\n", num[tmp1]);
}
else
{
read(tmp1), read(tmp2);
modify(tmp1, num[tmp1], -);
if(num[tmp1] - tmp2 >= ) num[tmp1] -= tmp2;
modify(tmp1, num[tmp1], );
printf("%I64d\n", num[tmp1]);
}
}
return ;
}
GYM100741 A
GYM100741 A Queries的更多相关文章
- 实践 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 ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- SPOJ GSS1 Can you answer these queries I[线段树]
Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...
- 【Codeforces710F】String Set Queries (强制在线)AC自动机 + 二进制分组
F. String Set Queries time limit per test:3 seconds memory limit per test:768 megabytes input:standa ...
随机推荐
- PAT甲级——A1081 Rational Sum
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- java代码优化写法(转摘)
本文源地址:https://blog.csdn.net/syc001/article/details/72841650 可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序 ...
- ch5 vlsms
Variabel Length Subnet Mask vlsms 较早的路由协议 ripv1 没有为子网准备的字段,子网信息会被丢失. 这意味着如果一个路由器运行着一个rip协议具有一个确定的子网掩 ...
- np一些基本操作1
##生成一个一维数组import numpy as np;nb7 = np.arange(0,100,2);print(nb7)print("======================== ...
- 数据挖掘-diabetes数据集分析-糖尿病病情预测_线性回归_最小平方回归
# coding: utf-8 # 利用 diabetes数据集来学习线性回归 # diabetes 是一个关于糖尿病的数据集, 该数据集包括442个病人的生理数据及一年以后的病情发展情况. # 数据 ...
- 2018-12-17-VisualStudio-使用新项目格式快速打出-Nuget-包
title author date CreateTime categories VisualStudio 使用新项目格式快速打出 Nuget 包 lindexi 2018-12-17 14:11:50 ...
- c语言string的函数
PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...
- 1.Spring【IOC】XML方式
1.下载开发包 http://repo.springsource.org/libs-release-local/org/springframework/spring 2.创建WEB工程,引入jar包 ...
- day20 作业
目录 今日作业 1.下面这段代码的输出结果将是什么?请解释. 2.多重继承的执行顺序,请解答以下输出结果是什么?并解释. 3.什么是新式类,什么是经典类,二者有什么区别?什么是深度优先,什么是广度优先 ...
- BZOJ 2683: 简单题(CDQ 分治)
题面 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: ...