Submission #981553

#TimeUsernameProblemLanguageResultExecution timeMemory
981553GhettoQueue (CEOI06_queue)C++17
50 / 100
321 ms26204 KiB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using mii = unordered_map<int, int>;
const int N = 1e9;

vector<pii> moves;
set<int> starts = {1};
mii l, r, f;
int s;
void precomp1() {
    for (auto it = starts.begin(); it != starts.end(); it++) {
        l[*it] = (it == starts.begin()) ? 0 : *next(it, -1);
        r[*it] = (next(it, 1) == starts.end()) ? 0 : *next(it, 1);
        f[*it] = (next(it, 1) == starts.end()) ? N : *next(it, 1) - 1;
        // cout << *it << ": " << l[*it] << " " << r[*it] << " " << f[*it] << endl;
    }
    for (pii move : moves) {
        int x = move.first, y = move.second;
        r[l[x]] = r[x], l[r[x]] = l[x];
        l[x] = l[y], r[x] = y;
        r[l[y]] = x, l[y] = x;
    }
    for (int x : starts)
        if (l[x] == 0) s = x;
    // int x = s;
    // while (x != 0) {
    //     cout << x << " " << f[x] << '\n';
    //     x = r[x];
    // }
}
mii sum_before;
void precomp2() {
    int x = s, sum = 0;
    while (x != 0) {
        sum_before[x] = sum;
        sum += f[x] - x + 1;
        // cout << x << " " << f[x] << ": " << sum_before[x] << endl;
        x = r[x];
    }
}

int main() {
    // freopen("queue.in", "r", stdin), freopen("queue.out", "w", stdout);
    int m; cin >> m;
    for (int i = 1; i <= m; i++) {
        int x, y; cin >> x >> y;
        moves.push_back({x, y});
        for (int z : {x, x + 1, y})
            if (z <= N) starts.insert(z);
    }
    
    precomp1();
    precomp2();

    int q; cin >> q;
    for (int tc = 1; tc <= q; tc++) {
        char ch; cin >> ch;
        int x; cin >> x;
        if (ch == 'L') {
            cout << 0 << '\n';
        } else {
            int y = *next(starts.upper_bound(x), -1);
            int ans = sum_before[y] + x - y + 1;
            cout << ans << '\n';
        }
    }

    
}   
#Verdict Execution timeMemoryGrader output
Fetching results...