社区讨论
纯粹的Treap,但未果,求助!TAT
P2234[HNOI2002] 营业额统计参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lyww9hem
- 此快照首次捕获于
- 2024/07/22 19:19 2 年前
- 此快照最后确认于
- 2024/07/22 20:18 2 年前
CPP
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+5;
int n,k,ans(0),rt,w(0);
struct node{
int lc,rc,fa,pos,vis;
#define lc(x) t[x].lc
#define rc(x) t[x].rc
#define fa(x) t[x].fa
#define pos(x) t[x].pos
#define vis(x) t[x].vis
}t[N];
void zig(int &x){ //右旋
int y=lc(x);
lc(x) = rc(y);
rc(y) = x;
x = y;
}
void zag(int &x){ //左旋
int y=rc(x);
rc(x) = lc(y);
lc(y) = x;
x = y;
}
void insert(int &x,const int vi){
if (!x){
vis(x=++w)=vi;
pos(x)=rand();
return ;
}
if (vi<vis(x)){
insert(lc(x),vi);
if (pos(lc(x))<pos(x))
zig(x);
}
else{
insert(lc(x),vi);
if (pos(lc(x))<pos(x))
zag(x);
}
}
int querypre(const int vi){
int x=rt,pr=-INT_MAX;
while(x){
if (vi>=vis(x)) pr=vis(x),x=rc(x);
else x=lc(x);
}
return pr;
}
int querysuf(const int vi){
int x=rt,sf=INT_MAX;
while(x){
if (vi<=vis(x)) sf=vis(x),x=lc(x);
else x=rc(x);
}
return sf;
}
int main(){
cin >> n >> k;
insert(rt,k);
ans+=k;
for (int i=2;i<=n;i++){
cin >> k;
int x=querypre(k);
int y=querysuf(k);
ans += min(k-x,y-k);
insert(rt,k);
}
cout << ans;
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...