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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
/*
refs:
https://codeforces.com/blog/entry/66022?#comment-500622
https://mzhang2021.github.io/cp-blog/historic-segtree/ (historic sums segment tree)
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
template<typename T>
struct lazysegtree {
/*=======================================================*/
struct data {
ll a,b;
};
struct lazy {
ll sum,mx_pref;
};
data d_neutral = {-inf2,-inf2};
lazy l_neutral = {0,0};
void merge(data &curr, data &left, data &right) {
curr.a = max(left.a,right.a);
curr.b = max(left.b,right.b);
}
void create(int x, int lx, int rx, T v) {
}
void modify(int x, int lx, int rx, T v) {
if(v.ff == 1){
lz[x].sum = v.ss;
lz[x].mx_pref = max(v.ss,0ll);
}
else{
tr[x].a = v.ss;
amax(tr[x].b,v.ss);
}
}
void propagate(int x, int lx, int rx) {
auto [sum,mx_pref] = lz[x];
amax(tr[x].b,tr[x].a+mx_pref);
tr[x].a += sum;
if(rx-lx > 1){
lz[2*x+1].mx_pref = max(lz[2*x+1].mx_pref,lz[2*x+1].sum+mx_pref);
lz[2*x+1].sum += sum;
lz[2*x+2].mx_pref = max(lz[2*x+2].mx_pref,lz[2*x+2].sum+mx_pref);
lz[2*x+2].sum += sum;
}
lz[x] = l_neutral;
}
/*=======================================================*/
int siz = 1;
vector<data> tr;
vector<lazy> lz;
lazysegtree() {
}
lazysegtree(int n) {
while (siz < n) siz *= 2;
tr.assign(2 * siz, d_neutral);
lz.assign(2 * siz, l_neutral);
}
void build(vector<T> &a, int n, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < n) {
create(x, lx, rx, a[lx]);
}
return;
}
int mid = (lx + rx) / 2;
build(a, n, 2 * x + 1, lx, mid);
build(a, n, 2 * x + 2, mid, rx);
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void build(vector<T> &a, int n) {
build(a, n, 0, 0, siz);
}
void rupd(int l, int r, T v, int x, int lx, int rx) {
propagate(x, lx, rx);
if (lx >= r or rx <= l) return;
if (lx >= l and rx <= r) {
modify(x, lx, rx, v);
propagate(x, lx, rx);
return;
}
int mid = (lx + rx) / 2;
rupd(l, r, v, 2 * x + 1, lx, mid);
rupd(l, r, v, 2 * x + 2, mid, rx);
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void rupd(int l, int r, T v) {
rupd(l, r + 1, v, 0, 0, siz);
}
data query(int l, int r, int x, int lx, int rx) {
propagate(x, lx, rx);
if (lx >= r or rx <= l) return d_neutral;
if (lx >= l and rx <= r) return tr[x];
int mid = (lx + rx) / 2;
data curr;
data left = query(l, r, 2 * x + 1, lx, mid);
data right = query(l, r, 2 * x + 2, mid, rx);
merge(curr, left, right);
return curr;
}
data query(int l, int r) {
return query(l, r + 1, 0, 0, siz);
}
};
void solve(int test_case)
{
ll n; cin >> n;
vector<ll> a(n+5);
vector<pll> b(n+5);
rep1(i,n) cin >> a[i] >> b[i].ff >> b[i].ss;
ll q; cin >> q;
vector<pll> queries(q+5);
rep1(i,q) cin >> queries[i].ff >> queries[i].ss;
vector<ll> ans(q+5,-inf2);
auto go = [&](){
vector<ll> enter[n+5], leave[n+5];
vector<pll> queries_here[n+5];
lazysegtree<pll> st(n+5);
rep1(i,n){
auto [l,r] = b[i];
if(i+l <= n){
enter[i+l].pb(i);
}
if(i+r+1 <= n){
leave[i+r+1].pb(i);
}
}
rep1(i,q){
auto [l,r] = queries[i];
queries_here[r].pb({l,i});
}
rep1(i,n){
trav(j,enter[i]){
st.rupd(j,j,{2,-a[j]});
}
trav(j,leave[i]){
st.rupd(j,j,{2,-inf2});
}
auto [l,r] = b[i];
l = i-l, r = i-r;
if(l > r) swap(l,r);
amax(l,1ll);
st.rupd(l,r,{1,a[i]});
st.rupd(l,r,{1,-a[i]});
for(auto [l,id] : queries_here[i]){
amax(ans[id],st.query(l,i).b);
}
}
};
go();
reverse(a.begin()+1,a.begin()+n+1);
reverse(b.begin()+1,b.begin()+n+1);
rep1(i,q){
auto &[l,r] = queries[i];
l = n-l+1;
r = n-r+1;
swap(l,r);
}
go();
rep1(i,q){
if(ans[i] < 0){
ans[i] = -1;
}
cout << ans[i] << endl;
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
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... |