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>
#define FOR(i,x,n) for(int i=x; i<n; i++)
#define F0R(i,n) FOR(i,0,n)
#define ROF(i,x,n) for(int i=n-1; i>=x; i--)
#define R0F(i,n) ROF(i,0,n)
#define WTF cout << "WTF" << endl
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int MAXN = 1e6 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
struct Query {
int lo, hi, k;
int id;
inline bool operator < (const Query other) {
return hi < other.hi;
}
} qs[MAXN];
int n, q;
int ns[MAXN], agh[MAXN];
int tree[MAXN << 2], ans[MAXN];
int get(int lq, int rq, int now_on = 1, int ls = 0, int rs = n - 1) {
if (rq < lq || rq < ls || rs < lq)
return -INF;
if (lq <= ls && rs <= rq)
return tree[now_on];
int mid = (ls + rs) >> 1;
return max(get(lq, rq, now_on << 1, ls, mid), get(lq, rq, now_on << 1 | 1, mid + 1, rs));
}
void change(int id, int val, int now_on = 1, int ls = 0, int rs = n - 1) {
if (ls == rs) {
tree[now_on] = val;
return;
}
int mid = (ls + rs) >> 1;
if (id <= mid) change(id, val, now_on << 1, ls, mid);
else change(id, val, now_on << 1 | 1, mid + 1, rs);
tree[now_on] = max(tree[now_on << 1], tree[now_on << 1 | 1]);
return;
}
int main() {
IOS;
cin >> n >> q;
F0R(i, n) cin >> ns[i];
stack<int> keep;
F0R(i, n) {
while(!keep.empty() && ns[keep.top()] <= ns[i])
keep.pop();
if (keep.empty()) agh[i] = -1;
else agh[i] = keep.top();
keep.push(i);
}
/*
F0R(i, n) cout << agh[i] << ' ';
cout << endl;
*/
F0R(i, q) {
cin >> qs[i].lo >> qs[i].hi >> qs[i].k;
qs[i].lo--; qs[i].hi--; qs[i].id = i;
}
sort(qs, qs + q);
int ptr = 0;
F0R(i, n) {
if (agh[i] != -1) {
//cout << "for i = " << i << " changing " << agh[i] << " to " << ns[ agh[i] ] + ns[i] << endl;
change(agh[i], ns[ agh[i] ] + ns[i]);
}
while(ptr < q && qs[ptr].hi <= i) {
ans[ qs[ptr].id ] = (get(qs[ptr].lo, qs[ptr].hi) <= qs[ptr].k);
ptr++;
}
}
#define endl '\n'
F0R(i, q) cout << ans[i] << endl;
}
# | 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... |