GYM 100741A Queries(树状数组)
A. Queries
0.25 seconds
64 megabytes
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 题意:
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(树状数组)的更多相关文章
- Codeforces 369E Valera and Queries --树状数组+离线操作
题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...
- Gym - 101755G Underpalindromity (树状数组)
Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...
- CF Gym 100463A (树状数组求逆序数)
题意:给你一个序列,和标准序列连线,求交点数. 题解:就是求逆序对个数,用树状数组优化就行了.具体过程就是按照顺序往树状数组了插点(根据点的大小),因为第i大的点应该排在第i位,插进去的时候他前面本该 ...
- Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理
题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...
- GYM 101889F(树状数组)
bit扫描坐标套路题,注意有重复的点,莽WA了. const int maxn = 1e5 + 5; struct node { ll B, F, D; bool operator < (con ...
- gym 100589A queries on the Tree 树状数组 + 分块
题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- Gym 101908C - Pizza Cutter - [树状数组]
题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...
- Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp
Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...
随机推荐
- cesium的学习
一.学习资料:http://cesiumjs.org/tutorials.html,看完6个教程后对图层加载.控件控制开关.地形数据叠加.模型添加.相机控制.图形绘制有一点了解.这也是cesium的主 ...
- 测试edit中数据是否合法
void XyModal::OnEnKillfocusEdit1() { // TODO: 在此添加控件通知处理程序代码 CString cText; GetDlgItemText(IDC_EDIT1 ...
- nginx设置绑定解析实现二级域名多域名
apache(httpd)配置多个二级域名看这个链接:https://www.cnblogs.com/Crazy-Liu/p/10879928.html 网站的目录结构为/home/www├── bb ...
- bootstrap table 生成的表格里动态添加HTML元素按钮,JS中添加点击事件,点击没反应---解决办法
bootstraptable中onExpandRow属性---js 方法添加的 html代码,然后给这代码里面的 元素 添加 事件,却获取不该元素.(称之为未来元素),由于是未来的 所以现在没有这个 ...
- 发现是在IE6-IE9下,下列元素table,thead,tfoot,tbody,tr,col,colgroup,html,title,style,frameset的innerHTML属性是只读的
table ID="zhutiTable" html2="<tr></tr>": 的数据 setTableInnerHTML(docu ...
- 【js】数组置空的其他方式及使用场景
数组在js中属于引用型类型. var arr = [1, 2, 3]; a = []; 通常使用以上方式.如果使用场景必须使用方法置空, 可以采用arr.length = 0; 或者使用a.splic ...
- 51.percentiles rank以及网站访问时延SLA统计
主要知识点: percentile_ranks的用法 percentile的优化 一.percentile_ranks的用法 SLA:就是所提供的服务的标准. 比如一个网站的提供的访问延时的S ...
- win10如何进入安全模式的几种方法
首先,说一下安全模式的作用: 安全模式, 用途有很多,常见的作用有以下几点 1. 电脑可能由于安装了某些驱动或者软件,不兼容导致电脑启动不了,可以进入安全模式卸载 2. 电脑中病毒之后,可以进入安全模 ...
- emacs 定制进缩风格
纵览 emacs 文档中描述,进缩风格实现只需要两步:第一步,根据内容与上下文找到对应的进缩风格的类别:第二步,依据进缩风格决定的表达式锚点的进缩偏移.下面我们对 cc-mode 风格定制加以说明. ...
- SqlServer转换为Mysql(mss2sql)
SqlServer转换为Mysql(mss2sql)工具 http://pan.baidu.com/s/1c2d8R8O 参考链接: http://www.cnblogs.com/angestudy/ ...