Submission #799724

#TimeUsernameProblemLanguageResultExecution timeMemory
799724MilosMilutinovicCollapse (JOI18_collapse)C++14
5 / 100
15073 ms19268 KiB
#include "collapse.h"
#include <bits/stdc++.h>

using namespace std;

vector<int> simulateCollapse(int n, vector<int> t, vector<int> x, vector<int> y, vector<int> w, vector<int> p) {
  int m = (int) t.size();
  int q = (int) w.size();
  for (int i = 0; i < m; i++) {
    if (x[i] > y[i]) {
      swap(x[i], y[i]);
    }
  }
  vector<int> to(m);
  map<pair<int, int>, int> mp;
  for (int i = 0; i < m; i++) {
    if (t[i] == 0) {
      to[i] = m - 1;
      mp[make_pair(x[i], y[i])] = i;
    } else {
      to[mp[make_pair(x[i], y[i])]] = i - 1;
    }
  }
  vector<vector<int>> qs(m);
  for (int i = 0; i < q; i++) {
    qs[w[i]].push_back(i);
  }
  vector<int> par(n);
  vector<int> sz(n, 1);
  vector<tuple<int, int, int>> stk;
  int comps = 0;
  iota(par.begin(), par.end(), 0);
  function<int(int)> root = [&](int x) {
    if (par[x] == x) {
      return x;
    }
    return root(par[x]);
  };
  auto Unite = [&](int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      stk.emplace_back(-1, -1, -1);
      return;
    }
    if (sz[x] < sz[y]) {
      swap(x, y);
    }
    stk.emplace_back(x, y, sz[y]);
    par[y] = x;
    sz[x] += sz[y];
    comps += 1;
  };
  auto Rollback = [&](int T) {
    while ((int) stk.size() > T) {
      auto it = stk.back();
      stk.pop_back();
      int x = get<0>(it);
      int y = get<1>(it);
      int s = get<2>(it);
      if (x == -1) {
        continue;
      }
      par[y] = y;
      sz[y] = s;
      sz[x] -= s;
      comps -= 1;
    }
  };
  const int BLOCK = 6;
  vector<int> ans(q);
  for (int l = 0; l < m; l += BLOCK) {
    int r = min(m - 1, l + BLOCK - 1);
    vector<int> full;
    vector<int> half;
    for (int i = 0; i <= r; i++) {
      if (t[i] == 1 || to[i] < l) {
        continue;
      }
      if (i <= l && to[i] >= r) {
        full.push_back(i);
      } else {
        half.push_back(i);
      }
    }
    vector<int> qq;
    for (int i = l; i <= r; i++) {
      for (int j : qs[i]) {
        qq.push_back(j);
      }
    }
    {
      sort(qq.begin(), qq.end(), [&](int i, int j) {
        return p[i] < p[j];
      });
      sort(full.begin(), full.end(), [&](int i, int j) {
        return y[i] < y[j];
      });
      int ptr = 0;
      for (int i : qq) {
        while (ptr < (int) full.size() && y[full[ptr]] <= p[i]) {
          Unite(x[full[ptr]], y[full[ptr]]);
          ptr += 1;
        }
        int T = (int) stk.size();
        for (int j : half) {
          if (j <= w[i] && to[j] >= w[i] && y[j] <= p[i]) {
            Unite(x[j], y[j]);
          }
        }
        ans[i] += (p[i] + 1) - comps;
        Rollback(T);
      }
      Rollback(0);
    }
    {
      sort(qq.begin(), qq.end(), [&](int i, int j) {
        return p[i] > p[j];
      });
      sort(full.begin(), full.end(), [&](int i, int j) {
        return x[i] > x[j];
      });
      int ptr = 0;
      for (int i : qq) {
        while (ptr < (int) full.size() && x[full[ptr]] > p[i]) {
          Unite(x[full[ptr]], y[full[ptr]]);
          ptr += 1;
        }
        int T = (int) stk.size();
        for (int j : half) {
          if (j <= w[i] && to[j] >= w[i] && x[j] > p[i]) {
            Unite(x[j], y[j]);
          }
        }
        ans[i] += (n - p[i] - 1) - comps;
        Rollback(T);
      }
      Rollback(0);
    }
  }
  return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...