이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
int a[N], b[N], h[N];
int ans[N];
const int INF = (int)1e9 + 7;
struct segment_tree {
struct node {
int res;
int lzh;
int mxh;
node () {
res = -1;
mxh = 0;
lzh = INF; // min truyen vao
};
};
int n;
vector<node> t;
segment_tree () {};
segment_tree (int _n) {
n = _n;
t.resize(4 * n + 7);
};
void push(int id) {
int &h = t[id].lzh;
node &a = t[id << 1], &b = t[id << 1 | 1];
if (a.mxh != 0) mini(a.lzh, h);
if (b.mxh != 0) mini(b.lzh, h);
maxi(a.res, a.mxh - a.lzh);
maxi(b.res, b.mxh - b.lzh);
h = INF;
}
void reff(int id) {
t[id].res = max(t[id << 1].res, t[id << 1 | 1].res);
t[id].mxh = max(t[id << 1].mxh, t[id << 1 | 1].mxh);
}
void upd(int id, int l, int r, int p, int vx) {
if (l == r) {
if (vx == 1) {
t[id].mxh = h[p];
} else {
t[id].mxh = 0;
}
return ;
}
if (t[id].lzh != INF) {
push(id);
}
int m = (l + r) >> 1;
if (p <= m)
upd(id << 1, l, m, p, vx);
else
upd(id << 1 | 1, m + 1, r, p, vx);
reff(id);
}
void upd(int p, int vx) {
upd(1, 1, n, p, vx);
}
void update(int id, int l, int r, int u, int v, int val) {
if (l > v or r < u) return ;
if (l >= u and r <= v) {
maxi(t[id].res, t[id].mxh - val);
if (t[id].mxh > 0)
mini(t[id].lzh, val);
return ;
}
if (t[id].lzh != INF) {
push(id);
}
int m = (l + r) >> 1;
update(id << 1, l, m, u, v, val);
update(id << 1 | 1, m + 1, r, u, v, val);
reff(id);
}
void update(int l, int r, int val) {
if (r <= 0) return ;
l = max(l, 1);
update(1, 1, n, l, r, val);
}
int get(int id, int l, int r, int u, int v) {
if (l > v or r < u) return -1;
if (l >= u and r <= v) return t[id].res;
if (t[id].lzh != INF) push(id);
int m = (l + r) >> 1;
return max(get(id << 1, l, m, u, v),
get(id << 1 | 1, m + 1, r, u, v));
}
int get(int l, int r) {
return get(1, 1, n, l, r);
}
};
int l[N], r[N];
vector<int> ins[N], del[N], qr[N];
void main_code() {
int n; cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> h[i] >> a[i] >> b[i];
}
int q; cin >> q;
for(int i = 1; i <= q; ++i) {
cin >> l[i] >> r[i];
ans[i] = -1;
}
for(int it = 0; it < 2; ++it) {
reverse(h + 1, h + n + 1);
reverse(a + 1, a + n + 1);
reverse(b + 1, b + n + 1);
for(int i = 1; i <= q; ++i) {
l[i] = n - l[i] + 1;
r[i] = n - r[i] + 1;
swap(l[i], r[i]);
}
for(int i = 1; i <= n; ++i) {
ins[i].clear(), del[i].clear(), qr[i].clear();
}
for(int i = 1; i <= n; ++i) {
ins[min(n + 1, i + a[i])].push_back(i);
del[min(n + 1, i + b[i])].push_back(i);
}
for(int i = 1; i <= q; ++i) {
qr[r[i]].push_back(i);
}
segment_tree st(n);
for(int i = 1; i <= n; ++i) {
for(int j : ins[i]) {
st.upd(j, 1);
}
st.update(i - b[i], i - a[i], h[i]);
for(int j : qr[i]) {
maxi(ans[j], st.get(l[j], i));
}
for(int j : del[i]) {
st.upd(j, -1);
}
}
}
for(int i = 1; i <= q; ++i) {
cout << ans[i] << nl;
}
}
/* Let the river flows naturally */
컴파일 시 표준 에러 (stderr) 메시지
antennas.cpp: In function 'int main()':
antennas.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |