This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#define LOCAL
#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 1010101
#define MAXQ 101010
#define INF 1'000'000'100
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
#define TC 1
#ifdef LOCAL
#define DEBUG(a) cout<<a
#else
#define DEBUG(...) 1234
#endif
#define B 2500
int A[MAX];
multiset<int> all[MAX];
multiset<int> upd[MAX];
int N, Q;
void prop(int id) {
all[id].clear();
int i;
for (i = id * B; i < min(N, (id + 1) * B); i++) {
upd[id].emplace(A[i]);
A[i] = *upd[id].begin();
upd[id].erase(upd[id].begin());
all[id].emplace(A[i]);
}
upd[id].clear();
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> N >> Q;
int i;
for (i = 0; i < N; i++) cin >> A[i];
for (i = 0; i < N; i++) all[i / B].insert(A[i]);
auto f = [&](int l, int r, int x) {
int i;
i = l;
while (i <= r) {
int b = i / B;
if ((i % B == 0) && i + B - 1 <= r) {
//DEBUG("ii" << i << ln);
upd[b].insert(x);
if (upd[b].size() > B) upd[b].erase(upd[b].find(*upd[b].rbegin()));
all[b].insert(x);
x = *all[b].rbegin();
all[b].erase(all[b].find(x));
i += B;
//DEBUG('x' << x << bb);
}
else {
if (upd[b].size()) prop(b);
//DEBUG('i' << i << bb << A[i] << bb << x << ln);
if (A[i] > x) {
//for (auto x : all[b]) DEBUG('x' << x << bb);
//DEBUG("fuck" << ln);
//for (int i = B * b; i < B * (b + 1); i++) DEBUG(A[i] << bb);
//DEBUG(ln);
//DEBUG("asdf" << x << bb << A[i]);
all[b].insert(x);
all[b].erase(all[b].find(A[i]));
swap(A[i], x);
}
i++;
//DEBUG('y' << x << bb);
}
}
return x;
};
while (Q--) {
int l, r, x;
cin >> l >> r >> x;
l--;
r--;
if (l > r) {
x = f(l, N - 1, x);
x = f(0, r, x);
}
else x = f(l, r, x);
cout << x << ln;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |