이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#define pii pair <int, int>
using namespace std;
const int NMAX = 2e5;
const int INF = 1e9 + 10; // ?
int n, q;
int v[NMAX + 1];
pii ant[NMAX + 1];
pii quer[NMAX + 1];
vector <int> inds[NMAX + 1];
vector <pii> ev[NMAX + 1];
int qans[NMAX + 1];
namespace AINT{
struct Node{
int maxi, mini, diff;
}seg[NMAX + 1];
void propag(int node, int l, int r){
int x = seg[node].mini;
seg[node].mini = +INF;
if(l < r){
seg[2 * node].mini = min(seg[2 * node].mini, x);
seg[2 * node].diff = max(seg[2 * node].diff, seg[2 * node].maxi - seg[2 * node].mini);
seg[2 * node + 1].mini = min(seg[2 * node + 1].mini, x);
seg[2 * node + 1].diff = max(seg[2 * node + 1].diff, seg[2 * node + 1].maxi - seg[2 * node + 1].mini);
}
}
void build(int node, int l, int r){
seg[node].maxi = -INF;
seg[node].mini = +INF;
seg[node].diff = seg[node].maxi - seg[node].mini;
if(l == r){
return;
}
int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
}
void updatemx(int node, int l, int r, int pos, int val){
propag(node, l, r);
if(l == r){
seg[node].maxi = val;
seg[node].mini = +INF;
seg[node].diff = seg[node].maxi - seg[node].mini;
return;
}
int mid = (l + r) / 2;
if(pos <= mid){
updatemx(2 * node, l, mid, pos, val);
}else{
updatemx(2 * node + 1, mid + 1, r, pos, val);
}
propag(2 * node, l, mid);
propag(2 * node + 1, mid + 1, r);
seg[node].maxi = max(seg[2 * node].maxi, seg[2 * node + 1].maxi);
seg[node].diff = max(seg[2 * node].diff, seg[2 * node + 1].diff);
}
void updatemn(int node, int l, int r, int ul, int ur, int val){
propag(node, l, r);
if(ul <= l && r <= ur){
seg[node].mini = min(val, seg[node].mini);
seg[node].diff = max(seg[node].diff, seg[node].maxi - seg[node].mini);
return;
}
int mid = (l + r) / 2;
if(ul <= mid){
updatemn(2 * node, l, mid, ul, ur, val);
}
if(mid + 1 <= ur){
updatemn(2 * node + 1, mid + 1, r, ul, ur, val);
}
propag(2 * node, l, mid);
propag(2 * node + 1, mid + 1, r);
seg[node].diff = max(seg[2 * node].diff, seg[2 * node + 1].diff);
}
int query(int node, int l, int r, int ql, int qr){
propag(node, l, r);
if(ql <= l && r <= qr){
return seg[node].diff;
}
int mid = (l + r) / 2, ans = -INF;
if(ql <= mid){
ans = max(ans, query(2 * node, l, mid, ql, qr));
}
if(mid + 1 <= qr){
ans = max(ans, query(2 * node + 1, mid + 1, r, ql, qr));
}
return ans;
}
int querym(int node, int l, int r, int ql, int qr){
propag(node, l, r);
if(ql <= l && r <= qr){
return seg[node].maxi;
}
int mid = (l + r) / 2, ans = -INF;
if(ql <= mid){
ans = max(ans, querym(2 * node, l, mid, ql, qr));
}
if(mid + 1 <= qr){
ans = max(ans, querym(2 * node + 1, mid + 1, r, ql, qr));
}
return ans;
}
}
void solve(){
// clears
for(int i = 1; i <= n; i++){
ev[i].clear();
inds[i].clear();
}
for(int i = 1; i <= n; i++){
if(i + ant[i].first <= n){
ev[i + ant[i].first].push_back({i, +1});
}
if(i + ant[i].second + 1 <= n){
ev[i + ant[i].second + 1].push_back({i, -1});
}
}
AINT::build(1, 1, n);
for(int i = 1; i <= q; i++){
inds[quer[i].second].push_back(i);
}
for(int i = 1; i <= n; i++){
// process events
for(pii e : ev[i]){
if(e.second == +1){
AINT::updatemx(1, 1, n, e.first, v[e.first]);
}else{
AINT::updatemx(1, 1, n, e.first, -INF); /// ???
}
}
// update with current element
if(i - ant[i].first >= 1){
AINT::updatemn(1, 1, n, max(1, i - ant[i].second), i - ant[i].first, v[i]);
}
// answer queries
for(int ind : inds[i]){
//cout << "==== " << AINT::querym(1, 1, n, quer[ind].first, quer[ind].second) << endl;
qans[ind] = max(qans[ind], AINT::query(1, 1, n, quer[ind].first, quer[ind].second));
}
}
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> v[i];
cin >> ant[i].first >> ant[i].second;
}
cin >> q;
for(int i = 1; i <= q; i++){
cin >> quer[i].first >> quer[i].second;
qans[i] = -1;
}
solve();
// reverse
for(int i = 1; i <= n / 2; i++){
swap(v[i], v[n - i + 1]);
swap(ant[i], ant[n - i + 1]);
}
for(int i = 1; i <= q; i++){
quer[i].first = n - quer[i].first + 1;
quer[i].second = n - quer[i].second + 1;
swap(quer[i].first, quer[i].second);
}
solve();
for(int i = 1; i <= q; i++){
cout << qans[i] << '\n';
}
return 0;
}
/*
5
10 2 4
1 1 1
2 1 3
1 1 1
100 1 1
5
1 2
2 3
1 3
1 4
1 5
20
260055884 2 15
737689751 5 5
575359903 1 15
341907415 14 14
162026576 9 19
55126745 10 19
95712405 11 14
416027186 8 13
370819848 11 14
629309664 4 13
822713895 5 15
390716905 13 17
577166133 8 19
195931195 10 17
377030463 14 17
968486685 11 19
963040581 4 10
566835557 1 12
586336111 6 16
385865831 8 9
1
1 20
5 16
*/
# | 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... |