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 ...
随机推荐
- Codeforces Round #599 (Div. 2)的简单题题解
难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...
- 深入浅出Mybatis系列(一)---Mybatis入门[转]
最近两年 springmvc + mybatis 的在这种搭配还是蛮火的,楼主我呢,也从来没真正去接触过mybatis, 趁近日得闲, 就去学习一下mybatis吧. 本次拟根据自己的学习进度,做一次 ...
- codeforces 1099E-Nice table
传送门:QAQQAQ 题意:给你一个矩阵只有AGCT,若对于每一个2*2的子矩阵中的四个字母互不相同,则称为这个矩阵是nice的,问至少变矩阵中的几个点可以使矩阵变nice 思路:没什么思路……就是大 ...
- C开发系列-指针
指针 通过一段简单的程序,引入指针的概念 #include <iostream> using namespace std; // changeValue函数的定义 void changeV ...
- SyntaxError: Non-ASCII character ‘xe5’ in file 04.py on line 4, but no encoding declared
出现问题的原因:程序中的编码错误,python默认是acii模式,没有支持utf8,代码中需要输出汉字,所以报错. 解决办法:源代码文件第一行添加:#coding:utf-8 -- coding: U ...
- Gym 100712H
Gym 100712Hhttps://vjudge.net/problem/195715/origin先缩点,再建立新图,然后跑两遍dfs求树上最长路 #include<iostream> ...
- [CQOI2011]放棋子--DP
题目描述: 输入格式 输入第一行为两个整数n, m, c,即行数.列数和棋子的颜色数.第二行包含c个正整数,即每个颜色的棋子数.所有颜色的棋子总数保证不超过nm.N,M<=30 C<=10 ...
- Laravel 错误处理
错误提示:cURL error 60: SSL certificate problem: unable to get local issuer certificate 解决方案:修改文件,重启队列即可 ...
- sqlserver 创建用户 sp_addlogin
创建新的 Microsoft® SQL Server™ 登录,使用户得以连接使用 SQL Server 身份验证的 SQL Server 实例. 语法: sp_addlogin [ @loginam ...
- PhpStorm中terminal窗口字体修改
在PhpStorm–File–Settings–Tools–Terminal中可以看到terminal调用的系统的cmd.exe程序 因此需要做的就是修改系统的cmd.exe中的字体,如下: CMD命 ...