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 <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 2000000 + 10;
const int INF = 1e9;
int n, q;
struct SegmentTree
{
struct Node
{
int height;
int answer;
int lazyMAX;
int lazyMIN;
int minActiveHeight;
int maxActiveHeight;
Node()
{
lazyMAX = -INF;
lazyMIN = INF;
minActiveHeight = INF;
maxActiveHeight = -INF;
answer = -1;
height = 0;
}
friend Node operator + (const Node &left, const Node &right)
{
Node res;
res.answer = std::max(left.answer, right.answer);
res.minActiveHeight = std::min(left.minActiveHeight, right.minActiveHeight);
res.maxActiveHeight = std::max(left.maxActiveHeight, right.maxActiveHeight);
return res;
}
};
Node tree[4*MAXN];
void push(int node, int l, int r)
{
if (tree[node].lazyMAX == -INF && tree[node].lazyMIN == INF)
{
return;
}
tree[node].answer = std::max(tree[node].answer, tree[node].lazyMAX - tree[node].minActiveHeight);
tree[node].answer = std::max(tree[node].answer, tree[node].maxActiveHeight - tree[node].lazyMIN);
if (l < r)
{
tree[2*node].lazyMAX = std::max(tree[2*node].lazyMAX, tree[node].lazyMAX);
tree[2*node].lazyMIN = std::min(tree[2*node].lazyMIN, tree[node].lazyMIN);
tree[2*node + 1].lazyMAX = std::max(tree[2*node + 1].lazyMAX, tree[node].lazyMAX);
tree[2*node + 1].lazyMIN = std::min(tree[2*node + 1].lazyMIN, tree[node].lazyMIN);
}
tree[node].lazyMAX = -INF;
tree[node].lazyMIN = INF;
}
void updateHeight(int l, int r, int node, int queryPos, int queryVal)
{
push(node, l, r);
if (queryPos < l || r < queryPos)
{
return;
}
if (l == r)
{
tree[node].height = queryVal;
tree[node].minActiveHeight = (queryVal == 0 ? INF : queryVal);
tree[node].maxActiveHeight = (queryVal == 0 ? -INF : queryVal);
return;
}
int mid = (l + r) / 2;
updateHeight(l, mid, 2*node, queryPos, queryVal);
updateHeight(mid + 1, r, 2*node + 1, queryPos, queryVal);
tree[node] = tree[2*node] + tree[2*node + 1];
}
void updateRange(int l, int r, int node, int queryL, int queryR, int queryVal)
{
push(node, l, r);
if (queryR < l || r < queryL)
{
return;
}
if (queryL <= l && r <= queryR)
{
tree[node].lazyMAX = queryVal;
tree[node].lazyMIN = queryVal;
push(node, l, r);
return;
}
int mid = (l + r) / 2;
updateRange(l, mid, 2*node, queryL, queryR, queryVal);
updateRange(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
tree[node] = tree[2*node] + tree[2*node + 1];
}
int query(int l, int r, int node, int queryL, int queryR)
{
push(node, l, r);
if (queryL <= l && r <= queryR)
{
return tree[node].answer;
}
int res = -1;
int mid = (l + r) / 2;
if (queryL <= mid) res = std::max(res, query(l, mid, 2*node, queryL, queryR));
if (mid + 1 <= queryR) res = std::max(res, query(mid + 1, r, 2*node + 1, queryL, queryR));
return res;
}
void updateHeight(int pos, int val)
{
updateHeight(1, n, 1, pos, val);
}
void updateRange(int l, int r, int val)
{
updateRange(1, n, 1, l, r, val);
}
int query(int l, int r)
{
return query(1, n, 1, l, r);
}
};
int a[MAXN];
int b[MAXN];
int h[MAXN];
int answer[MAXN];
std::vector <int> add[MAXN];
std::vector <int> rem[MAXN];
std::vector <std::pair <int,int>> queries[MAXN];
SegmentTree tree;
void solve()
{
for (int i = 1 ; i <= n ; ++i)
{
add[a[i] + i].push_back(i);
rem[b[i] + i + 1].push_back(i);
}
for (int i = 1 ; i <= n ; ++i)
{
for (const int &idx : add[i])
{
tree.updateHeight(idx, h[idx]);
}
for (const int &idx : rem[i])
{
tree.updateHeight(idx, 0);
}
if (i - a[i] > 0)
{
tree.updateRange(std::max(1, i - b[i]), std::max(1, i - a[i]), h[i]);
}
for (const auto &[l, idx] : queries[i])
{
answer[idx] = tree.query(l, i);
}
}
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> h[i] >> a[i] >> b[i];
}
std::cin >> q;
for (int i = 1 ; i <= q ; ++i)
{
int l, r;
std::cin >> l >> r;
queries[r].push_back({l, i});
}
}
void print()
{
for (int i = 1 ; i <= q ; ++i)
{
std::cout << answer[i] << '\n';
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
print();
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... |