1004-不醒人室
区间维护,结构体排序即可
这道题的输入注意,如果使用cin需要加上ios优化,不然会T。
#include<iostream>
#include<algorithm>
#define endl '\n'
using namespace std;
struct node{
int st,ed;
int isclass;
};
bool cmp(node a,node b){
if(a.st==b.st){
if(a.isclass==b.isclass&&a.isclass==1){
return a.ed<b.ed;
}else if(a.isclass==b.isclass&&a.isclass==0){
return a.ed>b.ed;
}
return a.isclass<b.isclass;
}
return a.st<b.st;
}
void solve(){
int n,m;
cin>>n>>m;
node a[n+m+2];
for(int i=1;i<=n;i++){
int x,y;cin>>x>>y;
a[i].st=x;a[i].ed=y;
a[i].isclass=1;
}
for(int i=n+1;i<=n+m;i++){
int x,y;cin>>x>>y;
a[i].st=x;a[i].ed=y;
a[i].isclass=0;
}
sort(a+1,a+1+n+m,cmp);
for(int i=2;i<=n+m;i++){
if(a[i].st<a[i-1].ed&&a[i].isclass!=a[i-1].isclass){
cout<<"No"<<endl;
return ;
}
}
for(int i=1;i<=n+m;i++){
if(!a[i].isclass){
int time=a[i].ed-a[i].st;
a[i].st=a[i].ed;
a[i].ed+=time*2;
}
}
sort(a+1,a+1+n+m,cmp);
int cover=0;
for(int i=1;i<=n+m;i++){
if(!a[i].isclass){
cover=max(cover,a[i].ed);
}else{
if(cover<a[i].ed){
cout<<"No"<<endl;
return ;
}
}
}
cout<<"Yes"<<endl;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
// freopen("./1004.in","r",stdin);
// freopen("./1004.out","w",stdout);
int T;cin>>T;
while(T--) solve();
}