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>
#define ll long long
#define f first
#define s second
#define pb push_back
#define N 200005
#define mp make_pair
using namespace std;
ll save[6*N],inf=1000000000000000,pown=1;
ll n,q,a[N],b[N],h[N],kl[N],kr[N],ans[N];
pair<ll,ll>t[6*N];
vector<ll>st[N],en[N];
vector<pair<ll,ll> >ge[N];
void updpoint(ll x,ll L,ll R,ll ind,ll val){
if(L == R){
if(val == -inf){
t[x].s = -inf;
save[x] = inf;
return;
}
t[x].f = max(t[x].f , t[x].s - val);
t[x].s = val;
save[x] = val;
return;
}
if(save[x] != inf){
save[2 * x] = min(save[x] , save[2 * x]);
t[2 * x].f = max(t[2 * x].f , t[2 * x].s - save[x]);
save[2 * x + 1] = min(save[x] , save[2 * x + 1]);
t[2 * x + 1].f = max(t[2 * x + 1].f , t[2 * x + 1].s - save[x]);
save[x] = inf;
}
ll mid = (L + R) / 2;
if(mid >= ind)updpoint(2 * x , L , mid , ind , val);
else updpoint(2 * x + 1 , mid + 1 , R , ind , val);
t[x].f = max(t[2 * x].f , t[2 * x + 1].f);
t[x].s = max(t[2 * x].s , t[2 * x + 1].s);
}
void upd(ll x,ll L,ll R,ll l,ll r,ll val){
if(L > r || R < l)return;
if(L >= l && R <= r){
save[x] = min(save[x] , val);
t[x].f = max(t[x].f , t[x].s - save[x]);
return;
}
if(save[x] != inf){
save[2 * x] = min(save[x] , save[2 * x]);
t[2 * x].f = max(t[2 * x].f , t[2 * x].s - save[x]);
save[2 * x + 1] = min(save[x] , save[2 * x + 1]);
t[2 * x + 1].f = max(t[2 * x + 1].f , t[2 * x + 1].s - save[x]);
save[x] = inf;
}
ll mid = (L + R) / 2;
upd(2 * x , L , mid , l , r , val);
upd(2 * x + 1 , mid + 1 , R , l , r , val);
t[x].f = max(t[2 * x].f , t[2 * x + 1].f);
t[x].s = max(t[2 * x].s , t[2 * x + 1].s);
}
ll get(ll x,ll L,ll R,ll l,ll r){
if(L > r || R < l)return -inf;
if(L >= l && R <= r)return t[x].f;
if(save[x] != inf){
save[2 * x] = min(save[x] , save[2 * x]);
t[2 * x].f = max(t[2 * x].f , t[2 * x].s - save[x]);
save[2 * x + 1] = min(save[x] , save[2 * x + 1]);
t[2 * x + 1].f = max(t[2 * x + 1].f , t[2 * x + 1].s - save[x]);
save[x] = inf;
}
ll mid = (L + R) / 2;
ll k1 = get(2 * x , L , mid , l , r);
ll k2 = get(2 * x + 1 , mid + 1 , R , l , r);
return max(k1 , k2);
}
void solve(){
for(int i=1; i<=n; i++){
st[i].clear();
en[i].clear();
ge[i].clear();
}
for(int i=1; i<=2 * pown; i++){
save[i] = inf;
t[i].f = -inf;
t[i].s = -inf;
}
for(int i=1; i<=q; i++){
ge[kr[i]].pb(mp(kl[i] , i));
}
for(int i=1; i<=n; i++){
if(i + a[i] <= n)
st[i + a[i]].pb(i);
if(i + b[i] <= n)
en[i + b[i]].pb(i);
}
for(int i=1; i<=n; i++){
for(int j=0; j<st[i].size(); j++)
updpoint(1 , 1 , pown , st[i][j] , h[st[i][j]]);
ll l = 1 , r = 0;
if(i - b[i] >= 1)l = i - b[i];
if(i - a[i] >= 1)r = i - a[i];
//cout << l << " " << r << " " << h[i] << '\n';
if(r >= l)
upd(1 , 1 , pown , l , r , h[i]);
for(int j=0; j<ge[i].size(); j++){
ll cur = get(1 , 1 , pown , ge[i][j].f , i);
//cout << i << " " << ge[i][j].f << " " << i << " " << cur << '\n';
ans[ge[i][j].s] = max(ans[ge[i][j].s] , cur);
}
for(int j=0; j<en[i].size(); j++)
updpoint(1 , 1 , pown , en[i][j] , -inf);
}
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
while(pown <= n)
pown *= 2;
for(int i=1; i<=n; i++){
cin >> h[i] >> a[i] >> b[i];
}
cin >> q;
for(int i=1; i<=q; i++){
cin >> kl[i] >> kr[i];
ans[i] = -1;
}
solve();
for(int i=1; i<=n / 2; i++){
swap(h[i] , h[n - i + 1]);
swap(a[i] , a[n - i + 1]);
swap(b[i] , b[n - i + 1]);
}
for(int i=1; i<=q; i++){
ll f = kl[i];
kl[i] = n - kr[i] + 1;
kr[i] = n - f + 1;
}
solve();
for(int i=1; i<=q; i++)
cout << ans[i] << '\n';
return 0;
}
Compilation message (stderr)
antennas.cpp: In function 'void solve()':
antennas.cpp:96:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<st[i].size(); j++)
~^~~~~~~~~~~~~
antennas.cpp:104:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<ge[i].size(); j++){
~^~~~~~~~~~~~~
antennas.cpp:109:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<en[i].size(); j++)
~^~~~~~~~~~~~~
# | 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... |