답안 #799725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799725 2023-07-31T21:11:26 Z MilosMilutinovic Collapse (JOI18_collapse) C++14
100 / 100
3780 ms 21552 KB
#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 = 350;
  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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 724 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 480 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 7 ms 772 KB Output is correct
6 Correct 22 ms 1160 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 8 ms 852 KB Output is correct
10 Correct 19 ms 1108 KB Output is correct
11 Correct 25 ms 1252 KB Output is correct
12 Correct 23 ms 1228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 3404 KB Output is correct
2 Correct 24 ms 3392 KB Output is correct
3 Correct 209 ms 10440 KB Output is correct
4 Correct 49 ms 3652 KB Output is correct
5 Correct 308 ms 10828 KB Output is correct
6 Correct 242 ms 3684 KB Output is correct
7 Correct 2097 ms 18580 KB Output is correct
8 Correct 309 ms 16048 KB Output is correct
9 Correct 26 ms 4928 KB Output is correct
10 Correct 30 ms 4844 KB Output is correct
11 Correct 185 ms 5112 KB Output is correct
12 Correct 324 ms 16932 KB Output is correct
13 Correct 1110 ms 18696 KB Output is correct
14 Correct 2662 ms 21448 KB Output is correct
15 Correct 1988 ms 21552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 3404 KB Output is correct
2 Correct 28 ms 3384 KB Output is correct
3 Correct 39 ms 3536 KB Output is correct
4 Correct 62 ms 3684 KB Output is correct
5 Correct 248 ms 3544 KB Output is correct
6 Correct 296 ms 3700 KB Output is correct
7 Correct 1558 ms 14672 KB Output is correct
8 Correct 3133 ms 20388 KB Output is correct
9 Correct 33 ms 4932 KB Output is correct
10 Correct 278 ms 5100 KB Output is correct
11 Correct 2919 ms 20980 KB Output is correct
12 Correct 3777 ms 21048 KB Output is correct
13 Correct 3151 ms 20904 KB Output is correct
14 Correct 3779 ms 20988 KB Output is correct
15 Correct 3039 ms 20932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 724 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 480 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 7 ms 772 KB Output is correct
6 Correct 22 ms 1160 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 8 ms 852 KB Output is correct
10 Correct 19 ms 1108 KB Output is correct
11 Correct 25 ms 1252 KB Output is correct
12 Correct 23 ms 1228 KB Output is correct
13 Correct 21 ms 3404 KB Output is correct
14 Correct 24 ms 3392 KB Output is correct
15 Correct 209 ms 10440 KB Output is correct
16 Correct 49 ms 3652 KB Output is correct
17 Correct 308 ms 10828 KB Output is correct
18 Correct 242 ms 3684 KB Output is correct
19 Correct 2097 ms 18580 KB Output is correct
20 Correct 309 ms 16048 KB Output is correct
21 Correct 26 ms 4928 KB Output is correct
22 Correct 30 ms 4844 KB Output is correct
23 Correct 185 ms 5112 KB Output is correct
24 Correct 324 ms 16932 KB Output is correct
25 Correct 1110 ms 18696 KB Output is correct
26 Correct 2662 ms 21448 KB Output is correct
27 Correct 1988 ms 21552 KB Output is correct
28 Correct 21 ms 3404 KB Output is correct
29 Correct 28 ms 3384 KB Output is correct
30 Correct 39 ms 3536 KB Output is correct
31 Correct 62 ms 3684 KB Output is correct
32 Correct 248 ms 3544 KB Output is correct
33 Correct 296 ms 3700 KB Output is correct
34 Correct 1558 ms 14672 KB Output is correct
35 Correct 3133 ms 20388 KB Output is correct
36 Correct 33 ms 4932 KB Output is correct
37 Correct 278 ms 5100 KB Output is correct
38 Correct 2919 ms 20980 KB Output is correct
39 Correct 3777 ms 21048 KB Output is correct
40 Correct 3151 ms 20904 KB Output is correct
41 Correct 3779 ms 20988 KB Output is correct
42 Correct 3039 ms 20932 KB Output is correct
43 Correct 317 ms 14264 KB Output is correct
44 Correct 2516 ms 19588 KB Output is correct
45 Correct 373 ms 14848 KB Output is correct
46 Correct 3135 ms 20300 KB Output is correct
47 Correct 32 ms 4940 KB Output is correct
48 Correct 40 ms 4916 KB Output is correct
49 Correct 258 ms 5200 KB Output is correct
50 Correct 362 ms 6644 KB Output is correct
51 Correct 415 ms 15672 KB Output is correct
52 Correct 1062 ms 16828 KB Output is correct
53 Correct 927 ms 16460 KB Output is correct
54 Correct 1689 ms 18188 KB Output is correct
55 Correct 1434 ms 17860 KB Output is correct
56 Correct 2005 ms 18760 KB Output is correct
57 Correct 2567 ms 20560 KB Output is correct
58 Correct 3040 ms 20528 KB Output is correct
59 Correct 2889 ms 20904 KB Output is correct
60 Correct 3780 ms 20952 KB Output is correct