Advertisement
Guest User

Untitled

a guest
Apr 28th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <random>
  5. #include <set>
  6. #include <algorithm>
  7.  
  8. std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
  9.  
  10. long long solve() {
  11.     std::set<int> wtf[2];
  12.     std::set<int> haha[2];
  13.     std::set<int> lul[2];
  14.     int n, k;
  15.     std::cin >> n >> k;
  16.     wtf[0].insert(-1);
  17.     wtf[0].insert(n);
  18.     wtf[1] = wtf[0];
  19.     haha[0] = haha[1] = wtf[0];
  20.     lul[0] = lul[1] = wtf[0];
  21.     std::vector< std::pair<int, int> > events(2 * n);
  22.     for(int i = 0; i < 2 * n; i++) {
  23.         std::cin >> events[i].first;
  24.         events[i].second = i;
  25.     }
  26.     std::sort(events.begin(), events.end());
  27.     std::reverse(events.begin(), events.end());
  28.     long long ans = 0;
  29.     int pt = 0;
  30.     for(auto e : events) {
  31.         int l, m, r;
  32.         m = e.second % n;
  33.         int id = e.second / n;
  34.         {
  35.             auto it = wtf[id].lower_bound(m);
  36.             r = *it;
  37.             it--;
  38.             l = *it + 1;
  39.         }
  40.         while(abs(e.first - events[pt].first) > k) {
  41.             auto ee = events[pt++];
  42.             haha[ee.second / n].erase(ee.second % n);
  43.             lul[ee.second / n].insert(ee.second % n);
  44.         }
  45.         {
  46.             // fixing l
  47.             auto it = lul[id^1].lower_bound(m + 1);
  48.             it--;
  49.             l = std::max(l, *it + 1);
  50.         }
  51.         {
  52.             // fixing r
  53.             auto it = lul[id^1].lower_bound(m);
  54.             r = std::min(r, *it);
  55.         }
  56.         int pl = l - 1;
  57.         {
  58.             // finding pl
  59.             auto it = haha[id^1].lower_bound(m + 1);
  60.             it--;
  61.             pl = std::max(pl, *it);
  62.         }
  63.         int pr = r;
  64.         {
  65.             // finding pr
  66.             auto it = haha[id^1].lower_bound(m);
  67.             pr = std::min(pr, *it);
  68.         }
  69.        
  70.         ans += (long long) (m - l + 1) * (r - pr) + (long long) (pl - l + 1) * (r - m) - (long long) (pl - l + 1) * (r - pr);
  71.         // inserts
  72.         wtf[id].insert(e.second % n);
  73.         haha[id].insert(m);
  74.     }
  75.     return ans;
  76. }
  77.  
  78. int main() {
  79.     std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
  80.     int t;
  81.     std::cin >> t;
  82.     for(int tt = 1; tt <= t; tt++) {
  83.         std::cout << "Case #" << tt << ": " << solve() << '\n';
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement