#include <bits/stdc++.h>
#define Y8o "Hedgehog Daniyar and Algorithms"
#define maxn (int) 1e6 + 5
#define ll long long
#define pii pair<ll, ll>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
//#define f first
//#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
int n, Q, a[maxn];
int L[maxn][20], Log[maxn], pre[maxn];
vector<int> cur[maxn];
struct dl { int l, r, k; } qr[maxn];
int get(int i, int j) {
int kc = Log[j - i + 1];
return max( L[i][kc], L[j - (1 << kc) + 1][kc] );
}
int st[4 * maxn];
struct SEGMENTREE {
void build(int id = 1, int l = 1, int r = n) {
if(l == r) {
if(pre[l] != 0) st[id] = a[pre[l]] + a[l];
return ;
}
int mid = (l + r) >> 1;
build(_left), build(_right);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
void update(int i, int id = 1, int l = 1, int r = n) {
if(i < l || r < i) return ;
if(l == r) return st[id] = 0, void();
int mid = (l + r) >> 1;
update(i, _left), update(i, _right);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int get(int u, int v, int id = 1, int l = 1, int r = n) {
if(v < l || r < u) return 0;
if(u <= l && r <= v) return st[id];
int mid = (l + r) >> 1;
return max( get(u, v, _left), get(u, v, _right) );
}
} ST;
void solve()
{
cin >> n >> Q;
for(int i = 1; i <= n; i ++) cin >> a[i], L[i][0] = a[i], Log[i] = __lg(i);
for(int i = 1; i <= Log[n]; i ++) {
for(int j = 1; j + (1 << i) - 1 <= n; j ++) {
L[j][i] = max(L[j][i - 1], L[j + (1 << (i - 1))][i - 1]);
}
}
for(int i = 1; i <= n; i ++) {
int l = 1, r = i;
while(l <= r) {
int mid = (l + r) >> 1;
if(get(mid, i) > a[i]) l = mid + 1;
else r = mid - 1;
}
pre[i] = r;
cur[r].push_back(i);
}
ST.build();
for(int i =1 ; i <= Q; i ++) {
int l, r, k; cin >> l >> r >> k;
qr[i] = {l, r, k};
}
sort(qr + 1, qr + Q + 1, [](dl x, dl y) {
return x.l < y.l;
});
int j = 1;
for(int i = 1; i <= Q; i ++) {
int l = qr[i].l, r = qr[i].r, k = qr[i].k;
while(j < l) {
for(int pos : cur[j]) ST.update(pos);
j ++;
}
if(ST.get(l, r) <= k) cout << 1 << '\n';
else cout << 0 << '\n';
}
}
int main()
{
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message
sortbooks.cpp: In function 'void iof()':
sortbooks.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
33112 KB |
Output is correct |
2 |
Correct |
4 ms |
33116 KB |
Output is correct |
3 |
Incorrect |
5 ms |
33116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
33112 KB |
Output is correct |
2 |
Correct |
4 ms |
33116 KB |
Output is correct |
3 |
Incorrect |
5 ms |
33116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
705 ms |
153176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
59 ms |
47540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
33112 KB |
Output is correct |
2 |
Correct |
4 ms |
33116 KB |
Output is correct |
3 |
Incorrect |
5 ms |
33116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
33112 KB |
Output is correct |
2 |
Correct |
4 ms |
33116 KB |
Output is correct |
3 |
Incorrect |
5 ms |
33116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |