Queries Gym - 100741A - 树状数组
给定
(1) + p r: 将 p 位置的元素加上 r, 输出此时 p 位置的值;
(2) - p r : 将 p 位置的元素减去 r,若 p 位置的值小于r 则不进行减法,输出此时 p 位置的值;
(3) s l r mod:求区间 [l, r] 中值 %m==mod 的所有元素的和,输出该和。
Input
第
第
第
接下来
Output
输出
数据范围:
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;
}
分类:
VJ-给题目添标签
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】