CHEFRES

PHOTO EMBED

Fri Aug 13 2021 04:11:57 GMT+0000 (UTC)

Saved by @zs #c++

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ull unsigned long long
using namespace std;
bool check(pair<ull,ull> timing1,pair<ull,ull> timing2){
    return timing1.first>timing2.first ? false:true;
}
int waiting_time(pair<ull,ull> *timing,ull time,int n){
    int s=0,e=n-1;
    while(s<=e){
        ull mid=(s+e)/2;
        if(timing[mid].first>time){
            if(mid==0 || timing[mid-1].second<=time){
            return timing[mid].first-time;
            }
            e=mid-1;
        }
        else if(timing[mid].first<time){
            if(timing[mid].second>time) return 0;
            s=mid+1;
        }
        else return 0;
    }
    return -1;
}
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n,m;
        cin>>n>>m;
        pair<ull,ull> *timing=new pair<ull,ull>[n];
        rep(i,0,n) cin>>timing[i].first>>timing[i].second;
        sort(timing,timing+n,check);
        ull *people=new ull[m];
        rep(i,0,m)  {
          cin>>people[i];
          cout<<waiting_time(timing,people[i],n)<<endl;
       }
    }


return 0;
}
content_copyCOPY