AT_abc432_e 题解
得亏这题我想得快,不然我就不是离青名只有两分了(
考虑题目让你干什么。说直白的就是,给你一个数组,然后会有一些修改,查询操作就是给定区间
注意这题它很坑,有
根据上面神秘又绕口的拆解我们可以知道,只需要维护数值区间范围内的个数以及总和即可。很容易发现这是一个单点修改区间查询,还愣着干什么,树状数组呀!这个就完全没必要线段树了吧,树状数组好写多了。维护两个即可,每次修改的时候先删再加。
赛后我发现对于
这里放上赛时代码,在
#include<bits/stdc++.h>
#define LL long long
#define UInt unsigned int
#define ULL unsigned long long
#define LD long double
#define pii pair<int,int>
#define pLL pair<LL,LL>
#define pDD pair<LD,LD>
#define fr first
#define se second
#define pb push_back
#define isr insert
using namespace std;
const int N = 5e5+5;
const int UP = 500002;
struct TreeP{
LL t[N];
void clr(){for(int i=0;i<=UP;i++)t[i]=0;return;}
void upd(int x,LL k){while(x<=UP)t[x]+=k,x+=x&-x;return;}
LL ask(int x){LL sm=0;while(x)sm+=t[x],x-=x&-x;return sm;}
}Sm,Ct;
LL n,Q,a[N];
LL read(){
LL su=0,pp=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')pp=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){su=su*10+ch-'0';ch=getchar();}
return su*pp;
}
int main(){
n=read(),Q=read();
for(int i=1;i<=n;i++)a[i]=read()+1;
Sm.clr(),Ct.clr();
for(int i=1;i<=n;i++)
Ct.upd(a[i],1),Sm.upd(a[i],a[i]);
while(Q--){
int opt=read();
if(opt==1){
LL x=read(),y=read()+1;
Ct.upd(a[x],-1),Sm.upd(a[x],-a[x]);
Ct.upd(y,1),Sm.upd(y,y),a[x]=y;
}else{
LL l=read()+1,r=read()+1;
if(l>r)r=l;
LL ans=Sm.ask(r)-Sm.ask(l-1);
ans+=Ct.ask(l-1)*l;
ans+=(n-Ct.ask(r))*r;
cout<<ans-n<<"\n";
}
}
return 0;
}
如果本篇题解对你有帮助的话,麻烦你点一个小小的赞,真是太感谢啦!