제출 #996308

#제출 시각아이디문제언어결과실행 시간메모리
996308MilosMilutinovicGift Exchange (JOI24_ho_t4)C++14
100 / 100
2008 ms72052 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> b(n); for (int i = 0; i < n; i++) { cin >> b[i]; } vector<int> prv(n); { vector<int> mx(16 * n, -1); vector<int> tag(16 * n, -1); auto Push = [&](int x) { mx[x * 2] = max(mx[x * 2], tag[x]); mx[x * 2 + 1] = max(mx[x * 2 + 1], tag[x]); tag[x * 2] = max(tag[x * 2], tag[x]); tag[x * 2 + 1] = max(tag[x * 2 + 1], tag[x]); tag[x] = -1; }; function<void(int, int, int, int, int, int)> Modify = [&](int x, int l, int r, int ll, int rr, int v) { if (ll <= l && r <= rr) { mx[x] = max(mx[x], v); tag[x] = max(tag[x], v); Push(x); return; } Push(x); int mid = (l + r) >> 1; if (rr <= mid) { Modify(x * 2, l, mid, ll, rr, v); } else if (ll > mid) { Modify(x * 2 + 1, mid + 1, r, ll, rr, v); } else { Modify(x * 2, l, mid, ll, rr, v); Modify(x * 2 + 1, mid + 1, r, ll, rr, v); } mx[x] = max(mx[x * 2], mx[x * 2 + 1]); }; function<int(int, int, int, int, int)> Query = [&](int x, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { return mx[x]; } Push(x); int mid = (l + r) >> 1; int ret = 0; if (rr <= mid) { ret = Query(x * 2, l, mid, ll, rr); } else if (ll > mid) { ret = Query(x * 2 + 1, mid + 1, r, ll, rr); } else { ret = max(Query(x * 2, l, mid, ll, rr), Query(x * 2 + 1, mid + 1, r, ll, rr)); } mx[x] = max(mx[x * 2], mx[x * 2 + 1]); return ret; }; for (int i = 0; i < n; i++) { prv[i] = Query(1, 1, 2 * n, b[i], a[i]); Modify(1, 1, 2 * n, b[i], a[i], i); } } vector<int> nxt(n); { vector<int> mn(16 * n, n); vector<int> tag(16 * n, n); auto Push = [&](int x) { mn[x * 2] = min(mn[x * 2], tag[x]); mn[x * 2 + 1] = min(mn[x * 2 + 1], tag[x]); tag[x * 2] = min(tag[x * 2], tag[x]); tag[x * 2 + 1] = min(tag[x * 2 + 1], tag[x]); tag[x] = n; }; function<void(int, int, int, int, int, int)> Modify = [&](int x, int l, int r, int ll, int rr, int v) { if (ll <= l && r <= rr) { mn[x] = min(mn[x], v); tag[x] = min(tag[x], v); Push(x); return; } Push(x); int mid = (l + r) >> 1; if (rr <= mid) { Modify(x * 2, l, mid, ll, rr, v); } else if (ll > mid) { Modify(x * 2 + 1, mid + 1, r, ll, rr, v); } else { Modify(x * 2, l, mid, ll, rr, v); Modify(x * 2 + 1, mid + 1, r, ll, rr, v); } mn[x] = min(mn[x * 2], mn[x * 2 + 1]); }; function<int(int, int, int, int, int)> Query = [&](int x, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { return mn[x]; } Push(x); int mid = (l + r) >> 1; int ret = 0; if (rr <= mid) { ret = Query(x * 2, l, mid, ll, rr); } else if (ll > mid) { ret = Query(x * 2 + 1, mid + 1, r, ll, rr); } else { ret = min(Query(x * 2, l, mid, ll, rr), Query(x * 2 + 1, mid + 1, r, ll, rr)); } mn[x] = min(mn[x * 2], mn[x * 2 + 1]); return ret; }; for (int i = n - 1; i >= 0; i--) { nxt[i] = Query(1, 1, 2 * n, b[i], a[i]); Modify(1, 1, 2 * n, b[i], a[i], i); } } vector<vector<int>> ev(n + 1); for (int i = 0; i < n; i++) { ev[i].push_back(i); ev[nxt[i]].push_back(~i); } int q; cin >> q; vector<int> l(q), r(q); for (int i = 0; i < q; i++) { cin >> l[i] >> r[i]; --l[i]; --r[i]; } vector<vector<int>> qs(n); for (int i = 0; i < q; i++) { qs[r[i]].push_back(i); } vector<int> mn(8 * n, n); function<void(int, int, int, int, int)> Modify = [&](int x, int l, int r, int i, int v) { if (l == r) { mn[x] = v; return; } int mid = (l + r) >> 1; if (i <= mid) { Modify(x * 2, l, mid, i, v); } else { Modify(x * 2 + 1, mid + 1, r, i, v); } mn[x] = min(mn[x * 2], mn[x * 2 + 1]); }; function<int(int, int, int, int, int)> Query = [&](int x, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { return mn[x]; } int mid = (l + r) >> 1; if (rr <= mid) { return Query(x * 2, l, mid, ll, rr); } else if (ll > mid) { return Query(x * 2 + 1, mid + 1, r, ll, rr); } else { return min(Query(x * 2, l, mid, ll, rr), Query(x * 2 + 1, mid + 1, r, ll, rr)); } }; vector<bool> res(q); for (int i = 0; i < n; i++) { for (int j : ev[i]) { if (j >= 0) { Modify(1, 0, n - 1, j, prv[j]); } else { Modify(1, 0, n - 1, ~j, n); } } for (int j : qs[i]) { res[j] = (l[j] <= Query(1, 0, n - 1, l[j], r[j])); } } for (int i = 0; i < q; i++) { cout << (res[i] ? "Yes" : "No") << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...