답안 #480756

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
480756 2021-10-18T03:07:49 Z tranxuanbach 중앙값 배열 (balkan11_medians) C++17
0 / 100
69 ms 21220 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] = -1;
            lazy[id] = -1;
            return;
        }
        int mid = (l + r) >> 1;
        build(id * 2, l, mid);
        build(id * 2 + 1, mid + 1, r);
        seg[id] = -1;
        lazy[id] = -1;
    }

    void down(int id){
        if (lazy[id] == -1){
            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] = -1;
    }

    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 -1;
        }
        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:                                           |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
*/
# 결과 실행 시간 메모리 Grader output
1 Runtime error 7 ms 9996 KB Execution killed with signal 11
2 Runtime error 9 ms 9932 KB Execution killed with signal 11
3 Runtime error 7 ms 10060 KB Execution killed with signal 11
4 Runtime error 7 ms 9932 KB Execution killed with signal 11
5 Runtime error 8 ms 9932 KB Execution killed with signal 11
6 Runtime error 8 ms 9932 KB Execution killed with signal 11
7 Runtime error 7 ms 9996 KB Execution killed with signal 11
8 Runtime error 7 ms 9932 KB Execution killed with signal 11
9 Runtime error 10 ms 9932 KB Execution killed with signal 11
10 Runtime error 7 ms 9940 KB Execution killed with signal 11
11 Runtime error 8 ms 10060 KB Execution killed with signal 11
12 Runtime error 7 ms 10060 KB Execution killed with signal 11
13 Runtime error 9 ms 10188 KB Execution killed with signal 11
# 결과 실행 시간 메모리 Grader output
1 Runtime error 8 ms 10188 KB Execution killed with signal 11
2 Runtime error 8 ms 10408 KB Execution killed with signal 11
3 Runtime error 11 ms 10828 KB Execution killed with signal 11
4 Incorrect 20 ms 6784 KB Integer 0 violates the range [1, 31999]
5 Runtime error 24 ms 13204 KB Execution killed with signal 11
6 Runtime error 42 ms 16332 KB Execution killed with signal 11
7 Runtime error 69 ms 21220 KB Execution killed with signal 11