Advertisement
Guest User

Untitled

a guest
Dec 27th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #ifdef DEBUG
  2. #define _GLIBCXX_DEBUG
  3. #endif
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. typedef long double ld;
  10.  
  11. #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
  12.  
  13. #define sz(x) ((int) (x).size())
  14. #define TASK "text"
  15.  
  16. const int inf = (int) 1.01e9;
  17. const long long infll = (long long) 1.01e18;
  18. const ld eps = 1e-9;
  19. const ld pi = acos((ld) -1);
  20.  
  21. mt19937 mrand(random_device{} ());
  22.  
  23. int rnd(int x) {
  24.   return mrand() % x;
  25. }
  26.  
  27. const int maxn = (int) 1e5 + 5;
  28. int n;
  29. int d[maxn], c[maxn];
  30. vector<int> g[maxn];
  31. long long s[maxn];
  32. map<long long, long long> *mps[maxn];
  33.  
  34. struct CoverTreePaths {
  35.   long long minimumCost(int _n, vector<int> pp, vector<int> dd, vector<int> cc, vector<int> params) {
  36.     n = _n;
  37.     for (int i = 0; i < n; i++) {
  38.       g[i].clear();
  39.     }
  40.     {
  41.       int t = sz(dd);
  42.       for (int i = 0; i < t - 1; i++) {
  43.         g[pp[i]].push_back(i + 1);
  44.       }
  45.       int x = pp[t - 2];
  46.       for (int i = t - 1; i < n - 1; i++) {
  47.         x = ((long long) params[0] * x + params[1]) % (i + 1);
  48.         g[x].push_back(i + 1);
  49.       }
  50.       for (int i = 0; i < t; i++) {
  51.         d[i] = dd[i];
  52.       }
  53.       x = dd[t - 1];
  54.       for (int i = t; i < n; i++) {
  55.         x = 1 + ((long long) params[2] * x + params[3]) % params[4];
  56.         d[i] = x;
  57.       }
  58.       for (int i = 0; i < t; i++) {
  59.         c[i] = cc[i];
  60.       }
  61.       x = cc[t - 1];
  62.       for (int i = t; i < n; i++) {
  63.         x = 1 + ((long long) params[5] * x + params[6]) % params[7];
  64.         c[i] = x;
  65.       }
  66.     }
  67.     for (int v = n - 1; v >= 0; v--) {
  68.       mps[v] = new map<long long, long long>();
  69.       auto &mpv = mps[v];
  70.       s[v] = 0;
  71.       for (int i = 0; i < sz(g[v]); i++) {
  72.         int u = g[v][i];
  73.         auto mpu = mps[u];
  74.         if (sz(*mpv) < sz(*mpu)) {
  75.           swap(mpv, mpu);
  76.         }
  77.         for (auto it = mpu->begin(); it != mpu->end(); it++) {
  78.           (*mpv)[it->first] += it->second;
  79.         }
  80.         s[v] += s[u];
  81.       }
  82.       while (true) {
  83.         if (!mpv->empty()) {
  84.           auto it = mpv->end();
  85.           it--;
  86.           if (it->first >= -d[v]) {
  87.             s[v] -= it->second;
  88.             mpv->erase(it);
  89.             continue;
  90.           }
  91.           if (s[v] >= c[v]) {
  92.             if (s[v] - it->second < c[v]) {
  93.               it->second -= s[v] - c[v];
  94.               s[v] = c[v];
  95.               break;
  96.             }
  97.             s[v] -= it->second;
  98.             mpv->erase(it);
  99.             continue;
  100.           }
  101.         }
  102.         (*mpv)[-d[v]] = c[v] - s[v];
  103.         s[v] = c[v];
  104.         break;
  105.       }
  106.     }
  107.     auto mp = *mps[0];
  108.     long long val = 0;
  109.     long long d = 0;
  110.     for (auto it = mp.begin(); it != mp.end(); it++) {
  111.       d += it->second;
  112.       auto nit = it;
  113.       nit++;
  114.       long long x;
  115.       if (nit == mp.end()) {
  116.         x = -it->first;
  117.       } else {
  118.         x = nit->first - it->first;
  119.       }
  120.       val += x * d;
  121.     }
  122.     return val;
  123.   }
  124. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement