# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1010819 | vjudge1 | Queue (CEOI06_queue) | C++17 | 301 ms | 15712 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;
}
if (!Prv[b]) {
Prv[b] = b - 1;
}
if (!Nxt[a]) {
Nxt[a] = a + 1;
}
if (!Nxt[b]) {
Nxt[b] = b + 1;
}
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<array<int, 2>> Pos, Val;
int q;
cin >> q;
int temp = 0;
while (q--) {
char kind;
int cur;
cin >> kind >> cur;
if (kind == 'P') {
Val.push_back({cur, temp});
}
else {
Pos.push_back({cur, temp});
}
temp++;
}
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;
int cur_val = 0, cur_ind = 0;
bool jump = 0;
int last = -1;
while (cur_val <= inf) {
//cout << cur_val << ' ' << cur_ind << endl;
while (pos < Pos.size() && Pos[pos][0] <= cur_ind) {
Ans.push_back({Pos[pos][1], cur_val - (cur_ind - Pos[pos][0])});
pos++;
}
while (val < Val.size() && Val[val][0] == cur_val || (Val[val][0] < cur_val && Val[val][0] > last && jump)) {
Ans.push_back({Val[val][1], cur_ind - (cur_val - Val[val][0])});
val++;
}
if (Nxt.count(cur_val)) {
cur_val = Nxt[cur_val];
cur_ind++;
jump = 0;
last = cur_val;
continue;
}
auto ind = st.upper_bound({cur_val, 2 * inf});
ind--;
auto cur = *ind;
if (cur_val < cur[0] || cur_val > cur[1]) {
break;
}
cur_ind += cur[1] - cur_val;
cur_val = cur[1];
last = cur_val;
jump = 1;
}
while (pos < Pos.size() && Pos[pos][0] <= cur_ind) {
Ans.push_back({Pos[pos][1], cur_val - (cur_ind - Pos[pos][0])});
pos++;
}
while (val < Val.size() && Val[val][0] <= cur_val) {
Ans.push_back({Val[val][1], cur_ind - (cur_val - Val[val][0])});
val++;
}
sort(all(Ans));
for (int i = 0; i < Ans.size(); i++) {
cout << Ans[i][1] << endl;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |