给定 \(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
1 2 3
3
s 1 3 2
+ 2 1
- 1 2
2
3
1

分析

多个树状数组

#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 - 树状数组的更多相关文章

  1. 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 ...

  2. Gym - 101908C 树状数组 逆序对

    Grandpa Giuseppe won a professional pizza cutter, the kind of type reel and, to celebrate, baked a r ...

  3. GYM 100741A Queries(树状数组)

    A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...

  4. gym 100589A queries on the Tree 树状数组 + 分块

    题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...

  5. Codeforces Gym 100114 H. Milestones 离线树状数组

    H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...

  6. Gym 101908C - Pizza Cutter - [树状数组]

    题目链接:https://codeforces.com/gym/101908/problem/C 题意: 一块正方形披萨,有 $H$ 刀是横切的,$V$ 刀是竖切的,不存在大于等于三条直线交于一点.求 ...

  7. Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp

    Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...

  8. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  9. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  10. 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 ...

随机推荐

  1. python获取某一年的所有节假日

    注:chinese_calander库需要每年手动更新一次 import datetime import chinese_calendar def get_holidays(year=None, in ...

  2. Java——File类

    File类 File:代表一个文件或者文件夹 方法 createNewFile() exists() getAbsolutePath() getName() getParent() isDirecto ...

  3. Mac 如何用python下载Mac OS

    ​ Python脚本下载 开源地址:macadmin-scripts 打开终端 : git clone https://github.com/munki/macadmin-scripts 下载inst ...

  4. vue指令入门

    1.  vue属性.事件.内容绑定 1 <div id="dv"> 2 <!-- v-cloak能够解决表达式闪烁问题 3 (当网速较慢时,会先出现{{msg}} ...

  5. Qt构造函数和析构函数报错undefinedreferenceto`vtable for 。。。。'

    百度了一堆,说各种的都有,无非就是说什么构造函数没有实现父类的纯虚函数,但是我明明继承并实现了,,,最后发现可能就是单纯的Qtcreater抽风了,一开始在构造里面清理了项目,发现不行. 于是一不做二 ...

  6. LWC-002_Composition

    Composition (lwc.dev) Set a Property on a Child Component # Send an Event from a Child to an Owner   ...

  7. DOM状态监听(观察者模式)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. 常用ansible命令梳理

    命令的具体格式 : ansible <host-pattern> [-f forks] [-m module_name] [-a args] 场景 命令 查询线上所有机器某个文件的含有某个 ...

  9. NSFW.js 前端使用教程

    引用js + 下载模型 先看文档 https://github.com/infinitered/nsfwjs 非常好,一点也看不懂.总之,先引js,不知道去哪里下载js就上jsdelivr搜一搜 按顺 ...

  10. vue项目跳转外部链接,替换链接地址参数信息

    //修改url某个指定的参数 url 地址. paramName 参数.   repalceValue替换的新值 replaceUrl( url, paramName, repalceValue){ ...