This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, mod = 1e9 + 7, inf = 1e9 + 5;
int tree[N * 8], lazy[N * 8];
int n;
int cor = 0;
void prob(int l, int r, int node)
{
tree[node] = min(tree[node], lazy[node]);
if (l != r)
lazy[node * 2] = min(lazy[node * 2], lazy[node]), lazy[node * 2 + 1] = min(lazy[node * 2 + 1], lazy[node]);
}
void update(int lq, int rq, int val, int l = 1, int r = cor, int node = 1)
{
prob(l, r, node);
if (lq <= l && r <= rq)
{
lazy[node] = min(val, lazy[node]);
prob(l, r, node);
return;
}
if (r < lq || l > rq)
return;
int mid = (l + r) / 2;
update(lq, rq, val, l, mid, node * 2), update(lq, rq, val, mid + 1, r, node * 2 + 1);
tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}
int query(int lq, int rq, int l = 1, int r = cor, int node = 1)
{
prob(l, r, node);
if (lq <= l && r <= rq)
return tree[node];
if (r < lq || l > rq)
return inf;
int mid = (l + r) / 2;
return min(query(lq, rq, l, mid, node * 2), query(lq, rq, mid + 1, r, node * 2 + 1));
}
map<int, int> mp;
int main()
{
ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
memset(lazy, '?', sizeof lazy);
memset(tree, '?', sizeof tree);
cin >> n;
vector<pair<int, int>> arr(n + 1);
for (int i = 1; i <= n; i++)
cin >> arr[i].first >> arr[i].second, mp[arr[i].first] = 1, mp[arr[i].second] = 1;
for (auto &v : mp)
v.second = ++cor;
for (int i = 1; i <= n; i++)
arr[i].first = mp[arr[i].first], arr[i].second = mp[arr[i].second];
vector<vector<int>> par(n + 2, vector<int>(22, n + 1));
for (int i = n; i >= 1; i--)
{
par[i][0] = par[i + 1][0];
par[i][0] = min(query(arr[i].first, arr[i].second), par[i][0]);
update(arr[i].first, arr[i].second, i);
}
for (int j = 1; j <= 21; j++)
for (int i = 1; i <= n; i++)
par[i][j] = par[par[i][j - 1]][j - 1];
int q; // code pls unhate me
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r; // zengy
int ans = 1;
for (int i = 21; i >= 0; i--)
if (par[l][i] <= r)
l = par[l][i], ans += (1 << i);
// aaa the num of teleports is 2^i? YES
cout << ans << "\n";
}
}
/*
3
1
3
*/
# | 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... |