Submission #311153

#TimeUsernameProblemLanguageResultExecution timeMemory
311153nikatamlianiInterval Collection (CCO20_day2problem2)C++14
25 / 25
4764 ms245252 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, oo = 1e8, LEFT = 0, RIGHT = 1;
multiset < int > ms[2][N], L, R;
struct node{
    int maxL, minR, best;
    node() {
        maxL = -oo;
        minR = oo;
        best = 2 * oo;
    }
}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') {
            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 {
            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();
        if(minR <= maxL) {
            cout << tree[1].best << '\n';
        } else {
            int ansL = -1, ansR = 1e9;
            ansR = *ms[0][maxL].begin();
            ansL = *ms[1][minR].rbegin();
            cout << ansR - ansL << '\n';
        }
    }
}

Compilation message (stderr)

Main.cpp: In function 'void update(int, int, int, int, int, int)':
Main.cpp:28:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   28 |         int m = l + r >> 1;
      |                 ~~^~~
Main.cpp: In function 'int main()':
Main.cpp:60:19: warning: 'rg' may be used uninitialized in this function [-Wmaybe-uninitialized]
   60 |             update(1, N - 1, l, rg, 1, RIGHT);
      |             ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:61:19: warning: 'lf' may be used uninitialized in this function [-Wmaybe-uninitialized]
   61 |             update(1, N - 1, r, lf, 1, LEFT);
      |             ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...