#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
struct SegmentTree
{
int n;
ll tree[4 * MAXN];
ll w[MAXN];
void init(int _n, ll _w[])
{
n = _n;
for(int i = 1; i <= n; i++)
{
w[i] = _w[i];
}
}
void update(int left, int right, int idx, int pos, ll val)
{
if(pos < left || right < pos)
return;
if(left == right)
{
tree[idx] = max(tree[idx], val + w[left]);
return;
}
int mid = left + (right - left) / 2;
update(left, mid, 2 * idx, pos, val);
update(mid + 1, right, 2 * idx + 1, pos, val);
tree[idx] = max(tree[2 * idx], tree[2 * idx + 1]);
}
ll query(int left, int right, int idx, int qleft, int qright)
{
if(qright < left || right < qleft)
return 0;
if(qleft <= left && right <= qright)
return tree[idx];
int mid = left + (right - left) / 2;
ll Lnode = query(left, mid, 2 * idx, qleft, qright);
ll Rnode = query(mid + 1, right, 2 * idx + 1, qleft, qright);
return max(Lnode, Rnode);
}
void update(int pos, ll val)
{
update(1, n, 1, pos, val);
}
ll query(int left, int right)
{
return query(1, n, 1, left, right);
}
};
struct qry
{
int left, right;
ll k;
int idx;
qry()
{
left = right = 1;
k = 0;
idx = 1;
}
qry(int _left, int _right, ll _k, int _idx)
{
left = _left;
right = _right;
k = _k;
idx = _idx;
}
friend bool operator<(const qry&q1, const qry&q2)
{
if(q1.right != q2.right)
return q1.right < q2.right;
if(q1.left != q2.left)
return q1.left < q2.left;
if(q1.k != q2.k)
return q1.k < q2.k;
return q1.idx < q2.idx;
}
};
int n, q;
ll w[MAXN];
bool ans[MAXN];
vector < qry > queries;
SegmentTree t;
void read()
{
cin >> n >> q;
for(int i = 1; i <= n; i++)
{
cin >> w[i];
}
for(int i = 1; i <= q; i++)
{
int left, right;
ll k;
cin >> left >> right >> k;
qry cur = qry(left, right, k, i);
queries.push_back(cur);
}
}
void solve()
{
t.init(n, w);
sort(queries.begin(), queries.end());
stack < int > st;
int ptr = 1;
for(qry c : queries)
{
int left = c.left;
int right = c.right;
ll k = c.k;
int idx = c.idx;
while(ptr <= right)
{
while(!st.empty() && w[st.top()] <= w[ptr])
st.pop();
if(!st.empty())
t.update(st.top(), w[ptr]);
st.push(ptr);
ptr++;
}
ll cur = t.query(left, right);
ans[idx] = (cur <= k);
}
}
void print()
{
for(int i = 1; i <= q; i++)
{
cout << ans[i] << endl;
}
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
print();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
3 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6880 KB |
Output is correct |
15 |
Correct |
3 ms |
6804 KB |
Output is correct |
16 |
Correct |
3 ms |
6940 KB |
Output is correct |
17 |
Correct |
3 ms |
6784 KB |
Output is correct |
18 |
Correct |
3 ms |
6684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
790 ms |
59424 KB |
Output is correct |
2 |
Correct |
732 ms |
59296 KB |
Output is correct |
3 |
Correct |
724 ms |
59300 KB |
Output is correct |
4 |
Correct |
738 ms |
59396 KB |
Output is correct |
5 |
Correct |
624 ms |
43428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
11772 KB |
Output is correct |
2 |
Correct |
59 ms |
11328 KB |
Output is correct |
3 |
Correct |
54 ms |
9104 KB |
Output is correct |
4 |
Correct |
56 ms |
9244 KB |
Output is correct |
5 |
Correct |
54 ms |
9144 KB |
Output is correct |
6 |
Correct |
46 ms |
9152 KB |
Output is correct |
7 |
Correct |
47 ms |
9148 KB |
Output is correct |
8 |
Correct |
51 ms |
9588 KB |
Output is correct |
9 |
Correct |
32 ms |
9160 KB |
Output is correct |
10 |
Correct |
46 ms |
9152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
3 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6880 KB |
Output is correct |
15 |
Correct |
3 ms |
6804 KB |
Output is correct |
16 |
Correct |
3 ms |
6940 KB |
Output is correct |
17 |
Correct |
3 ms |
6784 KB |
Output is correct |
18 |
Correct |
3 ms |
6684 KB |
Output is correct |
19 |
Correct |
139 ms |
18608 KB |
Output is correct |
20 |
Correct |
139 ms |
19120 KB |
Output is correct |
21 |
Correct |
128 ms |
17840 KB |
Output is correct |
22 |
Correct |
141 ms |
17976 KB |
Output is correct |
23 |
Correct |
130 ms |
17844 KB |
Output is correct |
24 |
Correct |
115 ms |
13764 KB |
Output is correct |
25 |
Correct |
107 ms |
14788 KB |
Output is correct |
26 |
Correct |
121 ms |
14380 KB |
Output is correct |
27 |
Correct |
121 ms |
14256 KB |
Output is correct |
28 |
Correct |
118 ms |
14772 KB |
Output is correct |
29 |
Correct |
121 ms |
14256 KB |
Output is correct |
30 |
Correct |
118 ms |
14532 KB |
Output is correct |
31 |
Correct |
132 ms |
14512 KB |
Output is correct |
32 |
Correct |
123 ms |
15028 KB |
Output is correct |
33 |
Correct |
125 ms |
14256 KB |
Output is correct |
34 |
Correct |
101 ms |
14452 KB |
Output is correct |
35 |
Correct |
103 ms |
14448 KB |
Output is correct |
36 |
Correct |
101 ms |
14284 KB |
Output is correct |
37 |
Correct |
108 ms |
14492 KB |
Output is correct |
38 |
Correct |
102 ms |
14276 KB |
Output is correct |
39 |
Correct |
110 ms |
15936 KB |
Output is correct |
40 |
Correct |
85 ms |
12728 KB |
Output is correct |
41 |
Correct |
110 ms |
14028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
3 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6880 KB |
Output is correct |
15 |
Correct |
3 ms |
6804 KB |
Output is correct |
16 |
Correct |
3 ms |
6940 KB |
Output is correct |
17 |
Correct |
3 ms |
6784 KB |
Output is correct |
18 |
Correct |
3 ms |
6684 KB |
Output is correct |
19 |
Correct |
790 ms |
59424 KB |
Output is correct |
20 |
Correct |
732 ms |
59296 KB |
Output is correct |
21 |
Correct |
724 ms |
59300 KB |
Output is correct |
22 |
Correct |
738 ms |
59396 KB |
Output is correct |
23 |
Correct |
624 ms |
43428 KB |
Output is correct |
24 |
Correct |
61 ms |
11772 KB |
Output is correct |
25 |
Correct |
59 ms |
11328 KB |
Output is correct |
26 |
Correct |
54 ms |
9104 KB |
Output is correct |
27 |
Correct |
56 ms |
9244 KB |
Output is correct |
28 |
Correct |
54 ms |
9144 KB |
Output is correct |
29 |
Correct |
46 ms |
9152 KB |
Output is correct |
30 |
Correct |
47 ms |
9148 KB |
Output is correct |
31 |
Correct |
51 ms |
9588 KB |
Output is correct |
32 |
Correct |
32 ms |
9160 KB |
Output is correct |
33 |
Correct |
46 ms |
9152 KB |
Output is correct |
34 |
Correct |
139 ms |
18608 KB |
Output is correct |
35 |
Correct |
139 ms |
19120 KB |
Output is correct |
36 |
Correct |
128 ms |
17840 KB |
Output is correct |
37 |
Correct |
141 ms |
17976 KB |
Output is correct |
38 |
Correct |
130 ms |
17844 KB |
Output is correct |
39 |
Correct |
115 ms |
13764 KB |
Output is correct |
40 |
Correct |
107 ms |
14788 KB |
Output is correct |
41 |
Correct |
121 ms |
14380 KB |
Output is correct |
42 |
Correct |
121 ms |
14256 KB |
Output is correct |
43 |
Correct |
118 ms |
14772 KB |
Output is correct |
44 |
Correct |
121 ms |
14256 KB |
Output is correct |
45 |
Correct |
118 ms |
14532 KB |
Output is correct |
46 |
Correct |
132 ms |
14512 KB |
Output is correct |
47 |
Correct |
123 ms |
15028 KB |
Output is correct |
48 |
Correct |
125 ms |
14256 KB |
Output is correct |
49 |
Correct |
101 ms |
14452 KB |
Output is correct |
50 |
Correct |
103 ms |
14448 KB |
Output is correct |
51 |
Correct |
101 ms |
14284 KB |
Output is correct |
52 |
Correct |
108 ms |
14492 KB |
Output is correct |
53 |
Correct |
102 ms |
14276 KB |
Output is correct |
54 |
Correct |
110 ms |
15936 KB |
Output is correct |
55 |
Correct |
85 ms |
12728 KB |
Output is correct |
56 |
Correct |
110 ms |
14028 KB |
Output is correct |
57 |
Correct |
719 ms |
59808 KB |
Output is correct |
58 |
Correct |
730 ms |
60064 KB |
Output is correct |
59 |
Correct |
723 ms |
59304 KB |
Output is correct |
60 |
Correct |
720 ms |
60452 KB |
Output is correct |
61 |
Correct |
723 ms |
59816 KB |
Output is correct |
62 |
Correct |
725 ms |
59300 KB |
Output is correct |
63 |
Correct |
581 ms |
44160 KB |
Output is correct |
64 |
Correct |
541 ms |
43684 KB |
Output is correct |
65 |
Correct |
656 ms |
43376 KB |
Output is correct |
66 |
Correct |
601 ms |
42920 KB |
Output is correct |
67 |
Correct |
609 ms |
43056 KB |
Output is correct |
68 |
Correct |
615 ms |
43940 KB |
Output is correct |
69 |
Correct |
625 ms |
42916 KB |
Output is correct |
70 |
Correct |
630 ms |
43152 KB |
Output is correct |
71 |
Correct |
622 ms |
42920 KB |
Output is correct |
72 |
Correct |
609 ms |
42988 KB |
Output is correct |
73 |
Correct |
535 ms |
43060 KB |
Output is correct |
74 |
Correct |
549 ms |
45216 KB |
Output is correct |
75 |
Correct |
555 ms |
47012 KB |
Output is correct |
76 |
Correct |
528 ms |
47264 KB |
Output is correct |
77 |
Correct |
569 ms |
45048 KB |
Output is correct |
78 |
Correct |
571 ms |
48548 KB |
Output is correct |
79 |
Correct |
435 ms |
40356 KB |
Output is correct |
80 |
Correct |
554 ms |
43428 KB |
Output is correct |