이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 20);
struct SegTree {
int n;
int st[2 * maxn];
SegTree() {}
SegTree(int _n) {
if (_n == 1) {
n = _n;
} else {
n = (1 << (31 - __builtin_clz(_n - 1) + 1));
}
for (int i = 0; i < 2 * n; ++i) {
st[i] = 0;
}
}
void update(int v, int tl, int tr, int i, int val) {
// cerr << "v: " << v << ", tl: " << tl << ", tr: " << tr << ", i: " << i << ", val: " << val << '\n';
if (tl + 1 == tr) {
st[v] = val;
return;
}
int m = tl + (tr - tl) / 2;
if (i < m) {
update(2 * v, tl, m, i, val);
} else {
update(2 * v + 1, m, tr, i, val);
}
st[v] = max(st[2 * v], st[2 * v + 1]);
}
void update(int i, int val) {
return update(1, 0, n, i, val);
}
int getMax(int v, int tl, int tr, int l, int r) {
if (l <= tl && tr <= r) {
return st[v];
}
int m = tl + (tr - tl) / 2, res = 0;
if (l < m) {
res = max(res, getMax(2 * v, tl, m, l, r));
}
if (m < r) {
res = max(res, getMax(2 * v + 1, m, tr, l, r));
}
return res;
}
int getMax(int l, int r) {
return getMax(1, 0, n, l, r);
}
~SegTree() {}
};
int w[maxn], leftGr[maxn], ans[maxn];
struct Event {
int l, r, idx;
Event() {}
Event(int _l, int _r, int _idx) {
l = _l, r = _r, idx = _idx;
}
bool operator<(const Event& other) const {
return make_pair(other.l, idx) < make_pair(l, other.idx);
}
~Event() {}
};
Event evs[2 * maxn];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> w[i];
}
vector<int> stk;
for (int i = 0; i < n; ++i) {
while (!stk.empty() && w[i] >= w[stk.back()]) {
stk.pop_back();
}
if (!stk.empty()) {
leftGr[i] = stk.back();
} else {
leftGr[i] = -1;
}
stk.push_back(i);
evs[i] = Event(leftGr[i], i, -1);
}
for (int i = 0; i < m; ++i) {
cin >> evs[n + i].l >> evs[n + i].r >> ans[i];
--evs[n + i].l, --evs[n + i].r;
evs[n + i].idx = i;
}
sort(evs, evs + n + m);
SegTree st(n);
for (int i = 0; i < n + m && evs[i].l != -1; ++i) {
if (evs[i].idx == -1) {
st.update(evs[i].r, w[evs[i].l] + w[evs[i].r]);
} else {
//vcerr << "idx: " << evs[i].idx << '\n';
//vcerr << "mx: " << st.getMax(evs[i].l, evs[i].r + 1) << '\n';
if (st.getMax(evs[i].l, evs[i].r + 1) <= ans[evs[i].idx]) {
ans[evs[i].idx] = 1;
} else {
ans[evs[i].idx] = 0;
}
}
}
for (int i = 0; i < m; ++i) {
cout << ans[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
/*
4 1
4 3 5 1
1 4 6
*/
# | 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... |