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 ...
随机推荐
- HCL试验八
pc1:配置静态ip地址,掩码,网关 路由器R1:配置ip地址192.168.1.254 24;配置dhcp int gi 0/0 ip add 192.168.1.254 24 qu dhcp en ...
- 中间消息件之 Solace(一) Solace的简介
什么是Solace? Solace是一家加拿大注册的公司,总部设在安大略省渥太华. 成立于2001年. 这家公司一直致力于获取信息的艺术和科学,并提供世界上最灵活,最强大的数据移动平台. 背景 在信息 ...
- deepin系统换软件下载源&商店卡死刷新空白问题解决
deepin换源方法 1. deepin默认的软件下载源网速很慢,所以我们可以自己手动换软件下载源,方法如下: 1.1. 任意地方,右键选择在此打开终端,(快捷键:Ctrl+Alt+T),输入sudo ...
- 编写java 程序与Linux进行远程连接并运行linux下的脚本
我这里是通过连接到centos6.5的大数据集群的主节点,并通过运行hadoop的启动脚本来启动hadoop 本人采用的是SSH的方式连接 通过创建maven项目来编写代码,在编写代码之前需要先导入架 ...
- Python3图片处理头像
一. 简介: Python3图片处理头像右上角类似QQ,微信右上角未读信息效果,其实没有实质作用,只是简单练习. 1. 环境: python3.5 random模块:生成随机数 PIL模块:图像处理模 ...
- Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- ssl安全验证
#ssl验证 r=requests.get('https://www.12306.cn',verify=False) print(r.content.decode('utf-8')) 结果:
- 有关最短路上的第k小/大值的总结
1.USACO08JAN Telephone Lines 题面 由于问的是最大值最小,所以二分加验证就好了 比较显然的,题干问的是第k+1长的路最短: 那么二分答案是正确的方向: 但是怎么验证? 我 ...
- IDEA 修改JavaWeb的访问路径
问题描述 对于我这个刚刚使用IDEA不久的新手来说,能够正常运行就不错了,不过到了后面,可能会觉得IDEA给你分配的默认访问路径很不顺手,比如访问的时候需要通过: http://loca ...
- Memcached安装 常用指令
Memcached 源码安装 # 安装依赖yum install -y gcc gcc-c++ automake autoconf make cmake libevent-devel.x86_64# ...