#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const long long MAXN = 1e6+10;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
long long n, m;
long long a[MAXN];
long long l, r, k;
long long inpair[MAXN];
long long pairw[MAXN];
void read_array()
{
cin >> n >> m;
stack < long long > s;
for (long long i = 1; i <= n; ++ i)
{
cin >> a[i];
while(!s.empty() && a[s.top()] <= a[i])
s.pop();
if(!s.empty())
{
inpair[i] = s.top();
pairw[i] = a[i] + a[s.top()];
}
s.push(i);
}
/**cout << "**" << endl;
for (long long i = 1;i <= n; ++ i)
cout << inpair[i] << " ";
cout << endl;
cout << "**" << endl;
for (long long i = 1;i <= n; ++ i)
cout << pairw[i] << " ";
cout << endl;*/
}
struct que
{
long long ql, qr, w, id;
que(){};
que(long long _ql, long long _qr, long long _w, long long _id)
{
ql = _ql;
qr = _qr;
w = _w;
id = _id;
}
};
vector < que > g;
bool cmp(que q1, que q2)
{
if(q1.qr != q2.qr)return(q1.qr < q2.qr);
return (q1.id < q2.id);
}
long long t[MAXN * 4];
long long ql, qr;
long long query(long long i, long long l, long long r)
{
if(ql <= l && r <= qr)return t[i];
if(qr < l || ql > r)return 0;
long long mid = (l + r)/2;
return max(query(2*i, l, mid), query(2*i+1, mid+1, r));
}
long long pos, val;
void update(long long i, long long l, long long r)
{
if(l == r)
{
t[i] = max(t[i], val);
return;
}
long long mid = (l + r)/2;
if(pos <= mid)update(2*i, l, mid);
else update(2*i+1, mid+1, r);
t[i] = max(t[2*i], t[2*i+1]);
}
long long ans[MAXN];
void queries()
{
for (long long i = 1; i <= m; ++ i)
{
cin >> l >> r >> k;
g.push_back(que(l, r, k, i));
}
sort(g.begin(), g.end(), cmp);
long long last = 0;
long long qll, qrr, ww, idd;
for (long long i = 0; i < g.size(); ++ i)
{
qll = g[i].ql;
qrr = g[i].qr;
ww = g[i].w;
idd = g[i].id;
// cout << qll << " " << qrr << " " << ww << " " << idd << endl;
for (long long j = last+1; j <= qrr; ++ j)
{
if(!inpair[j])continue;
pos = inpair[j];
val = pairw[j];
// cout << "in " << pos << " " << val << endl;
update(1, 1, n);
}
ql = qll;
qr = qrr-1;
long long res = query(1, 1, n);
// cout << "final res " << res << endl;
if(res <= ww)ans[idd] = 1;
last = qrr;
}
for (long long i =1; i <= m; ++ i)
{
cout << ans[i] << endl;
}
}
int main()
{
speed();
read_array();
queries();
return 0;
}
Compilation message
sortbooks.cpp: In function 'void queries()':
sortbooks.cpp:96:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<que>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (long long i = 0; i < g.size(); ++ i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
1248 KB |
Output is correct |
15 |
Correct |
4 ms |
1224 KB |
Output is correct |
16 |
Correct |
3 ms |
992 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
3 ms |
960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
902 ms |
105584 KB |
Output is correct |
2 |
Correct |
871 ms |
106620 KB |
Output is correct |
3 |
Correct |
864 ms |
106368 KB |
Output is correct |
4 |
Correct |
920 ms |
106624 KB |
Output is correct |
5 |
Correct |
739 ms |
82476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
10940 KB |
Output is correct |
2 |
Correct |
70 ms |
10940 KB |
Output is correct |
3 |
Correct |
62 ms |
7288 KB |
Output is correct |
4 |
Correct |
62 ms |
7360 KB |
Output is correct |
5 |
Correct |
61 ms |
7360 KB |
Output is correct |
6 |
Correct |
57 ms |
7120 KB |
Output is correct |
7 |
Correct |
58 ms |
7124 KB |
Output is correct |
8 |
Correct |
62 ms |
8392 KB |
Output is correct |
9 |
Correct |
39 ms |
5872 KB |
Output is correct |
10 |
Correct |
61 ms |
8124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
1248 KB |
Output is correct |
15 |
Correct |
4 ms |
1224 KB |
Output is correct |
16 |
Correct |
3 ms |
992 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
3 ms |
960 KB |
Output is correct |
19 |
Correct |
158 ms |
24056 KB |
Output is correct |
20 |
Correct |
165 ms |
23992 KB |
Output is correct |
21 |
Correct |
152 ms |
23988 KB |
Output is correct |
22 |
Correct |
162 ms |
24000 KB |
Output is correct |
23 |
Correct |
160 ms |
23988 KB |
Output is correct |
24 |
Correct |
139 ms |
16560 KB |
Output is correct |
25 |
Correct |
128 ms |
16584 KB |
Output is correct |
26 |
Correct |
139 ms |
17012 KB |
Output is correct |
27 |
Correct |
133 ms |
16816 KB |
Output is correct |
28 |
Correct |
139 ms |
16816 KB |
Output is correct |
29 |
Correct |
136 ms |
16816 KB |
Output is correct |
30 |
Correct |
148 ms |
16944 KB |
Output is correct |
31 |
Correct |
138 ms |
16848 KB |
Output is correct |
32 |
Correct |
138 ms |
16820 KB |
Output is correct |
33 |
Correct |
138 ms |
16824 KB |
Output is correct |
34 |
Correct |
127 ms |
16564 KB |
Output is correct |
35 |
Correct |
130 ms |
16568 KB |
Output is correct |
36 |
Correct |
125 ms |
16308 KB |
Output is correct |
37 |
Correct |
126 ms |
16668 KB |
Output is correct |
38 |
Correct |
132 ms |
16444 KB |
Output is correct |
39 |
Correct |
131 ms |
18020 KB |
Output is correct |
40 |
Correct |
116 ms |
14840 KB |
Output is correct |
41 |
Correct |
129 ms |
16816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
4 ms |
1248 KB |
Output is correct |
15 |
Correct |
4 ms |
1224 KB |
Output is correct |
16 |
Correct |
3 ms |
992 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
3 ms |
960 KB |
Output is correct |
19 |
Correct |
902 ms |
105584 KB |
Output is correct |
20 |
Correct |
871 ms |
106620 KB |
Output is correct |
21 |
Correct |
864 ms |
106368 KB |
Output is correct |
22 |
Correct |
920 ms |
106624 KB |
Output is correct |
23 |
Correct |
739 ms |
82476 KB |
Output is correct |
24 |
Correct |
74 ms |
10940 KB |
Output is correct |
25 |
Correct |
70 ms |
10940 KB |
Output is correct |
26 |
Correct |
62 ms |
7288 KB |
Output is correct |
27 |
Correct |
62 ms |
7360 KB |
Output is correct |
28 |
Correct |
61 ms |
7360 KB |
Output is correct |
29 |
Correct |
57 ms |
7120 KB |
Output is correct |
30 |
Correct |
58 ms |
7124 KB |
Output is correct |
31 |
Correct |
62 ms |
8392 KB |
Output is correct |
32 |
Correct |
39 ms |
5872 KB |
Output is correct |
33 |
Correct |
61 ms |
8124 KB |
Output is correct |
34 |
Correct |
158 ms |
24056 KB |
Output is correct |
35 |
Correct |
165 ms |
23992 KB |
Output is correct |
36 |
Correct |
152 ms |
23988 KB |
Output is correct |
37 |
Correct |
162 ms |
24000 KB |
Output is correct |
38 |
Correct |
160 ms |
23988 KB |
Output is correct |
39 |
Correct |
139 ms |
16560 KB |
Output is correct |
40 |
Correct |
128 ms |
16584 KB |
Output is correct |
41 |
Correct |
139 ms |
17012 KB |
Output is correct |
42 |
Correct |
133 ms |
16816 KB |
Output is correct |
43 |
Correct |
139 ms |
16816 KB |
Output is correct |
44 |
Correct |
136 ms |
16816 KB |
Output is correct |
45 |
Correct |
148 ms |
16944 KB |
Output is correct |
46 |
Correct |
138 ms |
16848 KB |
Output is correct |
47 |
Correct |
138 ms |
16820 KB |
Output is correct |
48 |
Correct |
138 ms |
16824 KB |
Output is correct |
49 |
Correct |
127 ms |
16564 KB |
Output is correct |
50 |
Correct |
130 ms |
16568 KB |
Output is correct |
51 |
Correct |
125 ms |
16308 KB |
Output is correct |
52 |
Correct |
126 ms |
16668 KB |
Output is correct |
53 |
Correct |
132 ms |
16444 KB |
Output is correct |
54 |
Correct |
131 ms |
18020 KB |
Output is correct |
55 |
Correct |
116 ms |
14840 KB |
Output is correct |
56 |
Correct |
129 ms |
16816 KB |
Output is correct |
57 |
Correct |
917 ms |
114948 KB |
Output is correct |
58 |
Correct |
880 ms |
114924 KB |
Output is correct |
59 |
Correct |
892 ms |
115040 KB |
Output is correct |
60 |
Correct |
875 ms |
114928 KB |
Output is correct |
61 |
Correct |
903 ms |
114936 KB |
Output is correct |
62 |
Correct |
878 ms |
115068 KB |
Output is correct |
63 |
Correct |
710 ms |
80972 KB |
Output is correct |
64 |
Correct |
688 ms |
81136 KB |
Output is correct |
65 |
Correct |
735 ms |
82980 KB |
Output is correct |
66 |
Correct |
769 ms |
83064 KB |
Output is correct |
67 |
Correct |
732 ms |
82820 KB |
Output is correct |
68 |
Correct |
794 ms |
83064 KB |
Output is correct |
69 |
Correct |
738 ms |
83064 KB |
Output is correct |
70 |
Correct |
754 ms |
82892 KB |
Output is correct |
71 |
Correct |
781 ms |
82924 KB |
Output is correct |
72 |
Correct |
750 ms |
83068 KB |
Output is correct |
73 |
Correct |
673 ms |
79736 KB |
Output is correct |
74 |
Correct |
682 ms |
80760 KB |
Output is correct |
75 |
Correct |
681 ms |
79728 KB |
Output is correct |
76 |
Correct |
678 ms |
79480 KB |
Output is correct |
77 |
Correct |
750 ms |
79500 KB |
Output is correct |
78 |
Correct |
698 ms |
87672 KB |
Output is correct |
79 |
Correct |
579 ms |
70328 KB |
Output is correct |
80 |
Correct |
733 ms |
85236 KB |
Output is correct |