#include<bits/stdc++.h>
//#include<bits/extc++.h>
using namespace std;
int n;
int ar[200005];
map<int,vector<int>>dp[200005];
void chmn(vector<int>&nw,vector<int>&a,vector<int>&b){
for(int i=0;i<min(a.size(),b.size());i++){
if(a[i]<b[i]){
swap(nw,a);
return;
}
if(a[i]>b[i]){
swap(nw,b);
return;
}
}
if(a.size()<b.size()){
swap(nw,a);
return;
}
swap(nw,b);
}
void mrg(vector<int>&vc,vector<int>&a,vector<int>&b){
int ait=0;int bit=0;
for(int lg=0;(1<<lg)<=a.size()*2;lg++){
for(int k=0;k<(1<<lg)&&(ait+k)<a.size();k++){
vc.push_back(a[ait+k]);
}
ait+=(1<<lg);
for(int k=0;k<(1<<lg)&&(bit+k)<b.size();k++){
vc.push_back(b[bit+k]);
}
bit+=(1<<lg);
}
}
vector<pair<int,int>>seq;
bitset<200001>vis[200005];
void dfs(int nw,int nwv){
if(vis[nw][nwv]){return;}
vis[nw][nwv]=1;
if(nw*2>n){
//dp[nw][nwv]={nwv};
seq.push_back({nw,nwv});
return;
}
if(nw*2+1>n){
// chmn(dp[nw][nwv],{nwv,ar[nw*2]},{ar[nw*2],nwv});
seq.push_back({nw,nwv});
return;
}
if(nwv<ar[nw*2]&&nwv<ar[nw*2+1]){
dfs(nw*2,ar[nw*2]);
dfs(nw*2+1,ar[nw*2+1]);
seq.push_back({nw,nwv});
return;
}
if(ar[nw*2]<nwv&&ar[nw*2]<ar[nw*2+1]){
dfs(nw*2,nwv);
dfs(nw*2+1,ar[nw*2+1]);
seq.push_back({nw,nwv});
return;
}
dfs(nw*2,nwv);
dfs(nw*2+1,ar[nw*2]);
dfs(nw*2,ar[nw*2]);
dfs(nw*2+1,nwv);
seq.push_back({nw,nwv});
}
void solve(int nw,int nwv){
if(dp[nw][nwv].size()){return;}
if(nw*2>n){
dp[nw][nwv]={nwv};
return;
}
if(nw*2+1>n){
chmn(dp[nw][nwv],{nwv,ar[nw*2]},{ar[nw*2],nwv});
return;
}
if(nwv<ar[nw*2]&&nwv<ar[nw*2+1]){
dp[nw][nwv].push_back(nwv);
solve(nw*2,ar[nw*2]);
solve(nw*2+1,ar[nw*2+1]);
mrg(dp[nw][nwv],dp[nw*2][ar[nw*2]],dp[nw*2+1][ar[nw*2+1]]);
return;
}
if(ar[nw*2]<nwv&&ar[nw*2]<ar[nw*2+1]){
dp[nw][nwv].push_back(ar[nw*2]);
solve(nw*2,nwv);
solve(nw*2+1,ar[nw*2+1]);
mrg(dp[nw][nwv],dp[nw*2][nwv],dp[nw*2+1][ar[nw*2+1]]);
return;
}
vector<int>vc;
vc.push_back(ar[nw*2+1]);
solve(nw*2,nwv);
solve(nw*2+1,ar[nw*2]);
mrg(vc,dp[nw*2][nwv],dp[nw*2+1][ar[nw*2]]);
dp[nw][nwv].push_back(ar[nw*2+1]);
solve(nw*2,ar[nw*2]);
solve(nw*2+1,nwv);
mrg(dp[nw][nwv],dp[nw*2][ar[nw*2]],dp[nw*2+1][nwv]);
chmn(dp[nw][nwv],dp[nw][nwv],vc);
vector<int>().swap(vc);
}
signed main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>ar[i];
}
dfs(1,ar[1]);
int lit=0;
for(int i=0;i<seq.size();i++){
solve(seq[i].first,seq[i].second);
while(__lg(seq[lit].first)>__lg(seq[i].first)+1){
// vector<int>vc;
vector<int>().swap(dp[seq[lit].first][seq[lit].second]);
// swap(vc,dp[seq[lit].first][seq[lit].second]);
dp[seq[lit].first].erase(seq[lit].second);
lit++;
}
}
for(int i=0;i<dp[1][ar[1]].size();i++){
cout<<dp[1][ar[1]][i]<<" ";
}cout<<endl;
return 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(dp[i][j].size()){
cout<<i<<" "<<j<<":";
for(int v:dp[i][j]){
cout<<v<<" ";
}
cout<<endl;
}
}
}
}
Compilation message
swap.cpp: In function 'void chmn(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:9:14: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
9 | for(int i=0;i<min(a.size(),b.size());i++){
| ~^~~~~~~~~~~~~~~~~~~~~~~
swap.cpp: In function 'void mrg(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:28:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int lg=0;(1<<lg)<=a.size()*2;lg++){
| ~~~~~~~^~~~~~~~~~~~
swap.cpp:29:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int k=0;k<(1<<lg)&&(ait+k)<a.size();k++){
| ~~~~~~~^~~~~~~~~
swap.cpp:33:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int k=0;k<(1<<lg)&&(bit+k)<b.size();k++){
| ~~~~~~~^~~~~~~~~
swap.cpp: In function 'void solve(int, int)':
swap.cpp:87:51: error: cannot bind non-const lvalue reference of type 'std::vector<int>&' to an rvalue of type 'std::vector<int>'
87 | chmn(dp[nw][nwv],{nwv,ar[nw*2]},{ar[nw*2],nwv});
| ^
In file included from /usr/include/c++/10/vector:67,
from /usr/include/c++/10/functional:62,
from /usr/include/c++/10/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/10/algorithm:74,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from swap.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:625:7: note: after user-defined conversion: 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<int>]'
625 | vector(initializer_list<value_type> __l,
| ^~~~~~
swap.cpp:7:38: note: initializing argument 2 of 'void chmn(std::vector<int>&, std::vector<int>&, std::vector<int>&)'
7 | void chmn(vector<int>&nw,vector<int>&a,vector<int>&b){
| ~~~~~~~~~~~~^
swap.cpp: In function 'int main()':
swap.cpp:130:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | for(int i=0;i<seq.size();i++){
| ~^~~~~~~~~~~
swap.cpp:140:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for(int i=0;i<dp[1][ar[1]].size();i++){
| ~^~~~~~~~~~~~~~~~~~~~