#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 long long ll;
struct one{
ll first, second, ind;
};
bool cmp(one p1, one p2){
if(p1.first == p2.first)
return p1.second < p2.second;
return p1.first < p2.first;
}
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];
}
}
ll dp[5007][5007];
vector<ll>end_here[5007];
vector<one>a;
void count_dp(one start){
for(int i = 0; i < a.size(); i++)
dp[start.ind][i] = 1e9;
dp[start.ind][start.ind] = 0;
bool ok = false;
multiset<ll>st;
for(int ind = 0; ind < a.size(); ind++){
auto i = a[ind];
if(i.ind == start.ind){
ok = true;
st.insert(0);
}
else if(ok){
if(st.empty()){
dp[start.ind][i.ind] = 1e9;
st.insert(1e9);
}
else{
dp[start.ind][i.ind] = *st.begin();
if(*st.begin() != (ll)(1e9))
dp[start.ind][i.ind]++;
st.insert(dp[start.ind][i.ind]);
}
}
else
st.insert(1e9);
if(ind + 1 < a.size()){
for(int del = i.first; del < a[ind + 1].first; del++)
for(auto j: end_here[del]){
st.erase(st.find(dp[start.ind][j]));
}
}
}
}
int main(){
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)
end_here[i.second].push_back(i.ind);
for(auto i: a){
count_dp(i);
}
while(q--){
ll l, r;
cin >> l >> r;
l--, r--;
if(dp[l][r] == (ll)(1e9))
cout << "impossible" << endl;
else
cout << dp[l][r] << endl;
}
return 0;
}
Compilation message
events.cpp: In function 'void count_dp(one)':
events.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<one>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
events.cpp:68:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<one>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int ind = 0; ind < a.size(); ind++){
| ~~~~^~~~~~~~~~
events.cpp:91:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<one>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | if(ind + 1 < a.size()){
| ~~~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Runtime error |
188 ms |
24972 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
49 ms |
12364 KB |
Output is correct |
4 |
Correct |
45 ms |
12384 KB |
Output is correct |
5 |
Incorrect |
61 ms |
12412 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
49 ms |
12364 KB |
Output is correct |
4 |
Correct |
45 ms |
12384 KB |
Output is correct |
5 |
Incorrect |
61 ms |
12412 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
49 ms |
12364 KB |
Output is correct |
4 |
Correct |
45 ms |
12384 KB |
Output is correct |
5 |
Incorrect |
61 ms |
12412 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
186 ms |
24964 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Runtime error |
188 ms |
24972 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |