Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- #define int long long
- #define pb push_back
- #define ppb pop_back
- #define all(x) (x).begin(),(x).end()
- #define uniq(v) (v).erase(unique(all(v)),(v).end())
- #define sz(x) (int)((x).size())
- #define f first
- #define s second
- #define pii pair<int,int>
- #define rep(i,a,b) for(int i = a; i < b; i++)
- #define repd(i,a,b) for(int i = a; i >= b; i--)
- #define mem1(a) memset(a, -1, sizeof(a))
- #define ppc __builtin_popcount
- #define ppcll __builtin_popcountll
- #define ll long long
- #define ld long double
- template<typename T,typename U>istream& operator>>(istream& in,pair<T,U> &a){in>>a.f>>a.s;return in;}
- template<typename T,typename U>ostream& operator<<(ostream& out,pair<T,U> a){out<<'('<<a.f<<", "<<a.s<<')';return out;}
- template<typename T>ostream& operator<<(ostream&cout,vector<T>const&v){cout<<"[";rep(i,0,sz(v)){if(i)cout<<", ";cout<<v[i];}return cout<<"]";}
- template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
- template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
- #ifndef ONLINE_JUDGE
- #define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
- template <typename Arg1>
- void __f(const char* name, Arg1&& arg1) {
- cout << name << " : " << arg1 << std::endl;
- }
- template <typename Arg1, typename... Args>
- void __f(const char* names, Arg1&& arg1, Args&&... args) {
- const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
- }
- #else
- #define dbg(...)
- #endif
- const ld pi = 3.14159265358979323846;
- const char nl = '\n';
- const long long INF=1e18;
- const int32_t M=1e9+7;
- const int32_t MM=998244353;
- const int N=1e6+5;
- ll n, m, q, k, l, r, x, y, z, a[N], b[N], c[N];
- string s,t;
- struct node {
- int val;
- node* left = nullptr;
- node* right = nullptr;
- node(int v) : val(v) {};
- };
- node* build(int l, int r, vector<int>& a) {
- if(l > r) return nullptr;
- if(l == r) return new node(a[l]);
- int len = r-l+1;
- int lg = __lg(len);
- int lastLevel = 1 << lg, each = (1 << lg) - 1;
- each /= 2;
- int leftSize = each + min(lastLevel/2, len - 2*each - 1);
- node* left = build(l, l + leftSize - 1, a);
- node* right = build(l + leftSize + 1, r, a);
- node* ret = new node(a[l + leftSize]);
- ret->left = left;
- ret->right = right;
- return ret;
- }
- void KSBR(){
- cin >> n;
- vector<int> a(n);
- for(int &x : a) cin >> x;
- node* root = build(0, n-1, a);
- queue<node*> q;
- q.push(root);
- while(sz(q)) {
- int siz = q.size();
- for(int i = 0; i < siz; i++) {
- node* cur = q.front(); q.pop();
- cout << cur->val << " ";
- if(cur->left) q.push(cur->left);
- if(cur->right) q.push(cur->right);
- }
- cout << nl;
- }
- }
- signed main(){
- ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- #ifdef SIEVE
- sieve();
- #endif
- #ifdef NCR
- init();
- #endif
- int t = 1;
- //cin>>t;
- while(t--) {
- KSBR();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement