This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "towers.h"
#include "bits/stdc++.h"
using namespace std;
struct segtree {
vector<int> tree;
int size;
void init(int n) {
size = 1;
while (size < n) size *= 2;
tree.assign(2 * size, 0);
}
void upd(int i, int v) {
i += size;
tree[i] = max(tree[i], v);
for (; i > 1; i >>= 1) tree[i >> 1] = max(tree[i], tree[i ^ 1]);
}
int get(int l, int r) {
if (l > r) return 0;
int res = 0;
for (l += size, r += size + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = max(res, tree[l++]);
if (r & 1) res = max(res, tree[--r]);
}
return res;
}
};
int n;
vector<int> a;
segtree sg1, sg2;
void init(int _n, std::vector<int> _a) {
n = _n;
a = _a;
}
int max_towers(int l, int r, int d) {
vector<int> all, v;
for (int i = 0; i < n; i++) {
all.emplace_back(a[i]);
all.emplace_back(a[i] - d);
all.emplace_back(a[i] + d);
}
sort(all.begin(), all.end());
for (int i = 0; i < all.size(); i++) {
if (i > 0 and all[i] == all[i - 1]) continue;
v.emplace_back(all[i]);
}
int z = all.size();
sg1.init(z + 1); sg2.init(z + 1);
int ans = 0;
for (int i = l; i <= r; i++) {
int pos = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
int pos2 = lower_bound(v.begin(), v.end(), a[i] + d) - v.begin();
ans = max(ans, sg1.get(pos2, z) + 1);
sg2.upd(pos2, sg1.get(pos2, z) + 1);
sg1.upd(pos, sg2.get(0, pos));
}
return ans;
}
Compilation message (stderr)
towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 0; i < all.size(); i++) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |