#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2")
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#include <unordered_map>
using namespace std;
typedef int ll;
struct one{
ll first, second, ind;
};
bool cmp(one &p1, one &p2){
if(p1.second == p2.second)
return p1.first < p2.first;
return p1.second < p2.second;
}
void compress(vector<one> &a){
vector<ll>b;
for(auto i: a){
b.push_back(i.first);
b.push_back(i.second);
}
sort(b.begin(), b.end());
b.resize(unique(b.begin(), b.end()) - b.begin());
ll step = 1;
map<ll, ll> mp;
for(auto i: b)
mp[i] = step++;
for(auto &i: a){
i.first = mp[i.first];
i.second = mp[i.second];
}
}
vector<ll>start_here[2 * 100007];
vector<one>a;
#define endl '\n'
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, q;
cin >> n >> q;
a.resize(n);
for(int i = 0; i < n; i++){
cin >> a[i].first >> a[i].second;
a[i].ind = i;
}
compress(a);
sort(a.begin(), a.end(), cmp);
for(auto i: a)
start_here[i.first].push_back(i.ind);
ll l, r;
while(q--){
cin >> l >> r;
//l = 1 + rand() % n, r = 1 + rand() % n;
l--, r--;
vector<ll>dp(a.size());
for(int i = 0; i < a.size(); i++)
dp[i] = 1e9;
dp[r] = 0;
bool ok = false;
multiset<ll>st;
for(int ind = a.size() - 1; ind >= 0; ind--){
auto i = a[ind];
if(i.ind == r){
ok = true;
st.insert(0);
}
else if(ok){
if(st.empty()){
dp[i.ind] = 1e9;
}
else{
dp[i.ind] = *st.begin();
if(dp[i.ind] != (ll)(1e9)){dp[i.ind]++;
st.insert(dp[i.ind]);
}
}
}
if(ind - 1 >= 0){
for(int del = i.second; del > a[ind - 1].second; del--)
for(auto j: start_here[del]){
if(st.find(dp[j]) != st.end())
st.erase(st.find(dp[j]));
}
}
}
if(dp[l] == (ll)(1e9))
cout << "impossible" << endl;
else{
cout << dp[l] << endl;
}
}
return 0;
}
/*
8 1
1 2
3 4
1 5
6 7
5 10
10 20
15 20
999999999 1000000000
1 7
*/
/*
1 2
1 1000000000
*/
/*
5 9
2 0
*/
/*
10 1
1 3
4 6
2 8
8 23
1 4
2 4
6 73
2 34
1 9
8 90
2 3
*/
Compilation message
events.cpp: In function 'int main()':
events.cpp:93:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<one>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1579 ms |
11572 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
7 ms |
5076 KB |
Output is correct |
4 |
Correct |
5 ms |
5076 KB |
Output is correct |
5 |
Correct |
6 ms |
5076 KB |
Output is correct |
6 |
Correct |
11 ms |
5076 KB |
Output is correct |
7 |
Correct |
13 ms |
5076 KB |
Output is correct |
8 |
Correct |
8 ms |
5164 KB |
Output is correct |
9 |
Correct |
11 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
7 ms |
5076 KB |
Output is correct |
4 |
Correct |
5 ms |
5076 KB |
Output is correct |
5 |
Correct |
6 ms |
5076 KB |
Output is correct |
6 |
Correct |
11 ms |
5076 KB |
Output is correct |
7 |
Correct |
13 ms |
5076 KB |
Output is correct |
8 |
Correct |
8 ms |
5164 KB |
Output is correct |
9 |
Correct |
11 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 |
8 ms |
5076 KB |
Output is correct |
13 |
Correct |
6 ms |
5076 KB |
Output is correct |
14 |
Correct |
6 ms |
5076 KB |
Output is correct |
15 |
Correct |
8 ms |
5076 KB |
Output is correct |
16 |
Correct |
13 ms |
5164 KB |
Output is correct |
17 |
Correct |
10 ms |
5164 KB |
Output is correct |
18 |
Correct |
11 ms |
5080 KB |
Output is correct |
19 |
Execution timed out |
1579 ms |
5696 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
7 ms |
5076 KB |
Output is correct |
4 |
Correct |
5 ms |
5076 KB |
Output is correct |
5 |
Correct |
6 ms |
5076 KB |
Output is correct |
6 |
Correct |
11 ms |
5076 KB |
Output is correct |
7 |
Correct |
13 ms |
5076 KB |
Output is correct |
8 |
Correct |
8 ms |
5164 KB |
Output is correct |
9 |
Correct |
11 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 |
7 ms |
5080 KB |
Output is correct |
13 |
Correct |
5 ms |
5116 KB |
Output is correct |
14 |
Correct |
6 ms |
5076 KB |
Output is correct |
15 |
Correct |
9 ms |
5092 KB |
Output is correct |
16 |
Correct |
14 ms |
5076 KB |
Output is correct |
17 |
Correct |
9 ms |
5076 KB |
Output is correct |
18 |
Correct |
13 ms |
5088 KB |
Output is correct |
19 |
Correct |
482 ms |
11592 KB |
Output is correct |
20 |
Correct |
352 ms |
11592 KB |
Output is correct |
21 |
Correct |
708 ms |
11588 KB |
Output is correct |
22 |
Incorrect |
284 ms |
13384 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1583 ms |
11692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Execution timed out |
1579 ms |
11572 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |