This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif
const int INF = 1e9 + 7;
struct SGT {
int n; vector<int> min_height, tag, max_dif;
SGT(int _n): n(_n), min_height(n * 4 + 4, INF),
tag(n * 4 + 4, -INF), max_dif(n * 4 + 4, -1) {}
void upd(int i, int v) {
tag[i] = max(tag[i], v);
max_dif[i] = max(max_dif[i], tag[i] - min_height[i]);
}
void push(int i) {
if (tag[i] == -INF) return;
upd(i * 2, tag[i]);
upd(i * 2 + 1, tag[i]);
tag[i] = -INF;
}
void pull(int i) {
min_height[i] = min(min_height[i * 2], min_height[i * 2 + 1]);
max_dif[i] = max(max_dif[i * 2], max_dif[i * 2 + 1]);
}
void set_height(int i, int l, int r, int p, int v) {
if (l == r) {
min_height[i] = v;
tag[i] = -INF;
//max_dif[i] = max(max_dif[i], tag[i] - min_height[i]);
return;
}
push(i);
int m = (l + r) / 2;
if (p <= m) set_height(i * 2, l, m, p, v);
else set_height(i * 2 + 1, m + 1, r, p, v);
pull(i);
return;
}
void chmax(int i, int l, int r, int ql, int qr, int v) {
if (ql <= l and r <= qr) {
upd(i, v);
return;
}
push(i);
int m = (l + r) / 2;
if (ql <= m) chmax(i * 2, l, m, ql, qr, v);
if (m < qr) chmax(i * 2 + 1, m + 1, r, ql, qr, v);
pull(i);
return;
}
int query(int i, int l, int r, int ql, int qr) {
if (ql <= l and r <= qr)
return max_dif[i];
push(i);
int m = (l + r) / 2, ans = -1;
if (ql <= m) ans = max(ans, query(i * 2, l, m, ql, qr));
if (m < qr) ans = max(ans, query(i * 2 + 1, m + 1, r, ql, qr));
return ans;
}
void set_height(int p, int v) { set_height(1, 1, n, p, v); }
void chmax(int ql, int qr, int v) { chmax(1, 1, n, ql, qr, v); }
int query(int ql, int qr) { return query(1, 1, n, ql, qr); }
void de(int i, int l, int r) {
if (l == r) {
debug(i, l, r, min_height[i], tag[i], max_dif[i]);
return;
}
push(i);
int m = (l + r) / 2;
de(i * 2, l, m);
de(i * 2 + 1, m + 1, r);
}
void de() {
cerr << "--------------------" << endl;
de(1, 1, n);
cerr << "--------------------" << endl;
}
};
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N; cin >> N;
vector<int> H(N + 1), A(N + 1), B(N + 1);
for (int i = 1; i <= N; ++i)
cin >> H[i] >> A[i] >> B[i];
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j)
if (H[i] - H[j] == 806460109)
debug(i, j, H[i], H[j]);
int Q; cin >> Q;
vector<int> L(Q + 1), R(Q + 1);
for (int i = 1; i <= Q; ++i)
cin >> L[i] >> R[i];
vector<int> ans(Q + 1, -1);
auto solve = [&]() -> void {
vector<vector<int>> in(N + 1);
vector<vector<int>> ou(N + 2);
for (int i = 1; i <= N; ++i) {
int lb = min(N + 1, i + A[i]);
int rb = min(N, i + B[i]);
if (lb < N + 1) {
in[lb].pb(i);
ou[rb + 1].pb(i);
}
}
debug(H);
debug(A);
debug(B);
debug(in);
debug(ou);
vector<vector<pii>> qs(N + 1);
for (int i = 1; i <= Q; ++i)
qs[R[i]].pb(L[i], i);
SGT sgt(N);
for (int i = 1; i <= N; ++i) {
for (int &j: in[i])
sgt.set_height(j, H[j]);
for (int &j: ou[i])
sgt.set_height(j, INF);
int lb = max(1, i - B[i]);
int rb = max(0, i - A[i]);
debug(i, lb, rb, H[i]);
if (rb > 0)
sgt.chmax(lb, rb, H[i]);
for (auto &[ql, id]: qs[i])
ans[id] = max(ans[id], sgt.query(ql, i));
//sgt.de();
}
debug(ans);
};
solve();
reverse(begin(H) + 1, end(H));
reverse(begin(A) + 1, end(A));
reverse(begin(B) + 1, end(B));
for (int i = 1; i <= Q; ++i) {
L[i] = N - L[i] + 1;
R[i] = N - R[i] + 1;
swap(L[i], R[i]);
}
solve();
for (int i = 1; i <= Q; ++i)
cout << ans[i] << '\n';
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... |