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 MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
template <class T>
struct Persistent {
struct node {
T val;
int l, r;
node (T val = 0, int l = 0, int r = 0) : val(val), l(l), r(r) {}
};
vector <node> it;
vector <int> stt;
int n, version;
private :
void update(int idx, int l, int r, int u, T val) {
if(l > u || r < u) return;
int cnt = ++version;
stt[idx] = cnt;
if(l == r) {
it[cnt].val = val;
return;
}
int mid = l + r >> 1;
update(idx << 1, l, mid, u, val);
update(idx << 1 | 1, mid + 1, r, u, val);
it[cnt] = node(it[stt[idx << 1]].val + it[stt[idx << 1 | 1]].val, stt[idx << 1], stt[idx << 1 | 1]);
}
T get(int idx, int l, int r, int u, int v) {
if(l > v || r < u) return 0;
if(l >= u && r <= v) return it[idx].val;
int mid = l + r >> 1;
return get(it[idx].l, l, mid, u, v) + get(it[idx].r, mid + 1, r, u, v);
}
public :
Persistent(int n = 0) : n(n) {
version = 0;
resize(n);
}
void resize(int n) {
it.resize(100 * n + 1);
stt.resize(n << 2 | 1);
}
int update(int u, T val) {
update(1, 1, n, u, val);
return stt[1];
}
T get(int id, int l, int r) {
return get(id, 1, n, l, r);
}
int size() {
return n;
}
};
const int MAXN = 2e5 + 5;
int N, Q, root[MAXN];
pair <int, int> a[MAXN];
void you_make_it(void) {
cin >> N >> Q;
FOR(i, 1, N) cin >> a[i].first, a[i].second = i;
sort(a + 1, a + N + 1);
Persistent <int> per(N);
FORD(i, N, 1) {
root[i] = per.update(a[i].second, 1);
}
while(Q--) {
int l, r; cin >> l >> r;
int L = 0, R = N;
while(R - L > 1) {
int mid = L + R >> 1;
int id = lower_bound(a + 1, a + N + 1, make_pair(mid, 0)) - a;
if(per.get(root[id], l, r) >= mid) L = mid;
else R = mid;
// cout << debug(root[mid]) << " " << mid << '\n';
}
cout << L << '\n';
}
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message (stderr)
index.cpp: In function 'void you_make_it()':
index.cpp:94:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int mid = L + R >> 1;
| ~~^~~
index.cpp: In instantiation of 'void Persistent<T>::update(int, int, int, int, T) [with T = int]':
index.cpp:63:9: required from 'int Persistent<T>::update(int, T) [with T = int]'
index.cpp:88:41: required from here
index.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
index.cpp: In instantiation of 'T Persistent<T>::get(int, int, int, int, int) [with T = int]':
index.cpp:68:13: required from 'T Persistent<T>::get(int, int, int) [with T = int]'
index.cpp:96:32: required from here
index.cpp:47:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |