MAGCARI

Magnet

Dec 17th, 2022
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 18 December 2022 [08:12]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. int main(){
  10.     cin.tie(0)->sync_with_stdio(0);
  11.     cin.exceptions(cin.failbit);
  12.     int n,m,q;
  13.     vector<pair<int ,int> > v;
  14.     vector<int > firstMagnet;
  15.     cin >> n >> m >> q;
  16.     for(int i=1;i<=m;i++){
  17.         int a,k;
  18.         cin >> a >> k;
  19.         v.push_back({a,1});
  20.         v.push_back({a+k,-1});
  21.     }
  22.     sort(v.begin(),v.end());
  23.  
  24.     int last = 0,cnt = 0,j;
  25.     firstMagnet.push_back(1);
  26.     for(int i=0;i<v.size();i=j){
  27.         for(j=i;j < v.size() && v[i].first == v[j].first;j++)
  28.             cnt+=v[j].second;
  29.         if(cnt%2 != last){
  30.             firstMagnet.push_back(v[i].first);
  31.             last = 1-last;
  32.         }
  33.     }
  34.     firstMagnet.push_back(n+1);
  35.  
  36.     while(q--){
  37.         int num;
  38.         cin >> num;
  39.         int idx = upper_bound(firstMagnet.begin(),firstMagnet.end(),num)-firstMagnet.begin();
  40.         cout << firstMagnet[idx]-firstMagnet[idx-1] << '\n';
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment