Submission #311140

# Submission time Handle Problem Language Result Execution time Memory
311140 2020-10-09T13:57:49 Z nikatamliani Interval Collection (CCO20_day2problem2) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1e6 + 5, oo = 1e9, LEFT = 0, RIGHT = 1;
multiset < int > ms[2][N], L, R;
struct node{
    int maxL = -oo, minR = oo, best = minR - maxL;
}tree[N * 4];
void merge(node &x, node &L, node &R) {
    x.maxL = max(L.maxL, R.maxL);
    x.minR = min(L.minR, R.minR);
    x.best = min({L.best, R.best, R.minR - L.maxL});
}
void update(int l, int r, int x, int val, int p, int TYPE) {
    if(l > x || r < x) return;
    if(l == r) {
        if(TYPE == LEFT) {
            tree[p].maxL = val;
        } else {
            tree[p].minR = val;
        }
        tree[p].best = tree[p].minR - tree[p].maxL;
    } else {
        int m = l + r >> 1;
        update(l, m, x, val, p << 1, TYPE);
        update(m + 1, r, x, val, p << 1 | 1, TYPE);
        merge(tree[p], tree[p << 1], tree[p << 1 | 1]);
    }
}
int main() {
    int q;
    cin >> q;
    multiset < pair < int, int > > m;
    for(int i = 1; i <= q; ++i) {
        int l, r;
        char t;
        cin >> t >> l >> r;
        if(t == 'R') {
            m.erase(m.find({l, r}));
            ms[0][l].erase(ms[0][l].find(r));
            ms[1][r].erase(ms[1][r].find(l));
            L.erase(L.find(l));
            R.erase(R.find(r));
            int lf = -oo, rg = oo;
            if(!ms[0][l].empty()) rg = *ms[0][l].begin();
            if(!ms[1][r].empty()) lf = *ms[1][r].rbegin();
            update(1, N - 1, l, rg, 1, RIGHT);
            update(1, N - 1, r, lf, 1, LEFT);
        } else {
            m.insert({l, r});
            ms[0][l].insert(r);
            ms[1][r].insert(l);
            L.insert(l);
            R.insert(r);
            int lf, rg;
            if(!ms[0][l].empty()) rg = *ms[0][l].begin();
            if(!ms[1][r].empty()) lf = *ms[1][r].rbegin();
            update(1, N - 1, l, rg, 1, RIGHT);
            update(1, N - 1, r, lf, 1, LEFT);
        }
        int maxL = *L.rbegin(), minR = *R.begin();
        // for(auto [l, r] : m) {
        //     maxL = max(maxL, l);
        //     minR = min(minR, r);
        // }
        if(minR <= maxL) {
            cout << tree[1].best << '\n';
        } else {
            int ansL = -1, ansR = 1e9;
            ansR = *ms[0][maxL].begin();
            ansL = *ms[1][minR].rbegin();
            // for(auto [l, r] : m) {
            //     if(l == maxL) {
            //         ansR = min(ansR, r); 
            //     }
            //     if(r == minR) {
            //         ansL = max(ansL, l);
            //     }
            // }
            cout << ansR - ansL << '\n';
        }
    }
}

Compilation message

Compilation timeout while compiling Main