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 "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
vi H;
struct segtree {
struct node {
// what do I need?
int mxans=-1;
array<int,2> h = {oo,-oo}, lazy = {oo,-oo};
node() {}
};
vector<node> seg;
int n,ptwo;
segtree(int nn) {
n=nn,ptwo=1;
while(ptwo<n) ptwo*=2;
seg.resize(2*ptwo);
}
void puttag(int i, array<int,2> lazy) {
auto& v = seg[i];
v.mxans = max({v.mxans,lazy[1]-v.h[0],v.h[1]-lazy[0]});
v.lazy[0] = min(v.lazy[0],lazy[0]);
v.lazy[1] = max(v.lazy[1],lazy[1]);
}
void pull(int i) {
auto& v = seg[i], &l = seg[i*2], &r = seg[i*2+1];
v.mxans = max(l.mxans,r.mxans);
v.h[0] = min(l.h[0],r.h[0]);
v.h[1] = max(l.h[1],r.h[1]);
}
void push(int i) { // TODO!
auto& v = seg[i];
puttag(i*2,v.lazy);
puttag(i*2+1,v.lazy);
v.lazy = {oo,-oo};
}
int get(int i, int l, int r, int ql, int qr) {
if(qr<l or r<ql) return -1;
if(ql<=l and r<=qr) {
return seg[i].mxans;
}
int mid = (l+r)/2;
push(i);
auto res = max(get(i*2,l,mid,ql,qr),get(i*2+1,mid+1,r,ql,qr)); // TODO
pull(i);
return res;
}
void set(int i, int l, int r, int ql, int qr, int v) {
if(qr<l or r<ql) return;
if(ql<=l and r<=qr) {
puttag(i,{v,v}); // correct?
return;
}
int mid = (l+r)/2;
push(i);
set(i*2,l,mid,ql,qr,v),set(i*2+1,mid+1,r,ql,qr,v);
pull(i);
}
void set2(int i, int l, int r, int q) {
if(q<l or r<q) return;
if(l==q and r==q) {
if(seg[i].h[0]==oo) seg[i].h = {H[l],H[l]};
else seg[i].h = {oo,-oo};
return;
}
int mid = (l+r)/2;
push(i);
set2(i*2,l,mid,q),set2(i*2+1,mid+1,r,q);
pull(i);
}
void set(int l, int r, ll v) {
set(1,0,ptwo-1,l,r,v);
}
void set2(int i) {
set2(1,0,ptwo-1,i);
}
int get(int l, int r) {
return get(1,0,ptwo-1,l,r);
}
node& operator[](int i) {return seg[i+ptwo];}
void build() {
for(int i=ptwo-1;i>=1;--i) {
pull(i);
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
H.resize(n);
vi a(n),b(n);
for(int i=0;i<n;++i) {
cin >> H[i] >> a[i] >> b[i];
}
vvi activations(n+1);
for(int i=0;i<n;++i) {
activations[min(n,i+a[i])].push_back(i);
activations[min(n,i+b[i]+1)].push_back(i);
}
int Q; cin >> Q;
vector<array<int,2>> qs(Q);
vvi ids(n);
{int id=0;
for(auto& [l,ans] : qs) {
int r; cin >> l >> r;
--l,--r;
ids[r].push_back(id++);
}
}
segtree seg(n);
seg.build();
for(int j=0;j<n;++j) {
int l = max(0,j-b[j]), r = max(-1,j-a[j]);
for(auto i : activations[j])
seg.set2(i);
if(r>=0) {
seg.set(l,r,H[j]);
}
for(int id : ids[j]) {
int lb = qs[id][0];
qs[id][1] = seg.get(lb,j);
}
}
for(auto [l,ans] : qs) {
cout << ans << '\n';
}
}
Compilation message (stderr)
antennas.cpp: In function 'int main()':
antennas.cpp:115:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
115 | for(auto& [l,ans] : qs) {
| ^
antennas.cpp:135:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
135 | for(auto [l,ans] : qs) {
| ^
# | 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... |