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 ;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
const int MAX = 2e5 + 10 ;
int L[MAX] , R[MAX] ;
int n , q ;
int val[MAX] , to[MAX][20] ;
void preprocess()
{
set< array<int , 3> >s ;
int last = 0 ;
to[n+1][0] = n+1 ;
for(int i = 1 ; i <= n ; ++i)
{
to[i][0] = n+1 ;
auto it = s.lower_bound({L[i] , -1 , -1}) ;
if(it != s.begin())
--it ;
while(it != s.end())
{
array<int , 3>a = *it ;
if(a[0] > R[i])
break ;
if(a[1] >= L[i])
val[i] = max(val[i] , a[2]) ;
++it ;
}
for(int j = last+1 ; j <= val[i] ; ++j)
s.erase({L[j] , R[j] , j}) ;
last = max(last , val[i]) ;
s.insert({L[i] , R[i] , i}) ;
to[val[i]][0] = i ;
}
for(int i = n-1 ; i >= 1 ; --i)
to[i][0] = min(to[i][0] , to[i+1][0]) ;
for(int j = 1 ; j < 20 ; ++j)
{
for(int i = 1 ; i <= n+1 ; ++i)
to[i][j] = to[to[i][j-1]][j-1] ;
}
}
int solve(int l , int r)
{
int ans = 1 ;
for(int i = 19 ; i >= 0 ; --i)
{
if(to[l][i] <= r)
ans += (1 << i) , l = to[l][i] ;
}
return ans ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n ; ++i)
cin>>L[i]>>R[i] ;
preprocess() ;
cin>>q ;
for(int i = 1 ; i <= q ; ++i)
{
int l , r ;
cin>>l>>r ;
cout<<solve(l , r)<<"\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... |