# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1010853 | vjudge1 | Queue (CEOI06_queue) | C++17 | 1046 ms | 18772 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;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
const int inf = 1e9;
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin >> n;
map<int, int> Nxt, Prv;
set<array<int, 2>> st;
st.insert({0, inf + 1});
while (n--) {
int a, b;
cin >> a >> b;
auto ind = st.upper_bound({a, 2 * inf});
ind--;
auto cur = *ind;
if (a >= cur[0] && a <= cur[1]) {
st.erase(ind);
if (a - 1 >= cur[0]) {
st.insert({cur[0], a - 1});
}
if (a + 1 <= cur[1]) {
st.insert({a + 1, cur[1]});
}
}
swap(a, b);
ind = st.upper_bound({a, 2 * inf});
ind--;
cur = *ind;
if (a >= cur[0] && a <= cur[1]) {
st.erase(ind);
if (a - 1 >= cur[0]) {
st.insert({cur[0], a - 1});
}
if (a + 1 <= cur[1]) {
st.insert({a + 1, cur[1]});
}
}
swap(a, b);
if (!Prv[a]) {
Prv[a] = a - 1;
Nxt[a - 1] = a;
}
if (!Prv[b]) {
Prv[b] = b - 1;
Nxt[b - 1] = b;
}
if (!Nxt[a]) {
Nxt[a] = a + 1;
Prv[a + 1] = a;
}
if (!Nxt[b]) {
Nxt[b] = b + 1;
Prv[b + 1] = b;
}
Nxt[Prv[a]] = Nxt[a], Prv[Nxt[a]] = Prv[a];
Nxt[Prv[b]] = a, Prv[a] = Prv[b];
Nxt[a] = b, Prv[b] = a;
}
vector<int> Pos, Val;
int q;
cin >> q;
vector<array<int, 2>> Q(q);
for (int i = 0; i < q; i++) {
char kind;
int cur;
cin >> kind >> cur;
if (kind == 'P') {
Q[i][0] = 0;
Val.push_back(cur);
}
else {
Q[i][0] = 1;
Pos.push_back(cur);
}
Q[i][1] = cur;
}
int pos = 0, val = 0;
sort(all(Pos));
sort(all(Val));
if (!Nxt[0]) {
Nxt[0] = 1;
}
if (!Nxt[inf]) {
Nxt[inf] = inf + 1;
}
vector<array<int, 2>> Ans;
map<int, int> Ansv, Ansp;
int cur_val = 0, cur_ind = 0, done = 0;
while (done < q) {
if (Nxt.count(cur_val)) {
int lv = cur_val, rv = cur_val;
int lp = cur_ind, rp = cur_ind;
int idx = lower_bound(all(Val), lv) - Val.begin();
while (idx < Val.size() && Val[idx] <= rv) {
Ansp[Val[idx]] = cur_ind;
idx++;
done++;
}
idx = lower_bound(all(Pos), lp) - Pos.begin();
while (idx < Pos.size() && Pos[idx] <= rp) {
Ansv[Pos[idx]] = cur_val;
idx++;
done++;
}
cur_val = Nxt[cur_val];
cur_ind++;
continue;
}
auto ind = st.upper_bound({cur_val, 2 * inf});
ind--;
auto cur = *ind;
int lv = cur_val, rv = cur[1] - 1;
int lp = cur_ind, rp = cur_ind + cur[1] - cur_val - 1;
int idx = lower_bound(all(Val), lv) - Val.begin();
while (idx < Val.size() && Val[idx] <= rv) {
Ansp[Val[idx]] = cur_ind + Val[idx] - lv;
idx++;
done++;
}
idx = lower_bound(all(Pos), lp) - Pos.begin();
while (idx < Pos.size() && Pos[idx] <= rp) {
Ansv[Pos[idx]] = cur_val + Pos[idx] - lp;
idx++;
done++;
}
cur_ind += cur[1] - cur_val;
cur_val = cur[1];
}
for (int i = 0; i < q; i++) {
if (!Q[i][0]) {
cout << Ansp[Q[i][1]] << endl;
}
else {
cout << Ansv[Q[i][1]] << endl;
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |