#include <bits/stdc++.h>
#define MAX_N 1000000
using namespace std;
int w[MAX_N];
struct nod {
int maxEffort, maxW;
};
vector <int> v[4 * MAX_N];
nod aint[4 * MAX_N];
nod ans;
void init( int nod, int l, int r ) {
int mid, i, j;
if ( l == r ) {
aint[nod].maxEffort = 0;
aint[nod].maxW = w[l];
v[nod].push_back( w[l] );
return;
}
mid = (l + r) / 2;
init( nod * 2 + 1, l, mid );
init( nod * 2 + 2, mid + 1, r );
int st, dr, mij, c;
st = 0;
dr = v[nod * 2 + 2].size();
while ( st - dr > 1 ) {
mij = (l + r) / 2;
if ( v[nod * 2 + 2][mij] < aint[nod * 2 + 1].maxW )
st = mij;
else
dr = mij;
}
c = v[nod * 2 + 2][st] < aint[nod * 2 + 1].maxW ? v[nod * 2 + 2][st] : -aint[nod * 2 + 1].maxW;
aint[nod].maxW = max( aint[nod * 2 + 1].maxW, aint[nod * 2 + 2].maxW );
aint[nod].maxEffort = max( max( aint[nod * 2 + 1].maxEffort, aint[nod * 2 + 2].maxEffort ), aint[nod * 2 + 1].maxW + c );
i = j = 0;
while ( i < v[nod * 2 + 1].size() && j < v[nod * 2 + 2].size() ) {
if ( v[nod * 2 + 1][i] < v[nod * 2 + 2][j] ) {
v[nod].push_back( v[nod * 2 + 1][i] );
i++;
} else {
v[nod].push_back( v[nod * 2 + 2][j] );
j++;
}
}
while ( i < v[nod * 2 + 1].size() ) {
v[nod].push_back( v[nod * 2 + 1][i] );
i++;
}
while ( j < v[nod * 2 + 2].size() ) {
v[nod].push_back( v[nod * 2 + 2][j] );
j++;
}
}
void query( int nod, int l, int r, int lq, int rq ) {
int mid;
if ( nod == 0 )
ans = { 0, 0 };
if ( l > rq || r < lq )
return;
if ( lq <= l && r <= rq ) {
int l, r, m, c;
l = 0;
r = v[nod].size();
while ( r - l > 1 ) {
m = (l + r) / 2;
if ( v[nod][m] < ans.maxW )
l = m;
else
r = m;
}
c = v[nod][l] < ans.maxW ? v[nod][l] : -ans.maxW;
ans.maxEffort = max( max( ans.maxEffort, aint[nod].maxEffort ), ans.maxW + c );
ans.maxW = max( ans.maxW, aint[nod].maxW );
return;
}
mid = (l + r) / 2;
query( nod * 2 + 1, l, mid, lq, rq );
query( nod * 2 + 2, mid + 1, r, lq, rq );
}
int main() {
int n, m, l, r, k, i;
cin >> n >> m;
for ( i = 0; i < n; i++ )
cin >> w[i];
init( 0, 0, n - 1 );
for ( i = 0; i < m; i++ ) {
cin >> l >> r >> k;
query( 0, 0, n - 1, l - 1, r - 1 );
if ( ans.maxEffort > k )
cout << "0\n";
else
cout << "1\n";
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'void init(int, int, int)':
sortbooks.cpp:47:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | while ( i < v[nod * 2 + 1].size() && j < v[nod * 2 + 2].size() ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:47:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | while ( i < v[nod * 2 + 1].size() && j < v[nod * 2 + 2].size() ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:56:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | while ( i < v[nod * 2 + 1].size() ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:60:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | while ( j < v[nod * 2 + 2].size() ) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94156 KB |
Output is correct |
2 |
Correct |
52 ms |
94144 KB |
Output is correct |
3 |
Incorrect |
55 ms |
94228 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94156 KB |
Output is correct |
2 |
Correct |
52 ms |
94144 KB |
Output is correct |
3 |
Incorrect |
55 ms |
94228 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3104 ms |
248708 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
530 ms |
110316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94156 KB |
Output is correct |
2 |
Correct |
52 ms |
94144 KB |
Output is correct |
3 |
Incorrect |
55 ms |
94228 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94156 KB |
Output is correct |
2 |
Correct |
52 ms |
94144 KB |
Output is correct |
3 |
Incorrect |
55 ms |
94228 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |