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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
constexpr int INF = 1e9+42;
struct Antenna {
int h, a, b;
};
struct Query {
int l, r, qid;
bool operator<(const Query& other) const {
return r == other.r ? l < other.l : r < other.r;
}
};
struct nodepos {
int maxfull = -INF, bestheight = +INF;
nodepos() {}
nodepos(int maxfull, int bestheight) : maxfull(maxfull), bestheight(bestheight) {}
void update(nodepos k) {
bestheight = k.bestheight;
}
nodepos merge(const nodepos& other) const {
return nodepos(max(maxfull, other.maxfull), min(bestheight, other.bestheight));
}
void apply(int h) {
maxfull = max(maxfull, h-bestheight);
}
};
struct nodeneg {
int maxfull = -INF, bestheight = -INF;
nodeneg() {}
nodeneg(int maxfull, int bestheight) : maxfull(maxfull), bestheight(bestheight) {}
void update(nodeneg k) {
bestheight = k.bestheight;
}
nodeneg merge(const nodeneg& other) const {
return nodeneg(max(maxfull, other.maxfull), max(bestheight, other.bestheight));
}
void apply(int h) {
maxfull = max(maxfull, bestheight-h);
}
};
int idpos() { return -INF; }
int idneg() { return +INF; }
ostream& operator<<(ostream& out, const nodepos k) {
return out << k.maxfull << ":" << k.bestheight << ":p";
}
ostream& operator<<(ostream& out, const nodeneg k) {
return out << k.maxfull << ":" << k.bestheight << ":n";
}
template <class node, class fun, fun(*id)()>
class lazy {
int N;
vector<pair<node,fun>> tree;
public:
lazy(const int __N) : N(1<<__lg(2*__N-1)) {
tree.resize(2*N);
}
void update(int req, node val, int l = 0, int r = -1, int idx = 1) {
if (r == -1) r = N-1;
if (req < l || req > r) return;
else if (req == l && req == r) {
tree[idx].first.update(val);
} else {
pushval(idx);
int mid = (l+r)>>1;
update(req, val, l, mid, 2*idx);
update(req, val, mid+1, r, 2*idx+1);
tree[idx].first = tree[2*idx].first.merge(tree[2*idx+1].first);
}
}
void update(int reql, int reqr, fun val, int l = 0, int r = -1, int idx = 1) {
if (r == -1) r = N-1;
if (reqr < l || reql > r) return;
else if (reql <= l && r <= reqr) {
pushval(idx);
tree[idx].second = val;
tree[idx].first.apply(val);
} else {
pushval(idx);
int mid = (l+r)>>1;
update(reql, reqr, val, l, mid, 2*idx);
update(reql, reqr, val, mid+1, r, 2*idx+1);
tree[idx].first = tree[2*idx].first.merge(tree[2*idx+1].first);
}
}
node query(int reql, int reqr, int l = 0, int r = -1, int idx = 1) {
if (r == -1) r = N-1;
if (reqr < l || reql > r) return node{};
else if (reql <= l && r <= reqr) {
return tree[idx].first;
} else {
pushval(idx);
int mid = (l+r)>>1;
return query(reql, reqr, l, mid, 2*idx).merge(query(reql, reqr, mid+1, r, 2*idx+1));
}
}
private:
void pushval(int idx) {
if (idx < N) {
tree[2*idx].first.apply(tree[idx].second);
tree[2*idx+1].first.apply(tree[idx].second);
}
tree[idx].second = id();
}
};
int N, Q;
vector<Antenna> A;
vector<vector<Query>> qbyR;
vector<vector<pair<int,bool>>> events;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> N;
A.resize(N);
events.resize(N+1);
for (int i = 0; i < N; i++) {
cin >> A[i].h >> A[i].a >> A[i].b;
events[min(i+A[i].a, N)].emplace_back(i, true);
events[min(i+A[i].b+1, N)].emplace_back(i, false);
}
cin >> Q;
//queries.resize(Q);
qbyR.resize(N);
for (int i = 0; i < Q; i++) {
Query q;
cin >> q.l >> q.r, q.l--, q.r--;
q.qid = i;
qbyR[q.r].push_back(q);
}
vector<int> ans(Q);
lazy<nodepos,int,idpos> treepos(N);
lazy<nodeneg,int,idneg> treeneg(N);
for (int R = 0; R < N; R++) {
for (auto [k, type] : events[R]) {
if (type) {
treepos.update(k, {-INF, A[k].h});
treeneg.update(k, {-INF, A[k].h});
} else {
treepos.update(k, {-INF, +INF});
treeneg.update(k, {-INF, -INF});
}
}
if (R-A[R].a >= 0) {
treepos.update(max(0, R-A[R].b), max(0, R-A[R].a), A[R].h);
treeneg.update(max(0, R-A[R].b), max(0, R-A[R].a), A[R].h);
}
for (auto& q : qbyR[R]) {
ans[q.qid] = max(-1, max(treepos.query(q.l, q.r).maxfull, treeneg.query(q.l, q.r).maxfull));
}
}
for (int i = 0; i < Q; i++) {
cout << ans[i] << '\n';
}
}
Compilation message (stderr)
antennas.cpp: In function 'int main()':
antennas.cpp:144:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
144 | for (auto [k, type] : events[R]) {
| ^
# | 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... |