hiho 1590

题目链接

小Hi的公司最近员工增长迅速,同时大大小小的会议也越来越多;导致公司内的M间会议室非常紧张。

现在小Hi知道公司目前有N个会议,其中第i个会议的时间区间是(Si, Ei)。 注意这里时间区间可以视为是开区间,也就是说(3, 5)和(5, 6)不会被视为是同时进行的会议。  

小Hi想知道如果他新增一个会议,时间区间是(X, Y),会不会导致出现会议室不够用的情况?

已知目前的N个会议不会导致会议室不够用。  

输入

第一行包含两个整数:N和M。  

以下N行每行两个整数Si和Ei,代表一个会议的时间区间。  

之后一行包含一个整数Q,代表小Hi询问的次数。  

以下Q行每行包含两个整数Xi和Yi,表示小Hi希望新增的会议时间。  

对于30%的数据,1 <= N, M, Q <= 1000  

对于100%的数据,1 <= N, M, Q <= 100000 0 <= Si < Ei <= 100000000 0 <= Xi < Yi <= 100000000

----------------------------------------------------------------------------------------------------------------------------------------------------

差劲。简单的区间题wa了这么多次。

首先肯定是统计现有的厚度为m的区间,这样来了一个新的区间只需判断是否和现有的区间交叉。

判断是否交叉不如判断是否不交叉,不交叉只有三种情况:在两边是两种,还有一种是落在区间的空隙里。

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))
#define OO 0x0fffffff
using namespace std;
typedef long long LL;
const int N = 100100;
struct Node{
    int t;
    bool isstart;
    Node(){}
    Node(int t,bool isstart):t(t),isstart(isstart){}
    bool operator<(const Node& another) const{
        if(t==another.t) return isstart<another.isstart;
        return t<another.t;
    }
};
Node nodes[N*2];
int main(){
    int n,m,s,e; cin>>n>>m;
    for(int i=0;i<n;i++){
        scanf("%d%d",&s,&e);
        nodes[i*2+0]=Node(s,true);
        nodes[i*2+1]=Node(e,false);
    }
    std::sort(nodes,nodes+n*2);
    int cnt = 0;
    vector<int> sets;
    for(int i=0;i<n*2;i++){
        if(!nodes[i].isstart) cnt--;
        else if(++cnt==m){
            sets.push_back(nodes[i+0].t);
            sets.push_back(nodes[i+1].t);
        }
    }
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%d%d",&s,&e);
        vector<int>::iterator sec = std::lower_bound(sets.begin(),sets.end(),e);
        if(s>=*(sets.end()-1)||sec==sets.begin()||(((sec-sets.begin())%2==0)&&(*(sec-1)<=s))) {
            puts("YES");
        }
        else{
            puts("NO");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/redips-l/p/7600983.html