This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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) {
int mn = min({val,a[v<<1],a[v<<1|1]});
if (mn==val) return rec[{v,val}]=v;
else if (mn==a[v<<1]) return rec[{v,val}]=search(v<<1,val,a,n);
else {
int lc=a[v<<1],rc=val;
if (rc<lc) return rec[{v,val}]=min(search(v<<1,rc,a,n),search(v<<1|1,rc,a,n));
else {
int lp1=search(v<<1,lc,a,n),lp2=search(v<<1|1,lc,a,n);
if (lp1<lp2) return rec[{v,val}]=search(v<<1|1,val,a,n);
else return rec[{v,val}]=search(v<<1,val,a,n);
}
}
} else {
if (val<a[v<<1]) return rec[{v,val}]=v;
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |