Advertisement
CyberN00b

Untitled

Dec 4th, 2022
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define LINT long long int
  3.  
  4. using namespace std;
  5.  
  6. struct Comp {
  7.     int start_time;
  8.     int end_time;
  9.     int cost;
  10.  
  11.     Comp(int s, int e, int c) : start_time(s), end_time(e), cost(c){}
  12.  
  13.     Comp() : start_time(0), end_time(0), cost(0){}
  14. };
  15.  
  16. int main()
  17. {
  18.     int a;
  19.     cin >> a;
  20.     vector<pair<int, LINT> > s; // cost
  21.     vector<pair<int, LINT> > s1; // time
  22.     vector<Comp> vc(a, *(new Comp()));
  23.     for (auto& x : vc) {
  24.         cin >> x.start_time >> x.end_time >> x.cost;
  25.     }
  26.     sort(vc.begin(), vc.end(), [](Comp& a, Comp& b){return a.start_time < b.start_time;});
  27.     LINT prev = 0;
  28.     for (const auto& x : vc) {
  29.         prev += x.cost;
  30.         s.push_back({x.start_time, prev});
  31.     }
  32.     sort(vc.begin(), vc.end(), [](Comp& a, Comp& b){return a.end_time < b.end_time;});
  33.     prev = 0;
  34.     for (const auto& x : vc) {
  35.         prev += x.end_time - x.start_time;
  36.         s1.push_back({x.end_time, prev});
  37.     }
  38.     int b;
  39.     cin >> b;
  40.     for (int i = 0; i < b; ++i) {
  41.         int a1, a2, a3;
  42.         cin >> a1 >> a2 >> a3;
  43.         if (a3 == 1) {
  44.             auto l = upper_bound(s.begin(), s.end(), pair<int, LINT>({a1, INT64_MIN}));
  45.             auto r = upper_bound(l, s.end(), pair<int, LINT>({a2, INT64_MAX}));
  46.             if (r == s.begin()) {
  47.                 cout << 0 << ' ';
  48.                 continue;
  49.             }
  50.             if (l == s.begin()) {
  51.                 r--;
  52.                 cout << r->second << ' ';
  53.                 continue;
  54.             }
  55.             l--;
  56.             r--;
  57.             cout << r->second - l->second << ' ';
  58.         } else {
  59.             auto l = upper_bound(s1.begin(), s1.end(), pair<int, LINT>({a1, INT64_MIN}));
  60.             auto r = upper_bound(l, s1.end(), pair<int, LINT>({a2, INT64_MAX}));
  61.             if (r == s1.begin()) {
  62.                 cout << 0 << ' ';
  63.                 continue;
  64.             }
  65.             if (l == s1.begin()) {
  66.                 r--;
  67.                 cout << r->second << ' ';
  68.                 continue;
  69.             }
  70.             l--;
  71.             r--;
  72.             cout << r->second - l->second << ' ';
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement