답안 #294670

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
294670 2020-09-09T08:17:08 Z rama_pang Collapse (JOI18_collapse) C++14
100 / 100
6860 ms 20336 KB
#include "collapse.h"
#include <bits/stdc++.h>
using namespace std;

class disjoint_set {
 public:
  disjoint_set() {}
  disjoint_set(int n) : n_(n), comp_count(n), comp(n, -1) {}

  int find_root(int x) {
    return (comp[x] < 0) ? x : find_root(comp[x]);
  }

  int merge(int x, int y) {
    x = find_root(x);
    y = find_root(y);
    if (x == y) {
      return 0;
    }
    if (-comp[x] > -comp[y]) {
      swap(x, y);
    }
    history.push_back({x, comp[x]});
    history.push_back({y, comp[y]});
    comp[y] += comp[x];
    comp[x] = y;
    comp_count--;
    return 1;
  }

  void rollback(int x) {
    comp_count += x;
    for (int i = 0; i < 2 * x; i++) {
      comp[history.back().first] = history.back().second;
      history.pop_back();
    }
  }

  void make_set() {
    n_ += 1;
    comp.emplace_back(-1);
  }

  int size(int x) {
    return -comp[find_root(x)];
  }

  int size() {
    return comp_count;
  }

 private:
  int n_;
  int comp_count;
  vector<int> comp;
  vector<pair<int, int>> history;
};
vector<int> simulateCollapse(int N, vector<int> T, 
                                    vector<int> X, 
                                    vector<int> Y, 
                                    vector<int> W, 
                                    vector<int> P) {
  int C = (int) T.size();
  int Q = (int) W.size();
  vector<int> ans(Q);
  vector<array<int, 4>> events;
  for (int i = 0; i < C; i++) {
    if (X[i] > Y[i]) {
      swap(X[i], Y[i]);
    }
    if (T[i] == 0) {
      events.push_back({i, -1, X[i], Y[i]});
    } else {
      events.push_back({i, -2, X[i], Y[i]});
    }
  }
  for (int i = 0; i < Q; i++) {
    events.push_back({W[i], i, P[i], P[i] + 1});
  }
  for (int rep = 0; rep < 2; rep++) {
    struct edge {
      int u, v;
      edge() : u(0), v(0) {}
      edge(int u, int v) : u(u), v(v) {}
      const bool operator < (const edge &o) const {
        return make_pair(u, v) < make_pair(o.u, o.v);
      }
    };
    struct interval {
      int l, r;
      interval() : l(0), r(0) {}
      interval(int l, int r) : l(l), r(r) {}
    };

    const int BLOCK = 777;
    const int INF = 1e8;

    set<edge> active_edges;
    sort(begin(events), end(events));
    for (int current = 0; current < (int) events.size(); current += BLOCK) {
      set<edge> changed_edges;
      for (int i = current; i < min(int(events.size()), current + BLOCK); i++) {
        if (events[i][1] < 0) {
          changed_edges.insert({events[i][2], events[i][3]});
        }
      }

      map<edge, int> active_time;
      vector<edge> edges_permanent;
      for (auto e : active_edges) {
        if (changed_edges.count(e) == 0) {
          edges_permanent.push_back(e);
        } else {
          active_time[e] = 0;
        }
      }

      vector<array<int, 3>> queries; // (x-coord, time, id)
      vector<pair<edge, interval>> edges_temp;
      for (int i = current; i < min(int(events.size()), current + BLOCK); i++) {
        if (events[i][1] == -1) {
          active_time[edge(events[i][2], events[i][3])] = events[i][0];
        } else if (events[i][1] == -2) {
          edge e = edge(events[i][2], events[i][3]);
          edges_temp.push_back(make_pair(e, interval(active_time[e], events[i][0])));
          active_time.erase(e);
        } else {
          queries.push_back({events[i][2], events[i][0], events[i][1]});
        }
      }
      for (auto e : active_time) {
        edges_temp.push_back(make_pair(e.first, interval(e.second, INF)));
      }
      sort(begin(edges_permanent), end(edges_permanent), [](const edge &e1, const edge &e2) {
        return e1.v < e2.v;
      });
      reverse(begin(edges_permanent), end(edges_permanent));
      sort(begin(queries), end(queries));

      disjoint_set dsu(N);
      for (auto q : queries) {
        while (!edges_permanent.empty() && edges_permanent.back().v <= q[0]) {
          int u = edges_permanent.back().u;
          int v = edges_permanent.back().v;
          edges_permanent.pop_back();
          dsu.merge(u, v);
        }

        int rollbacks = 0;
        for (auto e : edges_temp) {
          if (e.first.v <= q[0] && e.second.l <= q[1] && q[1] < e.second.r) {
            rollbacks += dsu.merge(e.first.u, e.first.v);
          }
        }
        ans[q[2]] += dsu.size() - (N - q[0] - 1);
        dsu.rollback(rollbacks);
      }

      for (int i = current; i < min(int(events.size()), current + BLOCK); i++) {
        if (events[i][1] == -1) {
          assert(active_edges.count({events[i][2], events[i][3]}) == 0);
          active_edges.insert({events[i][2], events[i][3]});
        } else if (events[i][1] == -2) {
          assert(active_edges.count({events[i][2], events[i][3]}) == 1);
          active_edges.erase({events[i][2], events[i][3]});
        }
      }
    }
    for (int i = 0; i < (int) events.size(); i++) {
      events[i][2] = N - events[i][2] - 1;
      events[i][3] = N - events[i][3] - 1;
      swap(events[i][2], events[i][3]);
    }
  }
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1024 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 5 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 15 ms 1024 KB Output is correct
6 Correct 34 ms 1148 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 17 ms 1024 KB Output is correct
10 Correct 35 ms 1024 KB Output is correct
11 Correct 52 ms 1372 KB Output is correct
12 Correct 47 ms 1336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 4840 KB Output is correct
2 Correct 69 ms 4980 KB Output is correct
3 Correct 284 ms 10216 KB Output is correct
4 Correct 72 ms 4980 KB Output is correct
5 Correct 453 ms 10472 KB Output is correct
6 Correct 154 ms 5360 KB Output is correct
7 Correct 4009 ms 16480 KB Output is correct
8 Correct 482 ms 11112 KB Output is correct
9 Correct 79 ms 5236 KB Output is correct
10 Correct 86 ms 5424 KB Output is correct
11 Correct 108 ms 5484 KB Output is correct
12 Correct 510 ms 11368 KB Output is correct
13 Correct 1995 ms 13792 KB Output is correct
14 Correct 5356 ms 17768 KB Output is correct
15 Correct 4233 ms 18440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 4848 KB Output is correct
2 Correct 79 ms 4848 KB Output is correct
3 Correct 86 ms 4972 KB Output is correct
4 Correct 83 ms 4972 KB Output is correct
5 Correct 93 ms 5100 KB Output is correct
6 Correct 180 ms 5360 KB Output is correct
7 Correct 2869 ms 13844 KB Output is correct
8 Correct 5721 ms 16676 KB Output is correct
9 Correct 83 ms 5228 KB Output is correct
10 Correct 132 ms 5736 KB Output is correct
11 Correct 5175 ms 19860 KB Output is correct
12 Correct 6825 ms 20336 KB Output is correct
13 Correct 5748 ms 20192 KB Output is correct
14 Correct 6821 ms 20036 KB Output is correct
15 Correct 5534 ms 19844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1024 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 5 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 15 ms 1024 KB Output is correct
6 Correct 34 ms 1148 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 17 ms 1024 KB Output is correct
10 Correct 35 ms 1024 KB Output is correct
11 Correct 52 ms 1372 KB Output is correct
12 Correct 47 ms 1336 KB Output is correct
13 Correct 62 ms 4840 KB Output is correct
14 Correct 69 ms 4980 KB Output is correct
15 Correct 284 ms 10216 KB Output is correct
16 Correct 72 ms 4980 KB Output is correct
17 Correct 453 ms 10472 KB Output is correct
18 Correct 154 ms 5360 KB Output is correct
19 Correct 4009 ms 16480 KB Output is correct
20 Correct 482 ms 11112 KB Output is correct
21 Correct 79 ms 5236 KB Output is correct
22 Correct 86 ms 5424 KB Output is correct
23 Correct 108 ms 5484 KB Output is correct
24 Correct 510 ms 11368 KB Output is correct
25 Correct 1995 ms 13792 KB Output is correct
26 Correct 5356 ms 17768 KB Output is correct
27 Correct 4233 ms 18440 KB Output is correct
28 Correct 62 ms 4848 KB Output is correct
29 Correct 79 ms 4848 KB Output is correct
30 Correct 86 ms 4972 KB Output is correct
31 Correct 83 ms 4972 KB Output is correct
32 Correct 93 ms 5100 KB Output is correct
33 Correct 180 ms 5360 KB Output is correct
34 Correct 2869 ms 13844 KB Output is correct
35 Correct 5721 ms 16676 KB Output is correct
36 Correct 83 ms 5228 KB Output is correct
37 Correct 132 ms 5736 KB Output is correct
38 Correct 5175 ms 19860 KB Output is correct
39 Correct 6825 ms 20336 KB Output is correct
40 Correct 5748 ms 20192 KB Output is correct
41 Correct 6821 ms 20036 KB Output is correct
42 Correct 5534 ms 19844 KB Output is correct
43 Correct 466 ms 10856 KB Output is correct
44 Correct 4412 ms 16380 KB Output is correct
45 Correct 566 ms 11112 KB Output is correct
46 Correct 5212 ms 16596 KB Output is correct
47 Correct 85 ms 5228 KB Output is correct
48 Correct 90 ms 5388 KB Output is correct
49 Correct 121 ms 5736 KB Output is correct
50 Correct 381 ms 6764 KB Output is correct
51 Correct 622 ms 11624 KB Output is correct
52 Correct 1717 ms 13280 KB Output is correct
53 Correct 1485 ms 13024 KB Output is correct
54 Correct 2760 ms 15268 KB Output is correct
55 Correct 2280 ms 15200 KB Output is correct
56 Correct 3264 ms 16236 KB Output is correct
57 Correct 4203 ms 18020 KB Output is correct
58 Correct 5262 ms 19080 KB Output is correct
59 Correct 5151 ms 19772 KB Output is correct
60 Correct 6860 ms 20120 KB Output is correct