#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;
map<pii,int> rec;
int search(int v,int val,vector<int> &a,int n) {
if (rec.find({v,val})!=rec.end()) return rec[{v,val}];
if ((v<<1)<=n) {
if ((v<<1|1)>n or a[v<<1|1]>val) {
if (a[v<<1]<val) return rec[{v,val}]=search(v<<1,val,a,n);
else return rec[{v,val}]=v;
} else {
int lc=a[v<<1],rc=val;
if (lc>rc) swap(lc,rc);
int lp1=search(v<<1,lc,a,n),lp2=search(v<<1|1,lc,a,n);
if (lp1<lp2) {
if (lc==val) return rec[{v,val}]=lp1;
else return rec[{v,val}]=search(v<<1|1,val,a,n);
} else {
if (lc==val) return rec[{v,val}]=lp2;
else return rec[{v,val}]=search(v<<1,val,a,n);
}
}
} else return rec[{v,val}]=v;
}
void dfs(int v,int val,vector<int> &ans,vector<int> &a,int n) {
if ((v<<1)<=n) {
if ((v<<1|1)<=n) {
int mn = min({val,a[v<<1],a[v<<1|1]});
ans[v]=mn;
if (mn==val) {
dfs(v<<1,a[v<<1],ans,a,n);
dfs(v<<1|1,a[v<<1|1],ans,a,n);
} else if (mn==a[v<<1]) {
dfs(v<<1,val,ans,a,n);
dfs(v<<1|1,a[v<<1|1],ans,a,n);
} else {
int lc=a[v<<1],rc=val;
if (lc>rc) swap(lc,rc);
int lp1=search(v<<1,lc,a,n),lp2=search(v<<1|1,lc,a,n);
if (lp1<lp2) {
dfs(v<<1,lc,ans,a,n);
dfs(v<<1|1,rc,ans,a,n);
} else {
dfs(v<<1,rc,ans,a,n);
dfs(v<<1|1,lc,ans,a,n);
}
}
} else {
ans[v]=min(a[v<<1],val);
dfs(v<<1,max(val,ans[v<<1]),ans,a,n);
}
} else ans[v]=val;
}
int main() {
speed;
int n;
cin>>n;
vector<int> a(n+1);
for (int i=1;i<=n;i++) {
cin>>a[i];
}
vector<int> ans(n+1);
dfs(1,a[1],ans,a,n);
for (int i=1;i<=n;i++) {
cout<<ans[i]<<" ";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |