Advertisement
Guest User

Untitled

a guest
Apr 28th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define all(x) (x).begin(), (x).end()
  4. #define itn int
  5. #define make_unique(x) sort((x).begin(), (x).end()); (x).erase(unique((x).begin(), (x).end()), (x).end())
  6.  
  7. using namespace std;
  8.  
  9. inline int nxt() {
  10.     int x;
  11.     scanf("%d", &x);
  12.     return x;
  13. }
  14.  
  15. const int N = 111111;
  16.  
  17. struct Shit {
  18.     array<int, N> cnt;
  19.     set<pair<int, int>> S;
  20.     long long total;
  21.  
  22.     Shit() {
  23.         cnt.fill(0);
  24.         total = 0;
  25.     }
  26.  
  27.     void inc(int x) {
  28.         ++cnt[x];
  29.         if (cnt[x] == 2) {
  30.             add(x);
  31.         }
  32.     }
  33.  
  34.     void add(int x) {
  35.         pair<int, int> p = {x, x};
  36.         auto it = S.lower_bound(p);
  37.         vector<pair<int, int>> to_erase;
  38.         if (it != S.end() && it->first == x + 1) {
  39.             p.second = it->second;
  40.             to_erase.push_back(*it);
  41.         }
  42.         if (it != S.begin()) {
  43.             --it;
  44.             if (it->second == x - 1) {
  45.                 p.first = it->first;
  46.                 to_erase.push_back(*it);
  47.             }
  48.         }
  49.         for (const auto& q : to_erase) {
  50.             total -= 1ll * (q.second - q.first + 1) * (q.second - q.first + 2) / 2;
  51.             S.erase(q);
  52.         }
  53.         total += 1ll * (p.second - p.first + 1) * (p.second - p.first + 2) / 2;
  54.         S.insert(p);
  55.     }
  56. };
  57.  
  58. vector<int> ev[2][N];
  59.  
  60. void solve() {
  61.     int n = nxt(), k = nxt();
  62.     vector<int> c(n), d(n);
  63.     for (int i = 0; i < n; ++i) {
  64.         c[i] = nxt();
  65.     }
  66.     for (int i = 0; i < n; ++i) {
  67.         d[i] = nxt();
  68.     }
  69.  
  70.     for (int i = 0; i < N; ++i) {
  71.         ev[0][i].clear();
  72.         ev[1][i].clear();
  73.     }
  74.  
  75.     for (int i = 0; i < n; ++i) {
  76.         ev[0][c[i]].push_back(i);
  77.         ev[1][d[i]].push_back(i);
  78.     }
  79.  
  80.     long long ans = 1ll * n * (n + 1) / 2;
  81.     for (int t : {0, 1}) {
  82.         Shit shit;
  83.         for (int i = 0; i < N; ++i) {
  84.             if (i - k - 1 >= 0) {
  85.                 for (int x : ev[t][i - k - 1]) {
  86.                     shit.inc(x);
  87.                 }
  88.             }
  89.             ans += shit.total;
  90.             for (int x : ev[t ^ 1][i]) {
  91.                 shit.inc(x);
  92.             }
  93.             ans -= shit.total;
  94.         }
  95.     }
  96.  
  97.     cout << ans << "\n";
  98. }
  99.  
  100. int main() {
  101.     int t = nxt();
  102.     for (int i = 1; i <= t; ++i) {
  103.         cout << "Case #" << i << ": ";
  104.         solve();
  105.     }
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement