이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
namespace subtask2{
struct segtree{
int k;
vector<int> v;
segtree(int n){
k = 1;
while(k < n) k*=2;
v.resize(2*k, -1);
}
void update(int in, int x){
in+=k;
v[in] = max(v[in], x);
in/=2;
while(in > 0){
v[in] = max(v[2*in], v[2*in+1]);
in/=2;
}
}
int get(int l, int r, int nd, int a, int b){
if(b < l || a > r) return -1;
if(a >= l && b <= r) return v[nd];
int c = (a+b)/2;
return max(get(l, r, 2*nd, a, c), get(l, r, 2*nd+1, c+1, b));
}
int get(int l, int r){
return get(l, r, 1, 0, k-1);
}
};
void main(int n){
vector<int> H(n), A(n), B(n);
for(int i = 0; i < n; i++){
cin >> H[i] >> A[i] >> B[i];
}
vector<vector<pair<int, int>>> ed(n);
for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++){
if(j >= i+A[i] && j <= i+B[i] && i >= j-B[j] && i <= j-A[j])
ed[j].push_back({i, abs(H[i]-H[j])});
}
segtree S(n);
int q; cin >> q;
vector<int> ans(q, -1);
vector<vector<pair<int, int>>> queries(n);
for(int i = 0; i < q; i++){
int l, r; cin >> l >> r; l--, r--;
queries[r].push_back({l, i});
}
for(int i = 0; i < n; i++){
for(auto [j, h]: ed[i]) S.update(j, h);
for(auto [l, in]: queries[i]) ans[in] = S.get(l, i);
}
for(int x: ans) cout << x << "\n";
}
};
namespace subtask3{
const int inf = 1000000005;
struct segtree{
struct Node{
int mx, mn;
Node(){
mx = -inf, mn = inf;
}
Node(int x){
mx = x, mn = x;
}
Node operator+(Node a){
Node rt;
rt.mx = max(mx, a.mx);
rt.mn = min(mn, a.mn);
return rt;
}
};
int k;
vector<Node> v;
segtree(int n){
k = 1;
while(k < n) k*=2;
v.resize(2*k, Node());
}
void update(int in, int x){
in+=k;
if(x == -1) v[in] = Node();
else v[in] = Node(x);
in/=2;
while(in > 0){
v[in] = v[2*in]+v[2*in+1];
in/=2;
}
}
Node get(int l, int r, int nd, int a, int b){
if(b < l || a > r) return Node();
if(a >= l && b <= r) return v[nd];
int c = (a+b)/2;
return get(l, r, 2*nd, a, c) + get(l, r, 2*nd+1, c+1, b);
}
Node get(int l, int r){
return get(l, r, 1, 0, k-1);
}
};
void main(int n){
vector<int> H(n), A(n), B(n);
for(int i = 0; i < n; i++){
cin >> H[i] >> A[i] >> B[i];
}
segtree S(n);
for(int i = 0; i < n; i++){
if(i-B[i] < 0 && i-A[i] >= 0) S.update(i, H[i]);
}
vector<vector<int>> st(n), ed(n);
for(int i = 0; i < n; i++){
if(i-B[i] >= 0) st[i-B[i]].push_back(i);
if(i-A[i] >= 0) ed[i-A[i]].push_back(i);
if(i+A[i] < n) st[i+A[i]].push_back(i);
if(i+B[i] < n) ed[i+B[i]].push_back(i);
}
int ans = -1;
for(int i = 0; i < n; i++){
for(int j: st[i]) S.update(j, H[j]);
segtree::Node nd;
if(i-A[i] >= 0) nd = nd+S.get(max(0, i-B[i]), i-A[i]);
if(i+A[i] < n) nd = nd+S.get(i+A[i], min(i+B[i], n-1));
if(nd.mx > -inf){
ans = max(ans, max(abs(H[i]-nd.mx), abs(H[i]-nd.mn)));
}
for(int j: ed[i]) S.update(j, -1);
}
cout << ans << "\n";
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
if(n <= 2000) subtask2::main(n);
else subtask3::main(n);
}
# | 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... |