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 ...
随机推荐
- AFO之后……
先上游记:@ 据说有一种东西叫狗屁不通文章生成器:@
- 使用WCF上传文件
在WCF没出现之前,我一直使用用WebService来上传文件,我不知道别人为什么要这么做,因为我们的文件服务器和网站后台和网站前台都不在同一个机器,操作人员觉得用FTP传文件太麻 ...
- swoole是如何实现任务定时自动化调度的?
https://www.muzilong.cn/article/117 开发环境 环境:lnmp下进行试验. 框架:laravel5 问题描述 这几天做银行对帐接口时,踩了一个坑,具体需求大致描述一下 ...
- TableView之表头、表尾,区头、区尾!
一.UITableView的UITableViewStyle 样式分为UITableViewStylePlain和UITableViewStyleGrouped两种: plain样式下区头和区尾是悬浮 ...
- PHP面向对象魔术方法之__get 和 __set函数
l 基本的介绍 (1) 当我们去使用不可以访问的属性时,系统就会调用__get方法. (2) 不可以访问的属性指的是(1 . 该属性不存在 2. 直接访问了protected或者private属性) ...
- JS基础语法之DOM02(事件)
1.常用事件 1.onclick 单击 应用场景:为按钮绑定 2.ondbclick 双击 3.onfocus 获得焦点 4.onblur 失去焦点 应用场景:用于表单验证,用户离开某个输入框时, ...
- 在HBase之上构建SQL引擎
- 细说WPF自定义路由事件
WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 W ...
- 初识css3 3d动画效果
(先看我博客右上角的3d盒子动画效果,目前没做兼容处理,最好最新的chrome看)无意间看到网上css3写的3d动画效果,实在炫酷,以前理解为需要js去计算去写,没想到css直接可以实现.于是开始研究 ...
- 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...