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 ...
随机推荐
- vagrant网站中box下载方法
假设需要下载Laravel/homestead这个包. 首先定位到地址:https://app.vagrantup.com/laravel/boxes/homestead/versions/8.0.0 ...
- IOS学习笔记57--IOS7状态栏适配(二)
上一遍文章通过XIB的设置达到了状态栏和view重合的问题,这一篇我们讲一讲网传的修改window frame方法. 先上步骤: 第一:在appdeletage里面 添加如下代码: if ( ...
- 用docker部署zabbix
官方文档 https://www.zabbix.com/documentation/3.4/zh/manual/installation/containers 1 启动一个空的Mysql服务器实例 d ...
- 在熟练使用2B铅笔前,请不要打开Axure
在互联网产品领域,Axure已成为产品经理.产品设计师以及交互设计师的必备工具,从某种程度讲,Axure帮助我们建立低保真模型,便于与用户的需求验证,也帮助我们构思交互细节,使前端和开发人员更容易理解 ...
- Python学习day08-python进阶(2)-内置方法
Python学习day08-python进阶(2)-内置方法 列表数据类型内置方法 作用 描述多个值,比如爱好 定义方法 xxxxxxxxxx 2 1 hobby_list ...
- APPScan手动探索
- 设置listContrl中指定行的颜色
在MFC中 自己通过手动拖放CListCtrl控件来制作自己的表格: 目的: 将指定item的行更该颜色: 步骤: 1,在窗口中拖放CListCtrl控件, 单击右键 创建控件对象: CListCtr ...
- property中ref、value、name的区别
转载: 版权声明:本文为CSDN博主「qq_36098284」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net ...
- Flannel部署
目录 Flannel CNI集成 配置Docker使用Flannel 1.为Flannel生成证书 [root@linux-node1 ~]# cd /usr/local/src/ssl/ [root ...
- TZ_03_mybatis的xml开发
1.通过Student.xml编写sql来操作数据库 1>insert语句插入后返回主键 加入标签useGeneratedKeys=“true” keyProperty=“oid” 中 keyP ...