이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, a;
cin >> n >> a;
vector< pair<int, int> > d(n);
for (int i = 0; i < n; i++) {
cin >> d[i].first;
d[i].second = i + 1;
}
sort(d.rbegin(), d.rend());
vector<int> top10;
vector<int> v;
vector<int> ans(n + 1);
for (int i = 0; i < min(10, n); i++) {
top10.push_back(d[i].second);
}
for (int i = n; i >= 10; i--) {
v.push_back(d[i].second);
}
int q;
cin >> q;
int st = 1;
while (q--) {
char t;
cin >> t;
if (t == 'F') {
int b;
cin >> b;
if (st) {
int cur = n;
vector<int> num(n + 1, -1);
for (auto& x : top10) {
if (num[x] == -1) {
num[x] = cur--;
}
}
for (int i = (int) v.size() - 1; i >= 0; i--) {
if (num[v[i]] == -1) {
num[v[i]] = cur--;
}
}
priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
pq.push({num[a], a});
int cnt = 0;
while (!pq.empty()) {
int x = pq.top().second;
pq.pop();
num[x] = -1;
ans[x] = cnt++;
if (x > 1 && num[x - 1] != -1) {
pq.push({num[x - 1], x - 1});
}
if (x < n && num[x + 1] != -1) {
pq.push({num[x + 1], x + 1});
}
}
st = 0;
}
cout << ans[b] << '\n';
} else {
int i, e;
cin >> i >> e;
auto it = top10.begin();
e--;
while (e--) {
it = next(it);
}
top10.insert(it, i);
v.push_back(top10.back());
top10.pop_back();
int cur = n;
vector<int> num(n + 1, -1);
for (auto& x : top10) {
if (num[x] == -1) {
num[x] = cur--;
}
}
for (int i = (int) v.size() - 1; i >= 0; i--) {
if (num[v[i]] == -1) {
num[v[i]] = cur--;
}
}
priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
pq.push({num[a], a});
int cnt = 0;
while (!pq.empty()) {
int x = pq.top().second;
pq.pop();
num[x] = -1;
ans[x] = cnt++;
if (x > 1 && num[x - 1] != -1) {
pq.push({num[x - 1], x - 1});
}
if (x < n && num[x + 1] != -1) {
pq.push({num[x + 1], x + 1});
}
}
st = 0;
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |