Gym-10071A-Queries(树状数组)
链接:
https://vjudge.net/problem/Gym-100741A
题意:
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). () ()
思路:
看半天没看懂题.
建10个树状数组即可, 对模m的每种情况分别统计.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e4+10;
LL A[MAXN], C[15][MAXN];
int n, m, q;
int Lowbit(int x)
{
return x&(-x);
}
void Add(int pos, int mod, LL val)
{
while (pos <= n)
{
C[mod][pos] += val;
pos += Lowbit(pos);
}
}
LL Query(int pos, int mod)
{
LL ans = 0;
while (pos > 0)
{
ans += C[mod][pos];
pos -= Lowbit(pos);
}
return ans;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1;i <= n;i++)
{
scanf("%lld", &A[i]);
Add(i, A[i]%m, A[i]);
}
scanf("%d", &q);
char opt[5];
int l, r, mod;
while (q--)
{
scanf("%s", opt);
if (opt[0] == 's')
{
scanf("%d%d%d", &l, &r, &mod);
printf("%lld\n", Query(r, mod)-Query(l-1, mod));
}
else if (opt[0] == '+')
{
scanf("%d%d", &l, &r);
Add(l, A[l]%m, -A[l]);
A[l] += r;
Add(l, A[l]%m, A[l]);
printf("%lld\n", A[l]);
}
else
{
scanf("%d%d", &l, &r);
Add(l, A[l]%m, -A[l]);
if (r <= A[l])
A[l] -= r;
Add(l, A[l]%m, A[l]);
printf("%lld\n", A[l]);
}
}
return 0;
}
Gym-10071A-Queries(树状数组)的更多相关文章
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- 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 ...
随机推荐
- PJzhang:查看你的微信、QQ、新浪微博都授权给过谁
猫宁!!! 个人信息的保护,需要时刻关注,不能有丝毫懈怠. 保护个人信息,有两个方向,一个是减少个人信息外泄,另一个是上传大量鱼龙杂的个人信息,难以分辨,混淆视听. 这里不谈第二个, ...
- Oracle 行列转换公式
1.行转列 SELECT STU_NAME,TERM,ZHANBI,COURSE_MARK FROM (SELECT '罗飞' STU_NAME, '2001-2002' TERM, ' 微积分, ' ...
- 深入理解分布式系统的2PC和3PC
协调者 在分布式系统中,每一个机器节点虽然都能明确的知道自己执行的事务是成功还是失败,但是却无法知道其他分布式节点的事务执行情况.因此,当一个事务要跨越多个分布式节点的时候(比如,淘宝下单流程,下单系 ...
- Storm本地启动拓扑报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/storm/topology/IRichSpout
问题描述: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/storm/topology ...
- 在Ubuntu上安装Hbase
1.下载hbase2.0.5 解压缩并改名为hbase目录,放到/usr/local下(注意权限) sudo mv ~/hbase-2.0.5 /usr/local/hbase 2.修改目录所有者 / ...
- Java基础开篇
我是一个2019毕业的非计算机的毕业生,从大二开始喜欢上Java直到现在一直都在学习,Brid从小就对计算机感兴趣,可惜高中的时候不懂事,没有规划未来,考上了一所专科学院,然后大一并不能转专业,现在毕 ...
- UML表示类图和对象图
类图表示不同的实体(人.事物和数据)如何彼此相关,显示了系统的静态结构.类图可用于表示逻辑类,逻辑类通常就是业务人员所谈及的事物种类,比如摇滚乐队.CD.广播剧,或者贷款.住房抵押.汽车信贷及利率的抽 ...
- 爬取糗事百科热门段子的数据并保存到本地,xpath的使用
和之前的爬虫类博客的爬取思路基本一致: 构造url_list,因为糗事百科的热门栏目默认是13页,所以这个就简单了 遍历发送请求获取响应 提取数据,这里用的是xpath提取,用的是Python的第三方 ...
- 线性基求交(线段树)--牛客第四场(xor)
题意: 给你n个基,q个询问,每个询问问你能不能 l~r 的所有基都能表示 x . 思路: 建一颗线性基的线段树,up就是求交的过程,按照线段树区间查询的方法进行check就可以了. #define ...
- MySQL创表--分页--自关联--
创建表book create table t_book( id int unsigned auto_increment primary key, bookName varchar(255) defau ...