#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
using ush = unsigned short;
const ll INF = 1e9 + 177013;
const int MAXN = 1e6 + 123;
const int SHORT = 1 << 16;
int w[MAXN];
int pv[MAXN];
int get_max(int l, int r) {
int mx = -INF;
int ans = 0;
for (int i = l; i < r; ++i) {
if (mx > w[i])
ans = max(ans, mx + w[i]);
else
mx = w[i];
}
return ans;
}
void merg_add(vector<pair<int, int>>& a, pair<int, int> vl) {
while (a.size() && a.back().first == vl.first) {
a.pop_back();
}
a.push_back(vl);
}
vector<pair<int, int>> merg(vector<pair<int, int>> a, vector<pair<int, int>> b) {
vector<pair<int, int>> ans;
int n = a.size();
int m = b.size();
int j = 0;
for (int i = 0; i < n; ++i) {
while (j < m && b[j] < a[i]) {
merg_add(ans, b[j]);
++j;
}
merg_add(ans, a[i]);
}
while (j < m) {
merg_add(ans, b[j]);
++j;
}
int s = ans.size();
for (int i = s - 2; i >= 0; --i) {
ans[i].second = max(ans[i].second, ans[i + 1].second);
}
return ans;
}
struct bitr {
void resize(int n) {
t.resize(n);
}
void add(int i, int x) {
while (i < t.size()) {
t[i].push_back(x);
i |= i + 1;
}
}
void init() {
for (int i = 0; i < t.size(); ++i)
sort(t[i].begin(), t[i].end());
}
int getcnt(int i, int btr) {
int ans = 0;
while (i >= 0) {
ans += t[i].end() - upper_bound(t[i].begin(), t[i].end(), btr);
i &= i + 1;
--i;
}
return ans;
}
private:
vector<vector<int>> t;
};
mt19937 rng(177013);
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i)
cin >> w[i];
//while (n < 1000000) {
//w[n++] = rng() % INF;
//}
vector<pair<int, int>> previnit = {{INF, -1}};
bitr tr;
tr.resize(n);
for (int i = 0; i < n; ++i) {
while (previnit.back().first <= w[i]) {
previnit.pop_back();
}
pv[i] = previnit.back().second;
previnit.push_back({w[i], i});
if (pv[i] > -1) {
tr.add(i, w[i] + w[pv[i]]);
}
}
tr.init();
for (int i = 0; i < m; ++i) {
int l, r, k;
cin >> l >> r >> k;
--l;
if (tr.getcnt(r - 1, k) > tr.getcnt(l - 1, k)) {
cout << 0 << '\n';
} else {
cout << 1 << '\n';
}
}
}
Compilation message
sortbooks.cpp: In member function 'void bitr::add(int, int)':
sortbooks.cpp:63:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | while (i < t.size()) {
| ~~^~~~~~~~~~
sortbooks.cpp: In member function 'void bitr::init()':
sortbooks.cpp:70:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0; i < t.size(); ++i)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2240 ms |
99496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
202 ms |
9996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |