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>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353, maxn = 5e4 + 5, lg = 40, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int a[maxn], n;
vector<int> cand[maxn], dp[maxn][lg];
int get(int u, int x){
return lower_bound(all(cand[u]), x) - begin(cand[u]);
}
vector<int> get(int r, int p, int lx, int rx){
vector<int> cand, resl = dp[r<<1][get(r<<1, lx)], resr = dp[r<<1|1][get(r<<1|1, rx)];
cand.reserve(sz(dp[r<<1][0]) + sz(dp[r<<1|1][0]) + 1); cand.pb(a[p]);
int ptr1 = 0, ptr2 = 0, st = 0;
while(ptr1 < sz(resl) || ptr2 < sz(resr)){
int k = 1<<st;
while(k && ptr1 < sz(resl)) cand.pb(resl[ptr1++]), k--;
k = 1<<st;
while(k && ptr2 < sz(resr)) cand.pb(resr[ptr2++]), k--;
st++;
}
return cand;
}
void dfs(int r){
int cnt = 0;
if(r<<1 <= n) dfs(r<<1), cnt++;
if(r<<1 < n) dfs(r<<1|1), cnt++;
rep(i,0,sz(cand[r])){
int x = cand[r][i];
if(cnt == 0) dp[r][i].pb(a[x]);
else if(cnt == 1){
dp[r][i].pb(a[x]), dp[r][i].pb(a[r<<1]);
if(a[x] > a[r<<1]) swap(dp[r][i][0], dp[r][i][1]);
}else{
int lx = r<<1, rx = r<<1|1;
if(a[x] < a[lx] && a[x] < a[rx]) dp[r][i] = get(r, x, lx, rx);
else if(a[lx] < a[x] && a[x] < a[rx]) dp[r][i] = get(r, lx, x, rx);
else if(a[lx] < a[rx] && a[rx] < a[x]) dp[r][i] = get(r, lx, x, rx);
else dp[r][i] = min(get(r, rx, x, lx), get(r, rx, lx, x));
}
}
if(cnt) rep(i,0,sz(cand[r<<1])) dp[r<<1][i].clear();
if(cnt == 2) rep(i,0,sz(cand[r<<1])) dp[r<<1|1][i].clear();
}
int main(){ IOS();
cin >> n;
rep(i,1,n+1) cin >> a[i];
rep(i,1,n+1){
cand[i].pb(i);
for(int j = i; j > 1; j >>= 1) cand[i].pb(j>>1), cand[i].pb(j^1);
sort(all(cand[i]));
while(sz(cand[i]) && cand[i].back() > n) cand[i].pop_back();
}
dfs(1);
// rep(i,1,n+1){
// rep(j,0,sz(cand[i])){
// er(i, cand[i][j]);
// for(int c: dp[i][j]) cerr << c << ' '; cerr << '\n';
// }
// }
for(int c: dp[1][0]) cout << c << ' ';
return 0;
}
Compilation message (stderr)
swap.cpp: In function 'void IOS()':
swap.cpp:19:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen("in.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
swap.cpp:20:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen("out.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |