#include<bits/stdc++.h>
using namespace std;
#define ll long long
int N, Q;
vector<ll> S, E;
vector<array<ll, 3>> ranges;
vector<ll> M;
void build(int p, int L, int R){
if(L == R){
M[p] = ranges[L][0];
return;
}
int mid = (L+R)/2;
build(p*2, L, mid);
build(p*2+1, mid+1, R);
M[p] = min(M[p*2], M[p*2+1]);
}
int findLeftMostGood(int p, int L, int R, int l, int v){
if(R < l) return -1;
if(L >= l) {
if(M[p] > v) return -1;
int low = L;
int high = R;
int pp = p;
while(low < high){
int mid = low + (high-low)/2;
if(M[pp*2] <= v){
high = mid;
pp*=2;
}else{
low = mid+1;
pp=pp*2+1;
}
}
return low;
}
int mid = (L+R)/2;
int ret1 = findLeftMostGood(p*2, L, mid, l, v);
if(ret1 != -1) return ret1;
return findLeftMostGood(p*2+1, mid+1, R, l, v);
}
int findTheFirstSmallerThan(int v){
int low = 0;
int high = N-1;
int idx = -1;
while(low < high){
int mid = low + (high-low)/2;
if(ranges[mid][1] > v){
low = mid+1;
}else{
idx = mid;
high = mid;
}
}
return idx;
}
void solve(){
cin>>N>>Q;
S.assign(N, 0);
E.assign(N, 0);
M.assign(4*N+1, 0);
ranges.assign(N, array<ll, 3>());
// graph.assign(N, vector<int>());
for(int i = 0; i < N; i++){
int s, e;
cin>>s>>e;
S[i] = s;
E[i] = e;
ranges[i] = {s, e, i};
}
map<int, int> mp;
sort(ranges.begin(), ranges.end(), [&](array<ll, 3> a, array<ll, 3> b){
return a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]);
});
bool subtask2 = N<=1000&&Q<=100;
if(!subtask2){
for(int i = 0; i < N; i++) {
mp[ranges[i][2]] = i;
}
build(1, 0, N-1);
for(int q = 0; q < Q; q++){
int sq, eq;
cin>>sq>>eq;
sq--;
eq--;
queue<int> que;
que.push(sq);
vector<ll> dist(N, LLONG_MAX);
dist[sq] = 0;
while(!que.empty()){
int curr = que.front();
que.pop();
int node = (dist[eq] == LLONG_MAX && S[eq] <= E[curr] && E[eq] >= E[curr]) ? eq : -1;
if(node == -1){
int idx = findLeftMostGood(1, 0, N-1, mp[eq], E[curr]);
if(idx != -1 && ranges[idx][0] <= E[curr] && ranges[idx][1] >= E[curr] && ranges[idx][1] <= E[eq])
node = ranges[idx][2];
}
if(node != -1 && node != curr){
dist[node] = dist[curr] +1;
que.push(node);
}
}
if(dist[eq] == LLONG_MAX)
cout<<"impossible"<<endl;
else cout<<dist[eq]<<endl;
}
return;
}
bool subtask3 = N <= 5000;
vector<vector<int>> P(N, vector<int>(N, INT_MAX));
vector<set<int>> S(N, set<int>());
sort(ranges.begin(), ranges.end(), [&](array<ll, 3> ar1, array<ll, 3> ar2){
return ar1[1] < ar2[1] || (ar1[1] == ar2[1] && ar1[0] < ar2[0]);
});
for(int i = 0; i < N; i++){
P[i][i] = 0;
vector<int> s;
for(int j = i-1; j >= 0; j--){
if(ranges[j][1] >= ranges[i][0]){
P[j][i] = 1;
s.push_back(j);
continue;
}
for(int v : s){
if(P[j][v] == INT_MAX) continue;
P[j][i] = min(P[j][i], P[j][v]+1);
}
// controlliamo
}
}
for(int q = 0; q < Q; q++){
int sq, eq;
cin>>sq>>eq;
sq--;
eq--;
if(P[sq][eq] == INT_MAX) cout<<"impossible"<<endl;
else cout<<P[sq][eq]<<endl;
}
}
int main(){
solve();
return 0;
}
Compilation message
events.cpp: In function 'void solve()':
events.cpp:159:7: warning: unused variable 'subtask3' [-Wunused-variable]
159 | bool subtask3 = N <= 5000;
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
14880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
9 ms |
4384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
9 ms |
4384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
9 ms |
4384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1572 ms |
14856 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
14880 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |