#include <bits/stdc++.h>
using namespace std ;
const int inf = 1e9 ;
const int MAX = 2e5 + 10 ;
int L[MAX] , R[MAX] ;
int n , q ;
void compress()
{
vector<int>v ;
for(int i = 1 ; i <= n ; ++i)
v.push_back(L[i]) , v.push_back(R[i]) ;
sort(v.begin() , v.end()) ;
v.erase(unique(v.begin() , v.end()) , v.end()) ;
for(int i = 1 ; i <= n ; ++i)
{
L[i] = lower_bound(v.begin() , v.end() , L[i]) - v.begin() ;
R[i] = lower_bound(v.begin() , v.end() , R[i]) - v.begin() ;
L[i]++ , R[i]++ ;
}
}
int dp[MAX] , ans[MAX] ;
vector< array<int , 3> >v ;
vector< pair<int , int> >queries[MAX] ;
bool cmp(array<int , 3>&a , array<int , 3>&b)
{
if(a[1] != b[1])
return (a[1] < b[1]) ;
else
return (a[0] < b[0]) ;
}
void solve(int st)
{
if(!queries[st].size())
return ;
for(int i = 1 ; i <= n ; ++i)
dp[i] = inf ;
dp[st] = 0 ;
stack<int>s ;
s.push(st) ;
for(auto &a : v)
{
int l = a[0] , r = a[1] , idx = a[2] ;
if(idx == st || R[s.top()] > r)
continue ;
int last = -1 ;
while(s.size() && R[s.top()] >= l)
{
last = s.top() ;
s.pop() ;
}
if(last == -1)
continue ;
s.push(last) , s.push(idx) ;
dp[idx] = dp[last] + 1 ;
}
for(auto &p : queries[st])
ans[p.second] = dp[p.first] ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>q ;
for(int i = 1 ; i <= n ; ++i)
cin>>L[i]>>R[i] ;
for(int i = 1 ; i <= q ; ++i)
{
int x , y ;
cin>>x>>y ;
queries[x].emplace_back(y , i) ;
}
compress() ;
for(int i = 1 ; i <= n ; ++i)
v.push_back({L[i] , R[i] , i}) ;
sort(v.begin() , v.end() , cmp) ;
for(int i = 1 ; i <= n ; ++i)
solve(i) ;
for(int i = 1 ; i <= q ; ++i)
{
if(ans[i] != inf)
cout<<ans[i]<<"\n" ;
else
cout<<"impossible\n" ;
}
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
94 ms |
10628 KB |
Output is correct |
3 |
Correct |
686 ms |
11016 KB |
Output is correct |
4 |
Execution timed out |
1581 ms |
10788 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4924 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5080 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4924 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5080 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
4 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
51 ms |
7128 KB |
Output is correct |
20 |
Correct |
49 ms |
7192 KB |
Output is correct |
21 |
Correct |
197 ms |
7836 KB |
Output is correct |
22 |
Correct |
201 ms |
7884 KB |
Output is correct |
23 |
Correct |
244 ms |
8736 KB |
Output is correct |
24 |
Correct |
177 ms |
8652 KB |
Output is correct |
25 |
Correct |
53 ms |
7944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4924 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5080 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5012 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5044 KB |
Output is correct |
17 |
Correct |
4 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
83 ms |
8888 KB |
Output is correct |
20 |
Correct |
146 ms |
8812 KB |
Output is correct |
21 |
Correct |
155 ms |
8928 KB |
Output is correct |
22 |
Correct |
115 ms |
8908 KB |
Output is correct |
23 |
Correct |
144 ms |
10672 KB |
Output is correct |
24 |
Correct |
97 ms |
10796 KB |
Output is correct |
25 |
Correct |
31 ms |
10152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
10676 KB |
Output is correct |
2 |
Correct |
659 ms |
10932 KB |
Output is correct |
3 |
Execution timed out |
1581 ms |
10828 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
94 ms |
10628 KB |
Output is correct |
3 |
Correct |
686 ms |
11016 KB |
Output is correct |
4 |
Execution timed out |
1581 ms |
10788 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |