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 ...
随机推荐
- Django视图之FBV与CBV
一. CBV与FBV CBV:Class Based View FBV:Function Based View 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的,那就是C ...
- nginx reload的原理
nginx启动时,会启动两个进程: 一个是Master进程和worker进程.改变配置后nginx做的事1)改变了nginx配置之后,HUP signal的信号需要发送给主进程.2)主进程首先会检测新 ...
- WDS部署基础知识:使用WDS捕获与应用映像(使用WDS定制系统和应用)
WDS部署基础知识:使用WDS捕获与应用映像(使用WDS定制系统和应用) Win7部署基础知识(8):使用WDS捕获与应用映像 一.添加映像组 使用WDS捕获映像时,会将映像加载到WDS服务器的映像 ...
- C#DataGridView格式化显示单元格的内容
今天又发现了一个很有用的东西,DataGridView的CellFormating事件 经常从数据库查到的原始数据需要经过转换之后显示在客户端,比如性别,“1”显示“男”,“0”显示“女”,为此经常将 ...
- linux 下文件上传的两种工具(XFTP5和Putty之pscp)方式
一.使用XFTP(,需要先在LINUX上安装启用FTP服务) 然后,在WINDOWS上启动XFPT6客户端,将下载的文件上传至LINUX 指定目录: 二.使用PUTTY软件安装目录下的PSCP命令 1 ...
- C++ unsigned long 转化为 unsigned char*
C++ Code 123456789101112131415161718 unsigned long lFileLen = 1000; unsigned char *ucFileLenFlag; ...
- MYSQL—第二部分(Linux版本的安装和数据表的操作)
Linux版本的安装(过于简单了) 安装: ? 1 yum install mysql-server 服务端启动 ? 1 mysql.server start 客户端连接 ? 1 2 3 4 5 6 ...
- spark教程(12)-生态与原理
spark 是目前非常流行的大数据计算框架. spark 生态 Spark core:包含 spark 的基本功能,定义了 RDD 的 API,其他 spark 库都基于 RDD 和 spark co ...
- python 写接口供外部调用
.py: import requests import urllib2 import commands import subprocess def check(): status, msg = com ...
- 安装gitlab ce
切换到root用户,安装相关依赖 yum install curl policycoreutils openssh-server openssh-clients service sshd restar ...