Queries Gym - 100741A - 树状数组
给定 \(n\) 和 \(m\),对于 \(n\) 个数字 \(a_i\),进行下列三种操作:
(1) + p r: 将 p 位置的元素加上 r, 输出此时 p 位置的值;
(2) - p r : 将 p 位置的元素减去 r,若 p 位置的值小于r 则不进行减法,输出此时 p 位置的值;
(3) s l r mod:求区间 [l, r] 中值 %m==mod 的所有元素的和,输出该和。
Input
第 \(1\) 行 \(n,m\);
第 \(2\) 行 \(n\) 个数 \(a_i\);
第 \(3\) 行 \(q\);
接下来 \(q\) 行,每行对应一种操作;
Output
输出 \(q\) 行,为每次操作的结果。
数据范围:\(1≤n,q≤10^4, 1≤m≤10, 0≤a_i≤10^9\)。
Input | Output |
---|---|
3 4 |
2 |
分析
多个树状数组
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e4+10,INF=0x3f3f3f3f;
int n,m,q;
LL tree[10][N],a[N];
#define lowbit(x) (x&-(x))
void add(int k,int x,int y){
while(x<=n) tree[k][x]+=y, x+=lowbit(x);
}
LL sum(int k,int x){
LL res=0;
while(x) res+=tree[k][x], x-=lowbit(x);
return res;
}
int main(){
// freopen("data.in", "r", stdin);
cin>>n>>m;
for(int i=1; i<=n; i++)cin>>a[i], add(a[i]%m, i, a[i]);
cin>>q; char op; int p,l,r,mod;
while(q--){
cin>>op;
if(op=='+'||op=='-'){
cin>>p>>r;
if(op=='-') r*=-1;
if(a[p]+r >= 0) {
add((a[p]%m+m)%m, p, -a[p]);
a[p] += r;
add((a[p]%m+m)%m, p, a[p]);
}
cout<<a[p]<<endl;
}else{
cin>>l>>r>>mod;
cout<<sum(mod, r)-sum(mod,l-1)<<endl;
}
}
return 0;
}
Queries Gym - 100741A - 树状数组的更多相关文章
- Codeforces 1167 F Scalar Queries 计算贡献+树状数组
题意 给一个数列\(a\),定义\(f(l,r)\)为\(b_1, b_2, \dots, b_{r - l + 1}\),\(b_i = a_{l - 1 + i}\),将\(b\)排序,\(f(l ...
- Gym - 101908C 树状数组 逆序对
Grandpa Giuseppe won a professional pizza cutter, the kind of type reel and, to celebrate, baked a r ...
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- 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 ...
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- CodeForces - 369E Valera and Queries(树状数组)
CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...
- CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)
You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...
随机推荐
- CodeGym自学笔记06——内存寻址和变量
内存寻址和变量 print() print() 函数用于在屏幕上逐个字符显示文本.当屏幕上某一行没有足够空间时,文本开始在下一行显示. println() 可以使用 println() 函数停止在当前 ...
- AWG含义及尺寸电流对照表-转载
AWG含义及尺寸电流对照表 - 麦穗鱼~ - 博客园 (cnblogs.com) AWG(American wire gauge)美国线规,是一种区分导线直径的标准,又被称为 Brown & ...
- 压力&负载理论
一.定义: 1.压力测试 是给软件不断加压,强制其在极限的情况下运行,观察它可以运行到何种程度,从而发现性能缺陷,是通过搭建与实际环境相似的测试环境,通过测试程序在同一时间内或某一段时间内,向 ...
- 常用ansible命令梳理
命令的具体格式 : ansible <host-pattern> [-f forks] [-m module_name] [-a args] 场景 命令 查询线上所有机器某个文件的含有某个 ...
- 利用canvas合并两个海报
图片1 是个海报,图片2是个二维码,把这个二维码镶嵌到图片1 的指定位置上 function drawAndShareImage(opt, cb) { if (!opt) { console.erro ...
- SQL server 去掉重复数据
只要数据表"列名"数据相同,则说明是两条重复的数据(ID为数据表的主键自动增长). 推荐使用方法一 -- 方法一 select * from 表名 A where not exis ...
- Java集合-练习巩固
练习一 public class H1_Test { public static void main(String[] args) { H1_News h1News = new H1_News(&qu ...
- Ubuntu安装微信/企业微信
1.安装Wine git clone https://gitee.com/wszqkzqk/deepin-wine-for-ubuntu.gitcd deepin-wine-for-ubuntusud ...
- Python列表等长度分割
1 def list_of_groups(init_list, childern_list_len): 2 ''' 3 :param init_list: 4 :param childern_list ...
- springMVC学习day02
了解springMVC 1. 了解官网 1. 首先到spring地址去,然后选择项目下面的任何一个子项目,我选择spring framework https://spring.io/ 2.选择spr ...