# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
973763 | akacool445k | Rainforest Jumps (APIO21_jumps) | C++14 | 0 ms | 0 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>
// #include "jumps.h"
using namespace std;
// #define int long long
#define ll long long
#define ff first
#define ss second
#define pint pair < int , int >
#define fast ios_base::sync_with_stdio(NULL); cin.tie(NULL)
typedef vector < int > vint;
const int inf = 1e9 + 9;
const int mxn = 2e5 + 2;
const int mod = 1e9 + 7;
int n;
int h[mxn] , le[mxn] , ri[mxn];
int inv[mxn];
int spx[19][mxn] , spn[19][mxn];
int s[4 * mxn];
void build(int id, int L, int R) {
if(L == R) {
s[id] = h[L];
return;
}
int x = 2 * id + 1, y = x + 1, M = (L + R) / 2;
build(x, L, M);
build(y, M + 1, R);
s[id] = max(s[x], s[y]);
}
pair<int, int> query(int id, int L, int R, int l, int r, int d) {
if(L == R) {
if(s[id] > d) return {-1, 0};
return {s[id], 1};
}
int x = 2 * id + 1, y = x + 1, M = (L + R) / 2;
if(L == l && R == r) {
if(s[id] < d) return {s[id], 1};
pint pr = query(y, M + 1, R, l, r, d);
if(pr.ss == 0) return pr;
pint pl = query(x, L, M, l, r, d);
return {max(pl.ff, pr.ff), 0};
}
if(r <= M) return query(x, L, M, l, r, d);
if(M + 1 <= l) return query(y, M + 1, R, l, r, d);
pint pr = query(y, M + 1, R, l, r, d);
if(pr.ss == 0) return pr;
pint pl = query(x, L, M, l, r, d);
return {max(pl.ff, pr.ff), pl.ss};
}
int main() {
int t, n;
cin >> t >> n;
for(int i = 0; i < n; i++) cin >> h[i];
build(0, 0, n - 1);
while(t--) {
int r, l, p;
cin >> l >> r >> p;
cout << query(0, 0, n - 1, l, r, p).ff << '\n';
}
}