A. Queries
time limit per test

0.25 s

memory limit per test

64 MB

input

standard input

output

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). () ()
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.

Examples
Input
3 4
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
Output
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的更多相关文章

  1. 实践 HTML5 的 CSS3 Media Queries

    先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...

  2. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  3. CSS3 Media Queries 实现响应式设计

    在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...

  4. 使用CSS3 Media Queries实现网页自适应

    原文来源:http://webdesignerwall.com 翻译:http://xinyo.org 当今银屏分辨率从 320px (iPhone)到 2560px (大屏显示器)或者更大.人们也不 ...

  5. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

  6. Media Queries 详解

    Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码:  <link href="css/reset.css" rel ...

  7. 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 ...

  8. 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 ...

  9. 【Codeforces710F】String Set Queries (强制在线)AC自动机 + 二进制分组

    F. String Set Queries time limit per test:3 seconds memory limit per test:768 megabytes input:standa ...

随机推荐

  1. springboot+mybatis+达梦数据库

    准备工作: 首先,安装达梦6数据库.安装完之后如下建表 然后,很重要的一点(写法一定要这样写,否则无限报错) 达梦数据库查表方式: select  *  from    "库名". ...

  2. php数据结构课程---6、常见排序有哪些

    php数据结构课程---6.常见排序有哪些 一.总结 一句话总结: 冒泡排序(Bubble sort):依次交换 选择排序 ( Selection Sort ):在未排序序列中找到最小(大)元素,依次 ...

  3. 转-VS2010常用功能使用介绍

    原文链接:http://www.jizhuomi.com/software/27.html 1.几个基础概念 在讲VS2010之前先讲下程序开发过程中的几个基本概念:源程序.目标程序和翻译程序. 源程 ...

  4. 《数据结构与算法分析——C语言描述》ADT实现(NO.04) : AVL树(AVL-Tree)

    上次我们已经实现了普通的二叉查找树.利用二叉查找树,可以用O(logN)高度的树状结构存储和查找数据,提高了存储和查找的效率. 然而,考虑一种极端情形:依次插入1,2,3,4,5,6,7,8,9九个元 ...

  5. reac-native + typescript 的环境搭建

    一. RN-TS环境搭建 . 安装RN脚手架 yarn add create-react-native-app -g yarn global add typescript . 创建项目文件夹 crea ...

  6. mybatis 中 if else 用法

    mybaits 中没有 else 要用 chose when otherwise 代替 下面就是MyBatis中的if....else...表示方法 <choose> <when t ...

  7. JavaWeb工程中url地址的写法

    两种url地址: 1. "/"给服务器使用, 代表web工程根路径(webroot)2. "/"给浏览器使用, 代表tomcat 目录下的webapps文件夹 ...

  8. 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题

    导语 发布app后,开发者最头疼的问题就是如何解决交付后的用户侧问题的还原和定位,是业界缺乏一整套系统的解决方案的空白领域,闲鱼技术团队结合自己业务痛点在flutter上提出一套全新的技术思路解决这个 ...

  9. 仓库盘点功能-ThinkPHP_学习随笔

    public function check() { $db = M('Bookinfo'); $region = I('post.region'); $c = $db -> count(); f ...

  10. String和StringBuffer的区别;字符串的一些基本方法

    String 和 StringBuffer区别 字符串广泛应用 在Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串. 需要注意的是,String的 ...