#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
#define ls treap[now].pl
#define rs treap[now].pr
const int mxn = 7e4+10;
struct node{
int pl,pr;
ll sum,pri,val;
node(){
val = pl = pr = sum = pri = 0;
}
};
node treap[mxn];
int arr[mxn];
int n;
int ppp = 0;
int newnode(){
return ++ppp;
}
void pull(int now){
treap[now].sum = treap[ls].sum+treap[rs].sum+treap[now].val;
return;
}
int merge(int a,int b){
if(!a)return b;
if(!b)return a;
if(treap[a].pri>treap[b].pri){
treap[a].pr = merge(treap[a].pr,b);
pull(a);
return a;
}
else{
treap[b].pl = merge(a,treap[b].pl);
pull(b);
return b;
}
}
void split(int now,int &a,int &b,int tar){
if(!now){
a = b = 0;
return;
}
if(treap[now].val+treap[ls].sum == tar){
a = now,b = rs;
treap[now].pr = 0;
}
else if(treap[ls].sum>=tar){
b = now;
split(ls,a,treap[b].pl,tar);
}
else{
a = now;
split(rs,treap[a].pr,b,tar-treap[ls].sum-treap[now].val);
}
pull(a);
pull(b);
return;
}
int ans[mxn];
int C = 0;
void dfs(int now){
if(!now)return;
dfs(ls);
ans[now] = ++C;
dfs(rs);
return;
}
void pv(int now){
if(!now)return;
pv(ls);
cout<<now<<',';
pv(rs);
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
int rt = 1;
for(int i = 1;i<=n;i++){
cin>>arr[i];
treap[i].pri = (rand()<<15)|rand();
}
treap[1].sum = treap[1].val = 1;
for(int i = 2;i<=n;i++){
int l,r;
split(rt,l,r,arr[i]-arr[i-1]-1);
treap[i].sum = treap[i].val = arr[i]-arr[i-1];
//cout<<i<<":"<<endl;pv(rt);cout<<endl;
//cout<<i<<":"<<endl;pv(l);cout<<endl;pv(r);cout<<endl;
rt = merge(l,i);
rt = merge(rt,r);
}
dfs(rt);
for(int i = 1;i<=n;i++)cout<<ans[i]<<' ';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |