Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6.  
  7. vector<long long> C[100005];
  8. long long Val[100005];
  9.  
  10. map<long long, int> taken;
  11. int n;
  12.  
  13.  
  14. int nodes = 0;
  15. pair<int, int> T[7000005];
  16. int Left[7000005], Right[7000005];
  17.  
  18. bool cmp(pair<int, int> a, pair<int, int> b) {
  19.     return C[a.first][a.second] < C[b.first][b.second];
  20. }
  21.  
  22. int inc(int x, int b, int e, int pos, int val) {
  23.     int ret = ++nodes;
  24.     Left[ret] = Left[x];
  25.     Right[ret] = Right[x];
  26.     if(b == e) {
  27.         if(val == -1)
  28.             return 0;
  29.         T[ret] = {pos, val};
  30.     } else {
  31.         int m = (b + e) / 2;
  32.         if(pos <= m) Left[ret] = inc(Left[ret], b, m, pos, val);
  33.         else Right[ret] = inc(Right[ret], m + 1, e, pos, val);
  34.         T[ret] = min(T[Left[ret]], T[Right[ret]], cmp);
  35.     }
  36.     return ret;
  37. }
  38.  
  39. struct qnode {
  40.     long long has, cost;
  41.     int root;
  42.    
  43.     long long getcost() const {
  44.         return cost + C[T[root].first][T[root].second];
  45.     }
  46.     long long gethash() const {
  47.         return has + Val[T[root].first];
  48.     }
  49.    
  50.     bool operator<(const qnode &oth) const {
  51.         return getcost() > oth.getcost();
  52.     }
  53. };
  54.  
  55. int32_t main() {
  56.    
  57.     //srand(time(0));
  58.     int k;
  59.    
  60.     freopen("roboherd.in", "r", stdin);
  61.     freopen("roboherd.out", "w", stdout);
  62.    
  63.     cin >> n >> k;
  64.    
  65.    
  66.     T[0] = {n + 1, 0};
  67.     C[n + 1].push_back(1e18);
  68.    
  69.     for(int i = 0; i < n; ++i) {
  70.         int cnt;
  71.         cin >> cnt;
  72.         C[i].resize(cnt);
  73.         Val[i] = 1LL * rand() * rand();
  74.         for(auto &x : C[i])
  75.             cin >> x;
  76.         sort(C[i].begin(), C[i].end());
  77.         for(int j = C[i].size() - 1; j; --j)
  78.             C[i][j] -= C[i][j - 1];
  79.     }
  80.    
  81.     int rt = 0;
  82.     for(int i = 0; i < n; ++i) {
  83.         if(C[i].size() > 1)
  84.             rt = inc(rt, 0, n, i, 1);
  85.     }
  86.    
  87.     priority_queue<qnode> Q;
  88.    
  89.    
  90.    
  91.     auto push = [&](long long has, long long cost, int rt) {
  92.         if(T[rt].first >= n || rt == 0) return;
  93.        
  94.         qnode node;
  95.         node.root = rt;
  96.         node.cost = cost;
  97.         node.has = has;
  98.        
  99.         if(taken.count(node.gethash())) return;
  100.         taken[node.gethash()] = rt;
  101.        
  102.        
  103.         Q.push(node);
  104.     };
  105.    
  106.     long long ans = 0;
  107.    
  108.     long long cost = 0, has = 0;
  109.     for(int i = 0; i < n; ++i) {
  110.         cost += C[i][0];
  111.         has += Val[i];
  112.     }
  113.     ans += cost;
  114.     taken[has] = rt;
  115.     k -= 1;
  116.    
  117.     //cerr << "HERE\n";
  118.    
  119.     push(has, cost, rt);
  120.    
  121.     for(int it = 0; it < k; ++it) {
  122.         auto top = Q.top();
  123.         long long cost = top.cost;
  124.         int root = top.root;
  125.         Q.pop();
  126.         ans += top.getcost();
  127.        
  128.        
  129.         //cerr << it << " " << top.getcost() << " " << top.gethash() << endl;
  130.        
  131.         auto p = T[root];
  132.        
  133.         long long nwcost = top.getcost();
  134.         long long nwhash = top.gethash();
  135.         int nwroot;
  136.         if(p.second + 1 < C[p.first].size())
  137.             nwroot = inc(root, 0, n, p.first, p.second + 1);
  138.         else nwroot = inc(root, 0, n, p.first, -1);
  139.         push(nwhash, nwcost, nwroot);
  140.        
  141.        
  142.         root = inc(root, 0, n, p.first, -1);
  143.         push(top.has, top.cost, root);
  144.     }
  145.    
  146.     cout << ans << endl;
  147.    
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement