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 ...
随机推荐
- Quartz实战
https://my.oschina.net/yinxiaoling/blog/542336?fromerr=s3ko7u33 Quartz实战 > 一.内存型(1) <bean name ...
- httpd-vhosts.conf
## VirtualHost example:# Almost any Apache directive may go into a VirtualHost container.# The first ...
- 简述cookie ,localStrage,sessionStorage的区别?
1.cookie: 是一个回话跟踪技术,信息存储在用户硬盘,可以做全局变量. 什么是会话:用户进入网站,开始浏览到结束的这样的一个过程,称为一次会话. 会话跟踪技术:浏览器和服务器之间进行多次请求数据 ...
- JavaScript day2(变量)
变量(variable) 允许计算机以一种动态的形式来存储和操作数据,通过操作指向数据的指针而不是数据本身来避免了内存泄露,变量(Variable)的名字可以由数字.字母.$ 或者 _组成,但是不能包 ...
- C++ Primer(第4版)-学习笔记-第1部分:基本语言
第1章 快速入门 每个C++程序都包含一个或多个函数,而且必须有一个命名为main. main函数是唯一被操作系统显式调用的函数,main函数的返回值必须是int或者void(无返回值) 函数体是函 ...
- models中,字段参数limit_choices_to的用法
这里,在使用 ModelForm 渲染前端页面的前提下,对于 models 中的 ManyToManyField 类型字段会在 ModelForm 中被转化为 ModelMultipleChoiceF ...
- PAT 1057. Stack
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- 在docker上构建tomcat容器
1.查看docker上的镜像 [root@holly ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.6 73829d7b ...
- BZOJ 4032 Luogu P4112 [HEOI2015]最短不公共子串 (DP、后缀自动机)
这其实是道水题... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4032 (luogu)https://www.luog ...
- time库
简介 返回系统当前时间戳(正常的生活时间) 返回格林威治时间戳对应的struct_time对象 本地时间的struct_time对象 当前时间戳对应的易读格式字符串时间(周几,月份,号数,时,分,秒, ...