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>
#define ll long long
#define pb push_back
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) (int)x.size()
#define fi first
#define sd second
#define all(x) x.begin(), x.end()
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = (int)2e5 + 5;
int n, l[N], r[N], q, nxt[N], t[8 * N], mod[8 * N], up[N][22];
vector<int> vec;
void push(int v)
{
mod[v + v] = min(mod[v + v], mod[v]);
mod[v + v + 1] = min(mod[v + v + 1], mod[v]);
t[v + v] = min(t[v + v], mod[v]);
t[v + v + 1] = min(t[v + v + 1], mod[v]);
}
void build(int v, int tl, int tr)
{
t[v] = n + 1;
mod[v] = n + 1;
if (tl == tr)
return;
int m = (tl + tr) >> 1;
build(v + v, tl, m);
build(v + v + 1, m + 1, tr);
}
int get(int v, int tl, int tr, int l, int r)
{
if (tl > tr || tl > r || tr < l) return n + 1;
if (tl >= l && tr <= r)
return t[v];
push(v);
int m = (tl + tr) >> 1;
return min(get(v + v, tl, m, l, r), get(v + v + 1, m + 1, tr, l, r));
}
void upd(int v, int tl, int tr, int l, int r, int val)
{
if (tl > tr || tl > r || tr < l) return;
if (tl >= l && tr <= r)
{
t[v] = val;
mod[v] = val;
return;
}
int m = (tl + tr) >> 1;
push(v);
upd(v + v, tl, m, l, r, val);
upd(v + v + 1, m + 1, tr, l, r, val);
t[v] = min(t[v + v], t[v + v + 1]);
}
int get_ans(int a, int b)
{
int ans = 0;
for(int i = 19; i >= 0; i--)
{
if (up[a][i] <= b)
a = up[a][i], ans += (1 << i);
}
return ans + 1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
vec.pb(l[i]);
vec.pb(r[i]);
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
for(int i = 1; i <= n; i++)
{
l[i] = lower_bound(all(vec), l[i]) - vec.begin(); l[i]++;
r[i] = lower_bound(all(vec), r[i]) - vec.begin(); r[i]++;
}
build(1, 1, 2 * n);
for(int i = n; i >= 1; i--)
{
int val = get(1, 1, 2 * n, l[i], r[i]);
nxt[i] = val;
upd(1, 1, 2 * n, l[i], r[i], i);
}
int mn = n + 1;
for(int i = n; i >= 1; i--)
{
mn = min(mn, nxt[i]);
nxt[i] = mn;
up[i][0] = nxt[i];
}
for(int j = 0; j <= 19; j++)
up[n + 1][j] = n + 1;
for(int j = 1; j <= 19; j++)
{
for(int i = 1; i <= n; i++)
up[i][j] = up[up[i][j - 1]][j - 1];
}
cin >> q;
while(q--)
{
int le, ri;
cin >> le >> ri;
cout << get_ans(le, ri) << '\n';
}
}
# | 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... |