#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define int ll
#define all(x) (x).begin(), (x).end()
#define md ((tl + tr) >> 1)
#define TL v + v, tl, md
#define TR v + v + 1, md + 1, tr
constexpr int maxn = 1'000'007;
constexpr ll inf = 1e18 + 7;
constexpr ll M = 1e9 + 7;
int binpow(int a, int n) {
if (n == 0)
return 1;
if (n & 1)
return a * binpow(a, n - 1) % M;
int x = binpow(a, n >> 1);
return x * x % M;
}
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const db eps = 0.00000000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
int n, a[maxn], b[maxn], q, L[maxn], R[maxn], t[4 * maxn], p[maxn];
vector<pair<int, int>> d[maxn], u[maxn];
set<int> s[maxn];
pair<int, int> ans[maxn];
void upd(int p, int x, bool tp, int v = 1, int tl = 1, int tr = 2 * n) {
if (tl == tr) {
if (tp)
s[tl].insert(x);
else
s[tl].erase(x);
if (s[tl].empty())
t[v] = inf;
else
t[v] = *s[tl].begin();
return;
}
if (p <= md)
upd(p, x, tp, TL);
else
upd(p, x, tp, TR);
t[v] = min(t[v + v], t[v + v + 1]);
}
int get(int l, int r, int v = 1, int tl = 1, int tr = 2 * n) {
if (tl >= l && tr <= r) {
return t[v];
}
if (tl > r || l > tr) {
return inf;
}
return min(get(l, r, TL), get(l, r, TR));
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
vector<int> v;
for (int i = 1; i <= n; i++) {
while (!v.empty() && b[i] > b[v.back()])
v.pop_back();
v.push_back(i);
int l = -1, r = v.size();
while (l + 1 < r) {
int mid = (l + r) / 2;
if (a[i] > b[v[mid]])
r = mid;
else
l = mid;
}
if (l == -1)
L[i] = 1;
else
L[i] = v[l] + 1;
}
v.clear();
for (int i = n; i >= 1; i--) {
while (!v.empty() && b[i] > b[v.back()])
v.pop_back();
v.push_back(i);
int l = -1, r = v.size();
while (l + 1 < r) {
int mid = (l + r) / 2;
if (a[i] > b[v[mid]])
r = mid;
else
l = mid;
}
if (l == -1)
R[i] = n;
else
R[i] = v[l] - 1;
}
for (int i = 1; i <= n; i++) {
int x = max(1ll, b[i] - 1);
u[i].emplace_back(L[i], x);
u[R[i] + 1].emplace_back(L[i], -x);
}
cin >> q;
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
d[r].emplace_back(l, i);
}
for (int i = 1; i <= 4 * 2 * n; i++)
t[i] = inf;
for (int i = 1; i <= n; i++) {
for (auto [lp, x] : u[i]) {
if (x > 0) {
upd(lp, x, 1);
} else {
upd(lp, -x, 0);
}
}
for (auto [l, j] : d[i]) {
ans[j].second = get(1, l);
}
}
v.clear();
for (int i = 1; i <= n; i++) {
while (!v.empty() && b[i] < b[v.back()])
v.pop_back();
v.push_back(i);
for (auto [lp, j] : d[i]) {
int l = -1, r = v.size();
while (l + 1 < r) {
int mid = (l + r) / 2;
if (lp > v[mid])
l = mid;
else
r = mid;
}
ans[j].first = a[v[r]];
}
}
for (int i = 1; i <= q; i++) {
// cout << ans[i].first << ' ' << ans[i].second << endl;
assert(ans[i].first >= 1 && ans[i].first <= 2 * n && ans[i].second >= 1 && ans[i].second <= 2 * n);
if (ans[i].first > ans[i].second)
cout << "Yes\n";
else
cout << "No\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--) {
solve();
// cout << '\n';
}
return 0;
}
| # | 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... |
| # | 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... |