이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
typedef complex<double> cd; // !!! long double slow
template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }
int n, q;
const int INF = 1000 * 1000 * 1000 + 10;
const int MX = 4 * 1000 * 1000;
struct Seg {
int mx[MX], t[MX];
vi sl[MX];
Seg(vi& a) {
forn(i, MX)
mx[i] = t[i] = -INF;
// mx.assign(4 * n, -INF);
// t.assign(4 * n, -INF);
// sl.resize(4 * n);
build(a);
}
void build(vi& a, int v = 1, int tl = 0, int tr = n - 1) {
if(tl == tr) {
mx[v] = a[tl];
sl[v].pb(a[tl]);
return;
}
int tm = (tl + tr) / 2;
build(a, 2 * v, tl, tm);
build(a, 2 * v + 1, tm + 1, tr);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
merge(all(sl[2 * v]), all(sl[2 * v + 1]), back_inserter(sl[v]));
t[v] = max(t[2 * v], t[2 * v + 1]);
int ind = lower_bound(all(sl[2 * v + 1]), mx[2 * v]) - sl[2 * v + 1].begin();
if(ind != 0)
maxeq(t[v], mx[2 * v] + sl[2 * v + 1][ind - 1]);
}
pair<bool, int> query(int l, int r, int k, int left_max, int v = 1, int tl = 0, int tr = n - 1) {
if(l > r)
return {true, -INF};
if(tl == l && r == tr) {
bool ans = t[v] <= k;
int rp = left_max - 1, lp = k - left_max + 1;
if(lp <= rp) {
int indl = lower_bound(all(sl[v]), lp) - sl[v].begin();
int indr = lower_bound(all(sl[v]), rp + 1) - sl[v].begin();
ans = ans && indl == indr;
}
return {ans, mx[v]};
}
int tm = (tl + tr) / 2;
auto q1 = query(l, min(r, tm), k, left_max, 2 * v, tl, tm);
auto q2 = query(max(l, tm + 1), r, k, max(left_max, q1.S), 2 * v + 1, tm + 1, tr);
return {q1.F && q2.F, max(q1.S, q2.S)};
}
};
void solve() {
cin >> n >> q;
vi a(n);
forn(i, n)
cin >> a[i];
Seg seg(a);
forn(_, q) {
int l, r, k;
cin >> l >> r >> k;
l--; r--;
auto ans = seg.query(l, r, k, -INF);
cout << ans.F << '\n';
}
// forn(k, 15) {
// auto ans = seg.query(0, n - 1, k, (k + 1) / 2, (k - 1) / 2);
// cout << k << ' ' << ans.F << '\n';
// }
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// freopen("boards.in", "r", stdin);
// freopen("boards.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |