#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,sse4,sse4.2,lzcnt,popcnt")
#define pb push_back
const int MXN = 2e5+1;
struct node {
int val;
node *lc, *rc;
node(int val=0, node *lc=NULL, node *rc=NULL): val(val), lc(lc), rc(rc) {}
};
int n, a[MXN];
unordered_map<int, node*> dp[MXN];
vector<int> arr(node *v) {
queue<node*> q;
vector<int> ans;
q.push(v);
while(!q.empty()) {
v = q.front();
q.pop();
ans.pb(v->val);
if(v->lc) q.push(v->lc);
if(v->rc) q.push(v->rc);
}
return ans;
}
bool operator<(vector<int> A, vector<int> B) {
for(int i=0; i<A.size(); i++)
if(A[i]<B[i]) return 1;
else if(A[i]>B[i]) return 0;
}
node *DP(int v, int x) {
if(dp[v].find(x) != dp[v].end()) return dp[v][x];
if((v<<1)>n) return dp[v][x] = new node(x);
if((v<<1|1)>n) {
if(x<a[v<<1]) return dp[v][x] = new node(x, DP(v<<1, a[v<<1]));
else return dp[v][x] = new node(a[v<<1], DP(v<<1, x));
}
if(x<a[v<<1] && x<a[v<<1|1])
return dp[v][x] = new node(x, DP(v<<1, a[v<<1]), DP(v<<1|1, a[v<<1|1]));
if(a[v<<1] < a[v<<1|1])
return dp[v][x] = new node(a[v<<1], DP(v<<1, x), DP(v<<1|1, a[v<<1|1]));
dp[v][x] = new node(a[v<<1|1], DP(v<<1, x), DP(v<<1|1, a[v<<1]));
vector<int> v1 = arr(dp[v][x]);
dp[v][x]->lc = DP(v<<1, a[v<<1]); dp[v][x]->rc = DP(v<<1|1, x);
vector<int> v2 = arr(dp[v][x]);
if(v1<v2) dp[v][x]->lc = DP(v<<1, x), dp[v][x]->rc = DP(v<<1|1, a[v<<1]);
return dp[v][x];
}
int32_t main() {
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
cin >> n;
for(int i=1; i<=n; i++) cin >> a[i];
vector<int> ans = arr(DP(1, a[1]));
for(int i : ans) cout << i << ' ';
cout << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
swap.cpp: In function 'bool operator<(std::vector<int>, std::vector<int>)':
swap.cpp:37:1: warning: control reaches end of non-void function [-Wreturn-type]
37 | }
| ^
# | 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... |