思路
让你干啥你就干啥呗
查询第x个妹子就get一下再修改
这里稳一点就维护了三个东西,也许两个也可以
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define ls rt<<1
#define rs rt<<1|1
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int maxn = 5e5 + 7;
int read() {
int x = 0, f = 1; char s = getchar();
for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
int n,m;
int a[maxn];
struct node {
int l,r,size,gs;
ll sum,ans;
}e[maxn<<2];
void pushup(int rt) {
e[rt].gs=e[ls].gs+e[rs].gs;
e[rt].ans=e[ls].ans+e[rs].ans;
e[rt].sum=e[ls].sum+e[rs].sum;
}
void build(int l,int r,int rt) {
e[rt].l=l,e[rt].r=r,e[rt].size=r-l+1;
if(l==r) {
if(l<=n) {
e[rt].gs=1;
e[rt].ans=e[rt].sum=a[l];
}
return;
}
int mid=(l+r)>>1;
build(l,mid,ls);
build(mid+1,r,rs);
pushup(rt);
}
void modify_1(int L,int k,int rt) { // The Lth city plus k .
if(e[rt].l==e[rt].r) {
e[rt].sum+=k;
if(e[rt].gs) e[rt].ans+=k;
return;
}
int mid=(e[rt].l+e[rt].r)>>1;
if(L<=mid) modify_1(L,k,ls);
else modify_1(L,k,rs);
pushup(rt);
}
void modify_2(int L,int k,int rt) { // The Lth sister fell in love with him.
if(e[rt].l==e[rt].r) {
e[rt].gs=k;
e[rt].ans=e[rt].sum=0;
return;
}
int mid=(e[rt].l+e[rt].r)>>1;
if(L<=mid) modify_2(L,k,ls);
else modify_2(L,k,rs);
pushup(rt);
}
int get(int k,int rt) {
if(e[rt].l==e[rt].r)
return e[rt].l;
if(k <= e[ls].gs) return get(k,ls);
else return get(k-e[ls].gs,rs);
}
int main() {
n=read(),m=read();
FOR(i,1,n) a[i]=read();
build(1,5e5,1);
FOR(i,1,m) {
char s;
scanf("%s",&s);
if(s=='Q') {
printf("%lld\n",e[1].ans);
} else if(s=='C') {
int x=read(),y=read();
modify_1(x,-y,1);
} else if(s=='D') {
int x=read();
modify_2(get(x,1),0,1);
} else if(s=='I') {
int x=read(),y=read();
modify_2(x,1,1);
modify_1(x,y,1);
}
}
return 0;
}