#include<bits/stdc++.h>
using namespace std;
const int N=100'010;
const int LG=20;
struct event {
int s, e, idx;
};
event es[N];
struct Binary_Lifting {
int up[N][LG];
void add(int node, int par) {
up[node][0]=par;
for(int i = 1;i<LG;i++) {
up[node][i]=up[up[node][i-1]][i-1];
}
}
int get(int start, int num) {
if(es[start].e>=num)return 1;
int res=1;
for(int i = LG-1;i>=0;i--) {
if(es[up[start][i]].e<num) {
start=up[start][i];
res+=(1<<i);
}
}
if(es[up[start][0]].e>=num)return res+1;
return -1;
}
};
bool operator<(const event &a, const event &b) {
return a.e>b.e;
}
int main () {
int n, q;
cin >> n >> q;
map<int,int> cc;
for(int i = 0;i<n;i++) {
cin >> es[i].e >> es[i].s;
es[i].s*=-1;
es[i].e*=-1;
cc[es[i].s]=1;
cc[es[i].e]=1;
es[i].idx=i;
}
vector<event> on[2*n], off[2*n];
{
int cnt=0;
for(auto &i:cc) {
i.second=cnt++;
}
for(int i = 0;i<n;i++) {
es[i].s=cc[es[i].s];
es[i].e=cc[es[i].e];
}
}
for(int i = 0;i<n;i++) {
on[es[i].e].push_back(es[i]);
off[es[i].s].push_back(es[i]);
}
Binary_Lifting bl;
multiset<event, less<>> ms;
for(int i = 0;i<n;i++) {
// cerr << es[i].s << " " << es[i].e << "\n";
int mx=i;
for(int j = 0;j<n;j++) {
if(i==j)continue;
if(es[i].s<=es[j].s and es[mx].e<es[j].e and es[j].s <= es[i].e) {
mx=j;
}
}
// cerr << "$" << i << " " << mx << endl;
bl.add(i,mx);
}
while(q--) {
int s, e;
cin >> s >> e;
swap(s,e);
if(es[s].s>es[e].s){
cout << -1 << "\n";
continue;
}
if(s==e) {
cout << 0 << "\n";
continue;
}
int res=bl.get(s-1,es[e-1].s);
if(res>=0)cout << res << "\n";
else cout << "impossible\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8020 KB |
Output is correct |
2 |
Execution timed out |
1575 ms |
29540 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
8020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
8020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
8020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1552 ms |
29580 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8020 KB |
Output is correct |
2 |
Execution timed out |
1575 ms |
29540 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |