#include <bits/stdc++.h>
/*
The whole event space is a DAG
Calculate the minimum switching to get from time A to B
Get from an event to another
Event to event
Every event is either completely contained in another, or not
Completely contained = directed edge from contained
Not = earlier end to later end
An O(E) = O(N^2) should get 35 points
Use RQTree to get minimum switches by sorting by end time; O(NQ log N)
You cannot switch to an event with same end time; waste
ST5: Earlier start time = earlier end time
Go backwards and use segment tree
*/
int n,q;
std::vector<int> rqtree;
std::vector<int> endpoints;
int query(int l,int r,int nl=0,int nr=n+1,int ni=0){
//std::cout<<nl<<"/"<<nr<<'\n';
if(nl+1>=nr){
if(l<=endpoints[nl]&&r>endpoints[nl])return rqtree[ni];
return 1000000001;
}
if(l>=endpoints[nr]||r<=endpoints[nl])return 1000000001;
if(l<=endpoints[nl]&&r>=endpoints[nr]){
return rqtree[ni];
}
int nm=(nl+nr)/2;
return std::min(query(l,r,nl,nm,ni+1),query(l,r,nm,nr,ni+2*(nm-nl)));
}
void update(int loc,int val,int nl=0,int nr=n+1,int ni=0){
//std::cout<<"upd "<<nl<<' '<<nr<<'\n';
if(nl+1>=nr){
if(loc==endpoints[nl])rqtree[ni]=val;
return;
}
int nm=(nl+nr)/2;
if(loc<endpoints[nm]){
rqtree[ni]=std::min(rqtree[ni],val);
update(loc,val,nl,nm,ni+1);
}else{
rqtree[ni]=std::min(rqtree[ni],val);
update(loc,val,nm,nr,ni+2*(nm-nl));
}
}
std::vector<std::pair<int,int>> events;
std::vector<std::pair<int,int>> queries;
std::vector<int> s_events;
std::vector<int> i_s_events;
std::vector<int> out_arr;
int main(){
std::cin>>n>>q;
for(int i=0;i<n;i++){
int a,b;
std::cin>>a>>b;
events.push_back({a,b});
endpoints.push_back(b);
s_events.push_back(i);
}
endpoints.push_back(0);
endpoints.push_back(1000000001);
std::sort(endpoints.begin(),endpoints.end());
std::sort(s_events.begin(),s_events.end(),[](int a,int b){return events[a].second==events[b].second?events[a].first<events[b].first:events[a].second<events[b].second;});
i_s_events.resize(n,-1);
for(int i=0;i<n;i++){
i_s_events[s_events[i]]=i;
}
for(int i=0;i<q;i++){
int start,end;
rqtree.clear();
rqtree.resize(2*n+5,1000000001);
std::cin>>start>>end;
start--;end--;//1 indexing
if(start==end){
std::cout<<"0\n";
continue;
}
int s_epos=i_s_events[start];
int e_epos=i_s_events[end];
update(events[start].second,0);//0 switches
for(int k=s_epos+1;k<=e_epos;k++){
//std::cout<<"ev from "<<events[s_events[k]].first<<" to "<<events[s_events[k]].second<<'\n';
int les=query(events[s_events[k]].first,events[s_events[k]].second+1);
//std::cout<<les<<";\n";
update(events[s_events[k]].second,les+1);
if(k==e_epos){
if(les>1000000000){
std::cout<<"impossible\n";
}else{
std::cout<<les+1<<'\n';
}
}
}
//std::cout<<"finish q\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
3456 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1578 ms |
3524 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1583 ms |
3456 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |