#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <cmath>
#include <random>
#include <chrono>
using namespace std;
#define int long long
int log_ = 10;
int inf = 1000000000000000000;
int mod = 998244353;
struct segment_tree
{
vector <int> mx;
vector <int> mn;
vector <int> mx_d;
int s;
void init(int n)
{
s = 1;
while (s < n)
s *= 2;
mx.resize(2 * s);
mn.resize(2 * s);
mx_d.resize(2 * s);
}
void build(int ind, int l, int r, vector <int>& a)
{
if (r - l == 1)
{
if (l < a.size())
{
mx[ind] = a[l];
mn[ind] = a[l];
mx_d[ind] = a[l];
return;
}
mx[ind] = 0;
mn[ind] = 0;
mx_d[ind] = 0;
return;
}
int m = (l + r) / 2;
build(ind * 2 + 1, l, m, a);
build(ind * 2 + 2, m, r, a);
mx[ind] = max(mx[ind * 2 + 1], mx[ind * 2 + 2]);
mn[ind] = min(mn[ind * 2 + 1], mn[ind * 2 + 2]);
mx_d[ind] = max(mx_d[ind * 2 + 1], mx_d[ind * 2 + 2]);
if (mx[ind * 2 + 1] > mn[ind * 2 + 2])
mx_d[ind] = max(mx_d[ind], max(0ll, mx[ind * 2 + 1] + mn[ind * 2 + 2]));
}
void build(vector <int>& a)
{
build(0, 0, s, a);
}
pair <pair <int, int>, int> get(int ind, int l, int r, int lx, int rx)
{
if ((lx <= l) and (r <= rx))
{
return { {mx[ind] , mn[ind]} , mx_d[ind] };
}
if ((r <= lx) or (rx <= l))
{
return { {-1 , -1} , -1 };
}
int m = (l + r) / 2;
pair <pair <int, int>, int> g1 = get(ind * 2 + 1, l, m, lx, rx);
pair <pair <int, int>, int> g2 = get(ind * 2 + 2, m, r, lx, rx);
if (g1.second == -1)
return g2;
if (g2.second == -1)
return g1;
pair <pair <int, int>, int> g;
g.first.first = max(g1.first.first, g2.first.first);
g.first.second = min(g1.first.second, g2.first.second);
g.second = max(g1.second, g2.second);
if (g1.first.first > g2.first.second)
g.second = max(g.second, max(0ll, g1.first.first + g2.first.second));
return g;
}
int get(int l, int r)
{
pair <pair <int, int>, int> g = get(0, 0, s, l, r);
return g.second;
}
};
signed main()
{
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
segment_tree st;
vector <int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
st.init(n);
st.build(a);
for (int i = 0; i < m; i++)
{
int l, r , k;
cin >> l >> r >> k;
l--;
int q = st.get(l, r);
if (q <= k)
cout << 1 << "\n";
else
cout << 0 << "\n";
}
}
Compilation message
sortbooks.cpp: In member function 'void segment_tree::build(long long int, long long int, long long int, std::vector<long long int>&)':
sortbooks.cpp:46:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if (l < a.size())
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2174 ms |
59508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
145 ms |
7580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |