# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
751958 | grogu | Two Antennas (JOI19_antennas) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
#define ld double
#define ll long long
#define llinf 1000000000000000000LL
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
using namespace std;
#define maxn 200005
ll n,q;
ll h[maxn],a[maxn],b[maxn];
pll v[maxn];
ll t[4*maxn],t2[4*maxn],lazy[4*maxn];
ll ans[maxn];
vector<pll> kv[maxn];
vector<ll> add[maxn],del[maxn];
ll ls[4*maxn],rs[4*maxn],tsz = 0,root = 0;
void init(ll &v,ll tl,ll tr){
v = ++tsz;
t[v] = t2[v] = lazy[v] = -llinf;
if(tl==tr) return;
ll mid = (tl+tr)/2;
init(ls[v],tl,mid);
init(rs[v],mid+1,tr);
}
void push(ll v){
if(lazy[v]!=-llinf){
t[v] = max(t[v],t2[v]+x);
if(ls[v]) lazy[ls[v]] = max(lazy[ls[v]],lazy[v]);
if(rs[v]) lazy[rs[v]] = max(lazy[rs[v]],lazy[v]);
lazy[v] = -llinf;
}
}
void upd2(ll v,ll tl,ll tr,ll i,ll x){
push(v);
if(tl==tr){t2[v] = x;return;}
ll mid = (tl+tr)/2;
if(i<=mid) upd2(ls[v],tl,mid,i,x);
else upd2(rs[v],mid+1,tr,i,x);
t2[v] = max(t2[ls[v]],t2[rs[v]]);
}
void upd(ll v,ll tl,ll tr,ll l,ll r,ll x){
push(v);
if(tl>tr||r<tl||l>tr) return;
if(tl>=l&&tr<=r){lazy[v] = x;push(v);return;}
ll mid = (tl+tr)/2;
upd(ls[v],tl,mid,l,r,x);
upd(rs[v],mid+1,tr,l,r,x);
t[v] = max(t[ls[v]],t[rs[v]]);
}
ll get(ll v,ll tl,ll tr,ll l,ll r){
push(v);
if(l>r) return -llinf;
if(tl==l&&tr==r) return t[v];
ll mid = (tl+tr)/2;
return max(get(ls[v],tl,mid,l,min(mid,r)),get(rs[v],mid+1,tr,max(mid+1,l),r));
}
void solve(){
init(root,1,n);
for(ll i = 1;i<=n;i++){
ll l = min(n+1,i+a[i]);
ll r = min(n+1,i+b[i]);
add[l].pb(i);
del[r].pb(i);
for(ll j : add[i]) upd2(root,1,n,j,h[j]);
upd(root,1,n,i-b[i],i-a[i],-h[i]);
for(pll p : kv[i]) ans[p.sc] = max(ans[p.sc],get(root,1,n,p.fi,i));
for(ll j : del[i]) upd2(root,1,n,j,-llinf);
add[i].clear();
del[i].clear();
}
for(ll i = 0;i<=tsz;i++) rs[i] = ls[i] = 0;
root = tsz = 0;
}
void tc(){
cin >> n;
for(ll i = 1;i<=n;i++) cin >> h[i] >> a[i] >> b[i];
cin >> q;
for(ll i = 1;i<=q;i++) cin >> v[i].fi >> v[i].sc;
for(ll i = 1;i<=q;i++) kv[v[i].sc].pb({v[i].fi,i}),ans[i] = -1;
solve();
for(ll i = 1;i<=n;i++) h[i] = -h[i];
solve();
for(ll i = 1;i<=q;i++) cout<<ans[i]<<endl;
}
int main(){
int t; t = 1;
while(t--){
tc();
}
return 0;
}