# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
981558 | Ghetto | Queue (CEOI06_queue) | C++17 | 278 ms | 29200 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
set<pii> sums;
void precomp2() {
int x = s, sum = 0;
while (x != 0) {
sum_before[x] = sum, sums.insert({sum_before[x], x});
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') {
int y = next(sums.lower_bound({x, -1}), -1)->second;
int ans = y + x - sum_before[y] - 1;
cout << ans << '\n';
} else {
int y = *next(starts.upper_bound(x), -1);
int ans = sum_before[y] + x - y + 1;
cout << ans << '\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |