题解:CF2097E Clearing the Snowdrift
简单题,没场切应该是因为被 d 卡空间部分击败。
(题外话:d 开 256MB 的目的是啥?强迫你手写动态空间 bitset?)
考虑你的策略是将最大值推平,具体来说将
考虑暴力就是从一个位置
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=5e5+10;
int n,d,a[N];
namespace LCT{
int fa[N],ch[N][2],a[N],W[N];
void init(){
for(int i=1;i<=n+1;i++){
ch[i][0]=ch[i][1]=0;
fa[i]=i+1;
a[i]=W[i]=0;
}
fa[n+1]=0;
}
int chk(int x){return (ch[fa[x]][1]==x);}
int is_root(int x){return (ch[fa[x]][0]!=x && ch[fa[x]][1]!=x);}
void pushup(int x){W[x]=W[ch[x][0]]+W[ch[x][1]]+a[x];}
void rotate(int x){
int y=fa[x],z=fa[y],o=chk(x),t=ch[x][o^1];
ch[y][o]=t,fa[t]=y;
if(!is_root(y)) ch[z][chk(y)]=x; fa[x]=z;
ch[x][o^1]=y,fa[y]=x;
pushup(y);
pushup(x);
pushup(z);
}
void splay(int x){
while(!is_root(x)){
int f=fa[x];
if(!is_root(f)){
if(chk(x)==chk(f)) rotate(f);
else rotate(x);
}
rotate(x);
}
}
void Access(int x){
for(int p=0;x;p=x,x=fa[x]){
splay(x);
ch[x][1]=p;
pushup(x);
}
}
int query(int x){
Access(x);
splay(x);
return W[x];
}
void upd(int x){
Access(x);
splay(x);
a[x]++,W[x]++;
fa[ch[x][0]]=0;
ch[x][0]=0;
fa[x]=min(x+d,n+1);
}
}
int p[N];
bool cmp(int i,int j){return a[i]<a[j];}
void read(){
cin>>n>>d;
for(int i=1;i<=n;i++) cin>>a[i],p[i]=i;
LCT::init();
sort(p+1,p+1+n,cmp);
}
void work(){
LL ans=0;
for(int i=n;i>=1;i--){
LCT::upd(p[i]);
ans+=1LL*(a[p[i]]-a[p[i-1]])*LCT::query(1);
}
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
// freopen("nzq.in","r",stdin);
// freopen("nzq.out","w",stdout);
int T=1;
cin>>T;
while(T--){
read();
work();
}
cerr<<1.0*clock()/CLOCKS_PER_SEC<<'\n';
return 0;
}