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 int long long
using namespace std;
const int INF = 1.05e9;
struct Node{
int mxA, mnA, mxB, mnB;
int cost;
Node(){
mxA = mxB = -INF;
mnA = mnB = INF;
cost = -INF;
}
};
struct SMT{
int n;
vector<Node> tree;
SMT(int n = 0) : n(n), tree(n<<2|3, Node()) {}
void down(int id){
if(tree[id].mxB == -INF && tree[id].mnB == INF) return;
int mx = tree[id].mxB;
int mn = tree[id].mnB;
int ID = id<<1;
tree[ID].mxB = max(tree[ID].mxB, mx);
tree[ID].mnB = min(tree[ID].mnB, mn);
tree[ID].cost = max(tree[ID].cost, tree[ID].mxA - tree[ID].mnB);
tree[ID].cost = max(tree[ID].cost, tree[ID].mxB - tree[ID].mnA);
tree[ID|1].mxB = max(tree[ID|1].mxB, mx);
tree[ID|1].mnB = min(tree[ID|1].mnB, mn);
tree[ID|1].cost = max(tree[ID|1].cost, tree[ID|1].mxA - tree[ID|1].mnB);
tree[ID|1].cost = max(tree[ID|1].cost, tree[ID|1].mxB - tree[ID|1].mnA);
tree[id].mxB = -INF;
tree[id].mnB = INF;
}
void updateA(int pos, int val){
int id = 1;
for(int lo=1, hi=n; lo<hi;){
int mid = (lo + hi)>>1;
down(id);
if(pos <= mid) id = id<<1, hi = mid;
else lo = mid + 1, id = id<<1 | 1;
}
tree[id].mxA = tree[id].mnA = val;
tree[id].mxB = -INF;
tree[id].mnB = INF;
for(id>>=1; id; id>>=1){
tree[id].cost = max(tree[id<<1].cost, tree[id<<1|1].cost);
tree[id].mxA = max(tree[id<<1].mxA, tree[id<<1|1].mxA);
tree[id].mnA = min(tree[id<<1].mnA, tree[id<<1|1].mnA);
}
}
void deleteA(int pos){
int id = 1;
for(int lo=1, hi=n; lo<hi;){
int mid = (lo + hi)>>1;
down(id);
if(pos <= mid) id = id<<1, hi = mid;
else lo = mid + 1, id = id<<1 | 1;
}
tree[id].mxA = tree[id].mxB = -INF;
tree[id].mnA = tree[id].mnB = INF;
for(id>>=1; id; id>>=1){
tree[id].cost = max(tree[id<<1].cost, tree[id<<1|1].cost);
tree[id].mxA = max(tree[id<<1].mxA, tree[id<<1|1].mxA);
tree[id].mnA = min(tree[id<<1].mnA, tree[id<<1|1].mnA);
}
}
void updateB(int l, int r, int id, const int &u, const int &v, const int &val){
if(l > v || r < u) return;
if(l >= u && r <= v){
tree[id].mxB = max(tree[id].mxB, val);
tree[id].mnB = min(tree[id].mnB, val);
tree[id].cost = max(tree[id].cost, tree[id].mxB - tree[id].mnA);
tree[id].cost = max(tree[id].cost, tree[id].mxA - tree[id].mnB);
return;
}
down(id);
int mid = (l + r)>>1;
updateB(l, mid, id<<1, u, v, val);
updateB(mid + 1, r, id<<1|1, u, v, val);
tree[id].cost = max(tree[id<<1].cost, tree[id<<1|1].cost);
tree[id].mxA = max(tree[id<<1].mxA, tree[id<<1|1].mxA);
tree[id].mnA = min(tree[id<<1].mnA, tree[id<<1|1].mnA);
}
void updateB(int l, int r, int val){
updateB(1, n, 1, l, r, val);
}
int get(int l, int r, int id, const int &u, const int &v){
if(l > v || r < u) return -1;
if(l >= u && r <= v) return tree[id].cost;
down(id);
int mid = (l + r)>>1;
return max(get(l, mid, id<<1, u, v), get(mid + 1, r, id<<1|1, u, v));
}
int get(int l, int r){
return get(1, n, 1, l, r);
}
};
const int MX = 400005;
int n, q;
int h[MX], a[MX], b[MX];
int ans[MX];
vector<int> G[MX];
vector<pair<int, int>> T[MX];
void nhap(){
cin >> n;
for(int i=1; i<=n; i++){
cin >> h[i] >> a[i] >> b[i];
G[i + a[i]].push_back(i);
G[i + b[i] + 1].push_back(-i);
}
cin >> q;
for(int i=1; i<=q; i++){
int l, r;
cin >> l >> r;
T[r].push_back({l, i});
}
}
void solve(){
SMT smt(n);
for(int i=1; i<=n; i++){
for(int j: G[i]){
if(j > 0) smt.updateA(j, h[j]);
if(j < 0) smt.deleteA(-j);
}
smt.updateB(i - b[i], i - a[i], h[i]);
for(pair<int, int> qr: T[i]){
int j, id;
tie(j, id) = qr;
ans[id] = smt.get(j, i);
if(ans[id] < 0) ans[id] = -1;
}
}
for(int i=1; i<=q; i++) cout << ans[i] << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
nhap();
solve();
return 0;
}
# | 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... |