Submission #480759

# Submission time Handle Problem Language Result Execution time Memory
480759 2021-10-18T03:13:26 Z tranxuanbach medians (balkan11_medians) C++17
100 / 100
140 ms 20908 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())

using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;

const int N = 2e5 + 5;

struct lazy_segment_tree{
    int seg[4 * N], lazy[4 * N];

    void build(int id, int l, int r){
        if (l == r){
            seg[id] = 0;
            lazy[id] = 0;
            return;
        }
        int mid = (l + r) >> 1;
        build(id * 2, l, mid);
        build(id * 2 + 1, mid + 1, r);
        seg[id] = 0;
        lazy[id] = 0;
    }

    void down(int id){
        if (lazy[id] == 0){
            return;
        }
        seg[id * 2] = max(seg[id * 2], lazy[id]);
        lazy[id * 2] = max(lazy[id * 2], lazy[id]);
        seg[id * 2 + 1] = max(seg[id * 2 + 1], lazy[id]);
        lazy[id * 2 + 1] = max(lazy[id * 2 + 1], lazy[id]);
        lazy[id] = 0;
    }

    void update(int id, int l, int r, int u, int v, int val){
        if (v < l or r < u){
            return;
        }
        if (u <= l and r <= v){
            seg[id] = max(seg[id], val);
            lazy[id] = max(lazy[id], val);
            return;
        }
        down(id);
        int mid = (l + r) >> 1;
        update(id * 2, l, mid, u, v, val);
        update(id * 2 + 1, mid + 1, r, u, v, val);
        seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
    }

    int get(int id, int l, int r, int u, int v){
        if (v < l or r < u){
            return 0;
        }
        if (u <= l and r <= v){
            return seg[id];
        }
        down(id);
        int mid = (l + r) >> 1;
        return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
    }
} seg;

int n, m;
int a[N];

int ans[N];
vi vpos[N];
set <int> stt;

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("KEK.inp", "r", stdin);
    // freopen("KEK.out", "w", stdout);
    cin >> n;
    ForE(i, 1, n){
        cin >> a[i];
    }

    m = 2 * n - 1;
    seg.build(1, 1, m);
    ForE(i, 2, n){
        if (a[i] > a[i - 1]){
            seg.update(1, 1, m, a[i - 1] + 1, a[i] - 1, i);
        }
        else if (a[i] < a[i - 1]){
            seg.update(1, 1, m, a[i] + 1, a[i - 1] - 1, i);
        }
    }
    ForE(i, 1, m){
        vpos[seg.get(1, 1, m, i, i)].emplace_back(i);
    }
    Fora(pos, vpos[0]){
        stt.insert(pos);
    }
    ans[1] = a[1];
    stt.erase(ans[1]);
    ForE(i, 2, n){
        if (a[i] > a[i - 1]){
            ans[2 * i - 2] = *stt.upper_bound(a[i - 1]);
            stt.erase(ans[2 * i - 2]);
            ans[2 * i - 1] = *stt.upper_bound(a[i - 1]);
            stt.erase(ans[2 * i - 1]);
        }
        else if (a[i] < a[i - 1]){
            ans[2 * i - 2] = *(--stt.lower_bound(a[i - 1]));
            stt.erase(ans[2 * i - 2]);
            ans[2 * i - 1] = *(--stt.lower_bound(a[i - 1]));
            stt.erase(ans[2 * i - 1]);
        }
        else{
            ans[2 * i - 2] = *stt.upper_bound(a[i - 1]);
            stt.erase(ans[2 * i - 2]);
            ans[2 * i - 1] = *(--stt.lower_bound(a[i - 1]));
            stt.erase(ans[2 * i - 1]);
        }
        Fora(pos, vpos[i]){
            stt.insert(pos);
        }
    }
    ForE(i, 1, m){
        cout << ans[i] << ' ';
    } cout << endl;
}

/*
==================================================+
INPUT:                                            |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
OUTPUT:                                           |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
*/
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4936 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 5 ms 5048 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 4 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5256 KB Output is correct
2 Correct 7 ms 5580 KB Output is correct
3 Correct 12 ms 6092 KB Output is correct
4 Correct 26 ms 7236 KB Output is correct
5 Correct 47 ms 9580 KB Output is correct
6 Correct 82 ms 14500 KB Output is correct
7 Correct 140 ms 20908 KB Output is correct