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] ;
vector< pair<int , int> >queries[MAX] ;
int ans[MAX] ;
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] ;
cin>>q ;
for(int i = 1 ; i <= q ; ++i)
{
int l , r ;
cin>>l>>r ;
queries[r].emplace_back(l , i) ;
}
set< array<int , 3> >s ;
int last = 0 ;
for(int i = 1 ; i <= n ; ++i)
{
auto it = s.lower_bound({L[i] , -1 , -1}) ;
if(it != s.begin())
--it ;
while(s.size() && 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}) ;
}
ordered_set< pair<int , int> >s2 ;
for(int i = 1 ; i <= n ; ++i)
{
s2.insert({val[i] , i}) ;
for(auto &p : queries[i])
ans[p.second] = 1 + s2.size() - s2.order_of_key({p.first , -1}) ;
}
for(int i = 1 ; i <= q ; ++i)
cout<<ans[i]<<"\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... |