Guest User

Untitled

a guest
Dec 27th, 2018
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 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.  
  33. map<long long, long long> dfs(int v) {
  34.   map<long long, long long> mpv;
  35.   s[v] = 0;
  36.   for (int i = 0; i < sz(g[v]); i++) {
  37.     int u = g[v][i];
  38.     auto mpu = dfs(u);
  39.     if (sz(mpv) < sz(mpu)) {
  40.       swap(mpv, mpu);
  41.     }
  42.     for (auto it = mpu.begin(); it != mpu.end(); it++) {
  43.       mpv[it->first] += it->second;
  44.     }
  45.     s[v] += s[u];
  46.   }
  47.   while (true) {
  48.     if (!mpv.empty()) {
  49.       auto it = mpv.end();
  50.       it--;
  51.       if (it->first >= -d[v]) {
  52.         s[v] -= it->second;
  53.         mpv.erase(it);
  54.         continue;
  55.       }
  56.       if (s[v] >= c[v]) {
  57.         if (s[v] - it->second < c[v]) {
  58.           it->second -= s[v] - c[v];
  59.           s[v] = c[v];
  60.           break;
  61.         }
  62.         s[v] -= it->second;
  63.         mpv.erase(it);
  64.         continue;
  65.       }
  66.     }
  67.     mpv[-d[v]] = c[v] - s[v];
  68.     s[v] = c[v];
  69.     break;
  70.   }
  71.   return mpv;
  72. }
  73.  
  74. struct CoverTreePaths {
  75.   long long minimumCost(int _n, vector<int> pp, vector<int> dd, vector<int> cc, vector<int> params) {
  76.     n = _n;
  77.     for (int i = 0; i < n; i++) {
  78.       g[i].clear();
  79.     }
  80.     {
  81.       int t = sz(dd);
  82.       for (int i = 0; i < t - 1; i++) {
  83.         g[pp[i]].push_back(i + 1);
  84.       }
  85.       int x = pp[t - 2];
  86.       for (int i = t - 1; i < n - 1; i++) {
  87.         x = ((long long) params[0] * x + params[1]) % (i + 1);
  88.         g[x].push_back(i + 1);
  89.       }
  90.       for (int i = 0; i < t; i++) {
  91.         d[i] = dd[i];
  92.       }
  93.       x = dd[t - 1];
  94.       for (int i = t; i < n; i++) {
  95.         x = 1 + ((long long) params[2] * x + params[3]) % params[4];
  96.         d[i] = x;
  97.       }
  98.       for (int i = 0; i < t; i++) {
  99.         c[i] = cc[i];
  100.       }
  101.       x = cc[t - 1];
  102.       for (int i = t; i < n; i++) {
  103.         x = 1 + ((long long) params[5] * x + params[6]) % params[7];
  104.         c[i] = x;
  105.       }
  106.     }
  107.     auto mp = dfs(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