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;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 4e5 + 5, B = 635;
int n, q;
int a[N], blk[N], L[B], R[B];
priority_queue<int> pq[B];
priority_queue<int, vector<int>, greater<int>> lz[B];
void bld(int id) {
priority_queue<int>().swap(pq[id]);
for (int i = L[id]; i <= R[id]; ++i) {
if (lz[id].size() && lz[id].top() < a[i]) {
lz[id].push(a[i]);
a[i] = lz[id].top(); lz[id].pop();
}
pq[id].push(a[i]);
}
priority_queue<int, vector<int>, greater<int>>().swap(lz[id]);
}
void upd(int l, int r, int &p) {
if (blk[l] == blk[r]) {
bld(blk[l]);
for (int i = l; i <= r; ++i) {
if (p < a[i]) {
swap(p, a[i]);
}
}
} else {
bld(blk[l]);
for (int i = l; i <= R[blk[l]]; ++i) {
if (p < a[i]) {
swap(p, a[i]);
}
}
for (int i = blk[l] + 1; i < blk[r]; ++i) {
if (p < pq[i].top()) {
pq[i].push(p); lz[i].push(p);
p = pq[i].top(); pq[i].pop();
}
}
bld(blk[r]);
for (int i = L[blk[r]]; i <= r; ++i) {
if (p < a[i]) {
swap(p, a[i]);
}
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
blk[i] = i / B;
R[blk[i]] = i;
pq[blk[i]].push(a[i]);
}
for (int i = n; i; --i) {
L[blk[i]] = i;
}
while (q--) {
int s, t, p; cin >> s >> t >> p;
if (s <= t) {
upd(s, t, p);
} else {
upd(s, n, p);
upd(1, t, p);
}
cout << p << "\n";
}
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... |