#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define ff first
#define ss second
const int mxl = 15;
const int mxn = 5050;
vector<vi> sus(mxl, vi (mxn));
vector<vi> tmp(mxl, vi (mxn));
int a[mxn], n, m;
struct con {
int ans, max;
con() {ans = max = 0;}
} str[mxn * 4];
void ono_max(int &MAX, int CMP) { if(MAX < CMP) MAX = CMP;}
void ono_min(int &MIN, int CMP) { if(MIN > CMP) MIN = CMP;}
void build(int idx, int lvl, int tl, int tr) {
if(tl == tr) {
// cout << tl << ": " << a[tl] << '\n';
sus[lvl][tl] = a[tl];
str[idx].max = a[tl];
str[idx].ans = 0;
return;
}
int tm = (tl + tr) / 2;
build(idx * 2 + 1, lvl + 1, tl, tm);
build(idx * 2 + 2, lvl + 1, tm + 1, tr);
int L = tl, R = tm + 1, i = tl;
while(1) {
if(L == tm + 1 && R == tr + 1) break;
if(L == tm + 1) {
sus[lvl][i] = sus[lvl + 1][R];
i++, R++;
continue;
}
if(R == tr + 1) {
sus[lvl][i] = sus[lvl + 1][L];
i++, L++;
continue;
}
if(sus[lvl + 1][L] <= sus[lvl + 1][R]) {
sus[lvl][i] = sus[lvl + 1][L];
L++, i++;
}
else {
sus[lvl][i] = sus[lvl + 1][R];
ono_max(str[idx].ans, str[idx * 2 + 1].max + sus[lvl][i]);
R++, i++;
}
}
ono_max(str[idx].max, str[idx * 2 + 1].max);
ono_max(str[idx].max, str[idx * 2 + 2].max);
ono_max(str[idx].ans, str[idx * 2 + 1].ans);
ono_max(str[idx].ans, str[idx * 2 + 2].ans);
// cout << tl << ' ' << tr << ":\n";
// for(int i = tl; i <= tr; i++)
// cout << sus[lvl][i] << ' ';
// cout << "- " << str[idx].max << ' ' << str[idx].ans << '\n';
}
pair<con, int> query(int idx, int lvl, int tl, int tr, int l, int r) {
if(tl == l && r == tr) return {str[idx], lvl};
int tm = (tl + tr) / 2;
if(r <= tm) return query(idx * 2 + 1, lvl + 1, tl, tm, l, r);
if(tm < l) return query(idx * 2 + 2, lvl + 1, tm + 1, tr, l, r);
pair<con, int> L_qr = query(idx * 2 + 1, lvl + 1, tl, tm, l, tm);
pair<con, int> R_qr = query(idx * 2 + 2, lvl + 1, tm + 1, tr, tm + 1, r);
con ret;
int L = l, R = tm + 1, i = l;
vi &L_next = (L_qr.ss > 0) ? sus[L_qr.ss] : tmp[-L_qr.ss];
vi &R_next = (R_qr.ss > 0) ? sus[R_qr.ss] : tmp[-R_qr.ss];
while(1) {
if(L == tm + 1 && R == tr + 1) break;
if(L == tm + 1) {
tmp[lvl][i] = R_next[R];
i++, R++;
continue;
}
if(R == tr + 1) {
tmp[lvl][i] = L_next[L];
i++, L++;
continue;
}
if(L_next[L] <= R_next[R]) {
tmp[lvl][i] = L_next[L];
L++, i++;
}
else {
tmp[lvl][i] = R_next[R];
ono_max(ret.ans, L_qr.ff.max + tmp[lvl][i]);
R++, i++;
}
}
ono_max(ret.ans, L_qr.ff.ans);
ono_max(ret.ans, R_qr.ff.ans);
ono_max(ret.max, L_qr.ff.max);
ono_max(ret.max, R_qr.ff.max);
return {ret, -lvl};
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
build(0, 0, 1, n);
// for(int i = 1; i <= n; i++)
// cout << sus[0][i] << ' ';
// for(int i = 0; i < 5; i++) {
// cout << "level: " << i << '\n';
// for(int j = 1; j <= n; j++)
// cout << sus[i][j] << ' ';
// cout << '\n';
// }
while(m--) {
int l, r, k;
cin >> l >> r >> k;
pair<con, bool> q = query(0, 0, 1, n, l, r);
cout << (q.ff.ans <= k) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
2136 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
2136 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |