Submission #59972349
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
int set = -1;
int act = 0;
Tag() {}
Tag(int s, int a) { set = s; act = a; }
void apply(Tag t) & {
if (t.act) {
act = t.act;
set = t.set;
} else if (t.set != -1) {
set = t.set;
}
}
};
struct Info {
int min_x, max_x, active = 0;
int y, best = -1;
Info() {}
Info(int x) {
min_x = max_x = x;
}
void apply(Tag t) & {
if (t.act) {
active = 1;
y = t.set;
best = y - min_x;
assert(min_x == max_x);
} else if (t.set != -1 && active) {
y = t.set;
best = y - max_x;
}
}
};
Info operator+(const Info &a, const Info &b) {
Info res;
if (a.active && b.active) {
res.min_x = min(a.min_x, b.min_x);
res.max_x = max(a.max_x, b.max_x);
res.best = min(a.best, b.best);
res.y = min(a.y, b.y);
res.active = 1;
return res;
} else if (a.active) {
res = a;
return a;
} else if (b.active) {
res = b;
return res;
} else {
return res;
}
}
// [l, r)
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto & x: a) cin >> x;
for (auto & x: b) cin >> x;
set<int> vals;
for (int i = 0; i < n; ++i) {
if (a[i] > b[i]) {
swap(a[i], b[i]);
}
vals.insert(a[i]);
vals.insert(b[i]);
}
map<int, int> to;
vector<Info> from;
for (auto x: vals) {
to[x] = from.size();
from.push_back(Info(x));
}
LazySegmentTree<Info, Tag> st(from);
st.rangeApply(to[a[0]], to[a[0]] + 1, Tag(a[0], 1));
st.rangeApply(to[b[0]], to[b[0]] + 1, Tag(b[0], 1));
// auto print = [&] () {
// for (int i = 0; i < from.size(); ++i) {
// cout << st.rangeQuery(i, i).best << " \n"[i + 1 == from.size()];
// }
// };
int N = from.size();
for (int i = 0; i < n; ++i) {
// int ans = 1e9;
int f = st.findLast(0, to[a[i]] + 1, [&](const Info x){
return x.active && x.y < a[i];
});
// cout << "? " << f << '\n';
if (f != -1) {
st.rangeApply(0, f + 1, Tag(a[i], 0));
// ans = min(ans, st.rangeQuery(0, f + 1).best);
}
auto info = st.rangeQuery(f + 1, to[a[i]] + 1);
// if (info.active) ans = min(ans, info.best);
if (to[a[i]] < N) {
info = st.rangeQuery(to[a[i]], N);
// cout << "? " << info.active << ' ' << info.y << '\n';
// cout << "AT " << to[a[i]] << ' ' << a[i] << '\n';
if (info.active) {
st.rangeApply(to[a[i]], to[a[i]] + 1, Tag(info.y, 1));
// cout << "D " << st.rangeQuery(to[a[i]], to[a[i]] + 1).best << '\n';
// ans = min(ans, info.best);
}
}
if (a[i] == b[i]) {
N = min(N, to[b[i]] + 1);
cout << st.rangeQuery(0, N).best << '\n';
continue;
}
int f2 = st.findLast(to[a[i]] + 1, to[b[i]] + 1, [&](const Info x){
return x.active && x.y < b[i];
});
// cout << "? " << f2 << '\n';
if (f2 != -1) {
st.rangeApply(to[a[i]] + 1, f2 + 1, Tag(b[i], 0));
}
if (to[b[i]] < N) {
info = st.rangeQuery(to[b[i]], N);
if (info.active) {
st.rangeApply(to[b[i]], to[b[i]] + 1, Tag(info.y, 1));
}
}
N = min(N, to[b[i]] + 1);
cout << st.rangeQuery(0, N).best << '\n';
}
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Many Easy Optimizations |
User |
aqxa |
Language |
C++ 20 (gcc 12.2) |
Score |
900 |
Code Size |
7834 Byte |
Status |
AC |
Exec Time |
3390 ms |
Memory |
216736 KiB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
900 / 900 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_sample_01.txt, 00_sample_02.txt |
All |
00_sample_01.txt, 00_sample_02.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt, 01_test_38.txt, 01_test_39.txt, 01_test_40.txt, 01_test_41.txt, 01_test_42.txt, 01_test_43.txt, 01_test_44.txt, 01_test_45.txt, 01_test_46.txt, 01_test_47.txt, 01_test_48.txt, 01_test_49.txt, 01_test_50.txt, 01_test_51.txt, 01_test_52.txt, 01_test_53.txt, 01_test_54.txt, 01_test_55.txt, 01_test_56.txt, 01_test_57.txt, 01_test_58.txt, 01_test_59.txt, 01_test_60.txt, 01_test_61.txt, 01_test_62.txt, 01_test_63.txt, 01_test_64.txt, 01_test_65.txt, 01_test_66.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_01.txt |
AC |
1 ms |
3460 KiB |
00_sample_02.txt |
AC |
1 ms |
3648 KiB |
01_test_01.txt |
AC |
2244 ms |
216640 KiB |
01_test_02.txt |
AC |
2332 ms |
216644 KiB |
01_test_03.txt |
AC |
2368 ms |
216520 KiB |
01_test_04.txt |
AC |
2182 ms |
216548 KiB |
01_test_05.txt |
AC |
2186 ms |
216472 KiB |
01_test_06.txt |
AC |
2328 ms |
216516 KiB |
01_test_07.txt |
AC |
2235 ms |
216544 KiB |
01_test_08.txt |
AC |
2260 ms |
216528 KiB |
01_test_09.txt |
AC |
2344 ms |
216536 KiB |
01_test_10.txt |
AC |
2306 ms |
216524 KiB |
01_test_11.txt |
AC |
2275 ms |
216544 KiB |
01_test_12.txt |
AC |
2177 ms |
216652 KiB |
01_test_13.txt |
AC |
2305 ms |
216576 KiB |
01_test_14.txt |
AC |
2203 ms |
216604 KiB |
01_test_15.txt |
AC |
2247 ms |
216544 KiB |
01_test_16.txt |
AC |
1454 ms |
170908 KiB |
01_test_17.txt |
AC |
277 ms |
47252 KiB |
01_test_18.txt |
AC |
584 ms |
84196 KiB |
01_test_19.txt |
AC |
266 ms |
44440 KiB |
01_test_20.txt |
AC |
1687 ms |
181320 KiB |
01_test_21.txt |
AC |
2322 ms |
216732 KiB |
01_test_22.txt |
AC |
2301 ms |
216612 KiB |
01_test_23.txt |
AC |
2307 ms |
216624 KiB |
01_test_24.txt |
AC |
2309 ms |
216608 KiB |
01_test_25.txt |
AC |
2313 ms |
216584 KiB |
01_test_26.txt |
AC |
2301 ms |
216636 KiB |
01_test_27.txt |
AC |
2314 ms |
216604 KiB |
01_test_28.txt |
AC |
2311 ms |
216572 KiB |
01_test_29.txt |
AC |
2308 ms |
216552 KiB |
01_test_30.txt |
AC |
2312 ms |
216544 KiB |
01_test_31.txt |
AC |
3263 ms |
216736 KiB |
01_test_32.txt |
AC |
3213 ms |
216548 KiB |
01_test_33.txt |
AC |
3286 ms |
216612 KiB |
01_test_34.txt |
AC |
3390 ms |
216548 KiB |
01_test_35.txt |
AC |
3269 ms |
216508 KiB |
01_test_36.txt |
AC |
3262 ms |
216336 KiB |
01_test_37.txt |
AC |
3255 ms |
216012 KiB |
01_test_38.txt |
AC |
3241 ms |
216636 KiB |
01_test_39.txt |
AC |
3382 ms |
216416 KiB |
01_test_40.txt |
AC |
3267 ms |
216512 KiB |
01_test_41.txt |
AC |
2030 ms |
216384 KiB |
01_test_42.txt |
AC |
2023 ms |
216544 KiB |
01_test_43.txt |
AC |
2038 ms |
215872 KiB |
01_test_44.txt |
AC |
2032 ms |
216128 KiB |
01_test_45.txt |
AC |
2033 ms |
216616 KiB |
01_test_46.txt |
AC |
2030 ms |
216468 KiB |
01_test_47.txt |
AC |
2044 ms |
216508 KiB |
01_test_48.txt |
AC |
2043 ms |
215968 KiB |
01_test_49.txt |
AC |
2043 ms |
216144 KiB |
01_test_50.txt |
AC |
2027 ms |
216484 KiB |
01_test_51.txt |
AC |
2319 ms |
216352 KiB |
01_test_52.txt |
AC |
2384 ms |
216500 KiB |
01_test_53.txt |
AC |
2314 ms |
215964 KiB |
01_test_54.txt |
AC |
2379 ms |
216564 KiB |
01_test_55.txt |
AC |
2314 ms |
216004 KiB |
01_test_56.txt |
AC |
2385 ms |
215936 KiB |
01_test_57.txt |
AC |
2310 ms |
216612 KiB |
01_test_58.txt |
AC |
2377 ms |
215952 KiB |
01_test_59.txt |
AC |
2309 ms |
216444 KiB |
01_test_60.txt |
AC |
2386 ms |
216236 KiB |
01_test_61.txt |
AC |
2 ms |
3460 KiB |
01_test_62.txt |
AC |
1 ms |
3520 KiB |
01_test_63.txt |
AC |
1 ms |
3564 KiB |
01_test_64.txt |
AC |
57 ms |
6952 KiB |
01_test_65.txt |
AC |
80 ms |
6984 KiB |
01_test_66.txt |
AC |
78 ms |
6968 KiB |