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 ...
随机推荐
- 【windows】bat脚本、批处理文件
::当前盘符 @echo current pan : %~d0 ::当前路径 @echo current path : %cd%\ ::当前bat文件路径 @echo the bat's path : ...
- Veeva_001常见问题总结
1.时间互动轴:其他代表与该医生的互动记录 2.Call的拜访流程 : 用户在Ipad端先制定计划拜访(已计划:青色)后,需手动同步到服务器上: 进入拜访详细,选择联系人和产品及相关拜访信息等,并'保 ...
- winform 子控件触发父控件事件
private void circlePanel_Click(object sender, EventArgs e) { var panel=sender as UIPanel; if (panel. ...
- Locust fasthttplocust客户端环境搭建
1.下载地址:https://github.com/locustio/locust2.python setup.py install3.在执行目录中将安装完成的locust文件夹替换至原本pip下的原 ...
- C#-out和ref 参数修饰符
参数修饰符: 无参数修饰符:如果一个参数没有任何参数修饰符修饰,那么认为它是值传递,意味着方法内部收到的是实参数据的副本 out:输出参数由方法内部进行赋值,(引用传递),如果方法内部没有给被out修 ...
- airtest截图后放在allure报告里面。
#需要导入的包 from airtest.aircv import * from airtest.aircv.utils import cv2_2_pil screen1 = G.DEVICE.sna ...
- Eureka服务剔除下线
1.参考文档: https://www.centoscn.vip/4317.html 2.剔除服务 格式: curl -X PUT "http://ip:port/eureka/apps/{ ...
- 使用EmguCV3.0.0调用摄像头并设置帧率
此教程使用emgu3.0.0,其他版本兼容性未知. 转载请注明出处: http://www.cnblogs.com/zaiyuzhong/p/open-camera-with-emgu3_0_0.ht ...
- Linux 第八节(防火墙 )
-------------------iptables-------------------------- RHEL 5 6 7.0 7.1 iptable RHEL 8 firewall FORWA ...
- uni-app --vue3--TypeScript 微信小程序开发
微信小程序开发文档: https://developers.weixin.qq.com/miniprogram/dev/framework/quickstart/getstart.html#%E7%9 ...