제출 #1136247

#제출 시각아이디문제언어결과실행 시간메모리
1136247Hamed_GhaffariSwap (BOI16_swap)C++20
68 / 100
1111 ms321248 KiB
#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];
map<int, node*> dp[MXN];

inline vector<node*> arr(node *v) {
  vector<node*> q;
  q.pb(v);
  int ptr=0;
  while(ptr<q.size()) {
    v = q[ptr++];
    if(v->lc) q.pb(v->lc);
    if(v->rc) q.pb(v->rc);
  }
  return q;
}

bool operator<(vector<node*> &A, vector<node*> &B) {
  for(int i=0; i<A.size(); i++)
    if(A[i]->val<B[i]->val) return 1;
    else if(A[i]->val>B[i]->val) 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<node*> 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<node*> 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<node*> ans = arr(DP(1, a[1]));
  for(node *v : ans) cout << v->val << ' ';
  cout << '\n';
  return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

swap.cpp: In function 'bool operator<(std::vector<node*>&, std::vector<node*>&)':
swap.cpp:35:1: warning: control reaches end of non-void function [-Wreturn-type]
   35 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...