v1km4n

Untitled

Apr 19th, 2021
444
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. using namespace std;
  7. int n;
  8.  
  9. struct myStruct {
  10.     int coord;
  11.     int number;
  12.     short flag;
  13.     myStruct(int coord = 0, int number = 0, short flag = 0) {
  14.         this->coord = coord;
  15.         this->number = number;
  16.         this->flag = flag;
  17.     }
  18. };
  19.  
  20. bool compare(myStruct a, myStruct b)
  21. {
  22.     if (a.coord != b.coord)
  23.         return a.coord < b.coord;
  24.     else return a.number < b.number;
  25. }
  26.  
  27. int main()
  28. {
  29.     vector < myStruct > myVector;
  30.     int a, b;
  31.     cin >> n;
  32.     for (int i = 0; i < n; i++) {
  33.         cin >> a >> b;
  34.         myVector.push_back(myStruct(a, (i + 1), 1));
  35.         myVector.push_back(myStruct(b, (i + 1), -1));
  36.     }
  37.  
  38.     sort(myVector.begin(), myVector.end(), compare);
  39.  
  40.     /*for (auto i = myVector.begin(); i != myVector.end(); i++) {
  41.         std::cout << i->coord << " " << i->number << " " << i->flag << std::endl;
  42.     }*/
  43.  
  44.     int m;
  45.     cin >> m;
  46.     int temp = 0;
  47.  
  48.     int leftBorder = myVector[0].coord;
  49.     int rightBorder = myVector.back().coord;
  50.  
  51.     for (int i = 0; i < m; i++) {
  52.         cin >> a;
  53.         if ((a > rightBorder) || (a < leftBorder)) {
  54.             cout << "-1" << endl;
  55.         }
  56.         else {
  57.             int upper = upper_bound(myVector.begin(), myVector.end(), myStruct(a, INT32_MAX, 0), compare) - myVector.begin();
  58.             if (myVector[upper - 1].coord == a) {
  59.                 cout << myVector[upper - 1].number << endl;
  60.                 continue;
  61.             }
  62.             temp = 0;
  63.             for (int i = upper; i < myVector.size(); i++) {
  64.                 temp += myVector[i].flag;
  65.                 if (temp  == -1) {
  66.                     cout << myVector[i].number << endl;
  67.                     break;
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
  73.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×