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;
typedef vector<int> vi;
const int mxn = 1e6 + 10;
vi a(mxn);
int n, m;
struct node {
int mx, mn;
bool flag;
} st[mxn * 4];
void build(int id, int tl, int tr) {
if(tl == tr) {
st[id].mn = a[tl];
st[id].mx = a[tr];
st[id].flag = 1;
return;
}
int tm = (tl + tr) / 2;
build(id * 2 + 1, tl, tm);
build(id * 2 + 2, tm + 1, tr);
st[id].mx = max(st[id * 2 + 1].mx, st[id * 2 + 2].mx);
st[id].mn = min(st[id * 2 + 1].mn, st[id * 2 + 2].mn);
st[id].flag = 1;
if(!st[id * 2 + 1].flag) st[id].flag = 0;
if(!st[id * 2 + 2].flag) st[id].flag = 0;
if(st[id * 2 + 1].mx > st[id * 2 + 2].mn)
st[id].flag = 0;
}
node query(int id, int tl, int tr, int l, int r) {
if(tl == l && r == tr) return st[id];
int tm = (tl + tr) / 2;
if(r <= tm) return query(id * 2 + 1, tl, tm, l, r);
if(tm < l) return query(id * 2 + 2, tm + 1, tr, l, r);
node L_qr = query(id * 2 + 1, tl, tm, l, tm);
node R_qr = query(id * 2 + 2, tm + 1, tr, tm + 1, r);
node ret; ret.flag = 1;
ret.mx = max(L_qr.mx, R_qr.mx);
ret.mn = min(L_qr.mn, R_qr.mn);
if(!L_qr.flag) ret.flag = 0;
if(!R_qr.flag) ret.flag = 0;
if(L_qr.mx > R_qr.mn) ret.flag = 0;
return ret;
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
build(0, 1, n);
while(m--) {
int l, r, k;
cin >> l >> r >> k;
cout << query(0, 1, n, l, r).flag << '\n';
}
return 0;
}
# | 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... |