Sanath12345

Untitled

Sep 24th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define loop(i,a,b) for(int i=(a);i<=(b);i++)
  4. #define read(a,n) for(int i=0;i<n;i++) cin>>a[i];
  5. #define setbits(n) __builtin_popcount(n)
  6. #define lsetbits(n) __buitin_popcountll(n)
  7. #define pb push_back
  8. typedef long long int ll;
  9. typedef struct interval{
  10. ll start;
  11. ll end;
  12.  
  13. }prob;
  14. bool compare(prob x,prob y){
  15. if(x.start<y.start) return true;
  16. else return false;
  17. }
  18.  
  19. ll waitingtime(prob a[],int index,int n,ll t){
  20. // cout<<t<<" "<<index<<endl;
  21. if(index==0){
  22. return a[0].start-t;
  23. }
  24. else{
  25. if(a[index-1].end>=t) return 0;
  26. else{
  27. if(index==n) return -1;
  28. else return a[index].start-t;
  29. }
  30. }
  31. }
  32.  
  33. int main(){
  34. int t;
  35. cin>>t;
  36. while(t--){
  37. int n,m;
  38. cin>>n>>m;
  39. prob a[n];
  40. ll time[m];
  41. loop(i,0,n-1){
  42. ll x,y;
  43. cin>>x>>y;
  44. a[i].start=x;
  45. a[i].end=(y-1);
  46.  
  47. }
  48. sort(a,a+n,compare);
  49. loop(i,0,m-1){
  50. cin>>time[i];
  51. int l=0,r=n-1;
  52. int index=n;
  53. while(l<=r){
  54. int mid=l+(r-l)/2;
  55. if(a[mid].start>=time[i]){
  56. index=mid;
  57. r=mid-1;
  58. }
  59. else{
  60. l=mid+1;
  61. }
  62. }
  63.  
  64. ll ans=waitingtime(a,index,n,time[i]);
  65. cout<<ans<<endl;
  66. }
  67.  
  68.  
  69. }
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment