A. Queries

time limit per test

0.25 seconds

memory limit per test

64 megabytes

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 题意:
1个长度为n 的序列,q 次三种操作:
+ p r: 下标为p 的数加r.
- p r: 下表为p 的数减r.
s l r mod: 询问在模m(提前给出) 意义下[l,r] 之中有多少个数等于mod.

/*
10个树状数组 分别记录mod m为几的数个数
加减号做 询问时输出模数所在树状数组l~r个数即可
*/
#include<bits/stdc++.h> #define N 10010
#define ll long long using namespace std; struct BIT
{
ll c[N];
int n; int lowbit(int x){return x&(-x);} void modify(int x,ll y){for(; x<=n; x+=lowbit(x)) c[x]+=y;} ll query(int x)
{
ll ret=;
for(; x; x-=lowbit(x)) ret+=c[x];
return ret;
} ll query(int l,int r){return query(r)-query(l-);}
} bit[]; int n,m,T;
ll a[N]; int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<m;i++) bit[i].n=n;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
bit[a[i]%m].modify(i,a[i]);
}
scanf("%d",&T);
while(T--)
{
int x,y,z;
char opt[];
scanf("%s%d%d",opt,&x,&y);
if(opt[]=='+')
{
bit[a[x]%m].modify(x,-a[x]);
a[x]+=y;
bit[a[x]%m].modify(x,a[x]);
printf("%lld\n",a[x]);
}
if(opt[]=='-')
{
if(a[x]<y) printf("%lld\n",a[x]);
else
{
bit[a[x]%m].modify(x,-a[x]);
a[x]-=y;
bit[a[x]%m].modify(x,a[x]);
printf("%lld\n",a[x]);
}
}
if(opt[]=='s')
{
scanf("%d",&z);
printf("%lld\n",bit[z].query(x,y));
}
}
return ;
}
 

GYM 100741A Queries(树状数组)的更多相关文章

  1. Codeforces 369E Valera and Queries --树状数组+离线操作

    题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...

  2. Gym - 101755G Underpalindromity (树状数组)

    Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...

  3. CF Gym 100463A (树状数组求逆序数)

    题意:给你一个序列,和标准序列连线,求交点数. 题解:就是求逆序对个数,用树状数组优化就行了.具体过程就是按照顺序往树状数组了插点(根据点的大小),因为第i大的点应该排在第i位,插进去的时候他前面本该 ...

  4. Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...

  5. GYM 101889F(树状数组)

    bit扫描坐标套路题,注意有重复的点,莽WA了. const int maxn = 1e5 + 5; struct node { ll B, F, D; bool operator < (con ...

  6. gym 100589A queries on the Tree 树状数组 + 分块

    题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...

  7. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  8. Gym 101908C - Pizza Cutter - [树状数组]

    题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...

  9. Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp

    Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...

随机推荐

  1. phpCURL抓取网页内容

    参考代码1:<?php // 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, " ...

  2. Style在Android中的继承关系

    Style在Android中的继承关系 Android的Styles(样式)和Themes(主题)非常类似Web开发里的CSS,方便开发者将页面内容和布局呈现分开.Style和Theme在Androi ...

  3. ionic 创建某个文件下的page

    ionic g page 文件名 --pagesDir src/pages/about

  4. centos安装指定mysql

    mysql下载地址:http://repo.mysql.com/ nginx下载地址 我下载是这个 http://nginx.org/packages/centos/7/noarch/RPMS/ngi ...

  5. 《Mysql - 到底可不可以使用 Join ?》

    一:Join 的问题? - 在实际生产中,使用 join 一般会集中在以下两类: - DBA 不让使用 Join ,使用 Join 会有什么问题呢? - 如果有两个大小不同的表做 join,应该用哪个 ...

  6. Vi/Vim基本用法

    Vi/Vim是Linux中一款功能强大的编辑器,vi是Visual Interface的缩写,即可视化接口,vim是vi iMprove的缩写,即 vi的增强版(具有语法着色功能).它在Linux上的 ...

  7. vue轮播插件vue-awesome-swiper

    https://surmon-china.github.io/vue-awesome-swiper/ 第一步安装 npm install vue-awesome-swiper --save 第二部在m ...

  8. poj 2031

    #include<stdio.h> #include<math.h> #include<stdlib.h> #define N 200 double co(doub ...

  9. [bzoj3061][Usaco13Feb]Partitioning the Farm_动态规划_状压dp

    Partitioning the Farm bzoj-3061 Usaco13Feb 题目大意:给定一个n*n的方格图,用k条贯穿方格图的直线将整个方格图分割,使得每一块的权值和的最大值最小. 注释: ...

  10. MySQL的各种网络IO超时的用法和实现

    2016-04-06 赵伟 数据库开发者 客户端C API 在C API中调用mysql_options()来设置mysql_init() 所创建的连接对象的属性,使用这三个选项可以设置连接超时和读写 ...