Submission #858713

# Submission time Handle Problem Language Result Execution time Memory
858713 2023-10-09T05:10:51 Z NeroZein Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
601 ms 60984 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 1e5 + 5;

long long ans;
set<int> r[N];
int p[N], sz[N];
set<pair<int, int>> in[N], out[N]; 

int get(int x) {
  return p[x] = (p[x] == x ? x : get(p[x])); 
}

void merge(int x, int y) {
  x = get(x), y = get(y);
  if (x == y) {
    return;
  }
  auto f = in[x].lower_bound(make_pair(y, 0));
  while (f != in[x].end() && f->first == y) {
    ans -= sz[x];
    r[x].erase(f->second); 
    out[y].erase(make_pair(x, f->second)); 
    f = in[x].erase(f); 
  }
  f = in[y].lower_bound(make_pair(x, 0)); 
  while (f != in[y].end() && f->first == x) {
    ans -= sz[y];
    r[y].erase(f->second); 
    out[x].erase(make_pair(y, f->second)); 
    f = in[y].erase(f); 
  }
  if (in[x].size() + out[x].size() < in[y].size() + out[y].size()) {
    swap(x, y); 
  }
  ans -= (long long) in[x].size() * sz[x];
  ans -= (long long) in[y].size() * sz[y];
  ans -= (long long) sz[x] * (sz[x] - 1); 
  ans -= (long long) sz[y] * (sz[y] - 1); 
  vector<int> to_merge; 
  for (auto [i, j] : in[y]) {
    out[i].erase({y, j}); 
    out[i].emplace(x, j); 
    in[x].emplace(i, j); 
    r[x].insert(j); 
    f = out[x].lower_bound(make_pair(i, 0)); 
    if (f != out[x].end() && f->first == i) {
      to_merge.push_back(i); 
    }
  }
  p[y] = x;
  sz[x] += sz[y];
  ans += (long long) in[x].size() * sz[x];
  ans += (long long) sz[x] * (sz[x] - 1);
  for (auto [i, j] : out[y]) {
    in[i].erase(make_pair(y, j));
    in[i].emplace(x, j); 
    out[x].emplace(i, j); 
    f = in[x].lower_bound(make_pair(i, 0));
    if (f != in[x].end() && f->first == i) {
      to_merge.push_back(i); 
    }
  }
  for (int i : to_merge) {
    merge(x, i); 
  }
}

void add(int x, int y) {
  int px = get(x), py = get(y);
  if (px == py || r[py].count(x)) {
    return;
  }
  ans += sz[py];
  r[py].insert(x);
  in[py].emplace(px, x);
  out[px].emplace(py, x);
  auto f = in[px].lower_bound(make_pair(py, 0)); 
  if (f != in[px].end() && f->first == py) {
    merge(px, py); 
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  iota(p + 1, p + 1 + n, 1); 
  fill(sz + 1, sz + 1 + n, 1); 
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    add(x, y);
    cout << ans << '\n'; 
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15220 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15284 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15196 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15240 KB Output is correct
17 Correct 3 ms 15236 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 3 ms 15300 KB Output is correct
21 Correct 3 ms 15196 KB Output is correct
22 Correct 3 ms 15196 KB Output is correct
23 Correct 3 ms 15196 KB Output is correct
24 Correct 3 ms 15196 KB Output is correct
25 Correct 3 ms 15196 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 3 ms 15196 KB Output is correct
28 Correct 2 ms 15348 KB Output is correct
29 Correct 3 ms 15196 KB Output is correct
30 Correct 3 ms 15196 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 3 ms 15196 KB Output is correct
33 Correct 3 ms 15196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15220 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15284 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15196 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15240 KB Output is correct
17 Correct 3 ms 15236 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 3 ms 15300 KB Output is correct
21 Correct 3 ms 15196 KB Output is correct
22 Correct 3 ms 15196 KB Output is correct
23 Correct 3 ms 15196 KB Output is correct
24 Correct 3 ms 15196 KB Output is correct
25 Correct 3 ms 15196 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 3 ms 15196 KB Output is correct
28 Correct 2 ms 15348 KB Output is correct
29 Correct 3 ms 15196 KB Output is correct
30 Correct 3 ms 15196 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 3 ms 15196 KB Output is correct
33 Correct 3 ms 15196 KB Output is correct
34 Correct 4 ms 15196 KB Output is correct
35 Correct 62 ms 18856 KB Output is correct
36 Correct 83 ms 21332 KB Output is correct
37 Correct 90 ms 21588 KB Output is correct
38 Correct 84 ms 21072 KB Output is correct
39 Correct 4 ms 15196 KB Output is correct
40 Correct 5 ms 15452 KB Output is correct
41 Correct 4 ms 15452 KB Output is correct
42 Correct 4 ms 15196 KB Output is correct
43 Correct 6 ms 15452 KB Output is correct
44 Correct 5 ms 15452 KB Output is correct
45 Correct 4 ms 15196 KB Output is correct
46 Correct 4 ms 15196 KB Output is correct
47 Correct 5 ms 15452 KB Output is correct
48 Correct 5 ms 15452 KB Output is correct
49 Correct 11 ms 16220 KB Output is correct
50 Correct 86 ms 21532 KB Output is correct
51 Correct 8 ms 15704 KB Output is correct
52 Correct 81 ms 19776 KB Output is correct
53 Correct 11 ms 16216 KB Output is correct
54 Correct 75 ms 20768 KB Output is correct
55 Correct 7 ms 15708 KB Output is correct
56 Correct 7 ms 15708 KB Output is correct
57 Correct 7 ms 16120 KB Output is correct
58 Correct 7 ms 16220 KB Output is correct
59 Correct 4 ms 15196 KB Output is correct
60 Correct 64 ms 17696 KB Output is correct
61 Correct 7 ms 15448 KB Output is correct
62 Correct 78 ms 21072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15196 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 15220 KB Output is correct
5 Correct 3 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15284 KB Output is correct
8 Correct 3 ms 15196 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15196 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15240 KB Output is correct
17 Correct 3 ms 15236 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15196 KB Output is correct
20 Correct 3 ms 15300 KB Output is correct
21 Correct 3 ms 15196 KB Output is correct
22 Correct 3 ms 15196 KB Output is correct
23 Correct 3 ms 15196 KB Output is correct
24 Correct 3 ms 15196 KB Output is correct
25 Correct 3 ms 15196 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 3 ms 15196 KB Output is correct
28 Correct 2 ms 15348 KB Output is correct
29 Correct 3 ms 15196 KB Output is correct
30 Correct 3 ms 15196 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 3 ms 15196 KB Output is correct
33 Correct 3 ms 15196 KB Output is correct
34 Correct 4 ms 15196 KB Output is correct
35 Correct 62 ms 18856 KB Output is correct
36 Correct 83 ms 21332 KB Output is correct
37 Correct 90 ms 21588 KB Output is correct
38 Correct 84 ms 21072 KB Output is correct
39 Correct 4 ms 15196 KB Output is correct
40 Correct 5 ms 15452 KB Output is correct
41 Correct 4 ms 15452 KB Output is correct
42 Correct 4 ms 15196 KB Output is correct
43 Correct 6 ms 15452 KB Output is correct
44 Correct 5 ms 15452 KB Output is correct
45 Correct 4 ms 15196 KB Output is correct
46 Correct 4 ms 15196 KB Output is correct
47 Correct 5 ms 15452 KB Output is correct
48 Correct 5 ms 15452 KB Output is correct
49 Correct 11 ms 16220 KB Output is correct
50 Correct 86 ms 21532 KB Output is correct
51 Correct 8 ms 15704 KB Output is correct
52 Correct 81 ms 19776 KB Output is correct
53 Correct 11 ms 16216 KB Output is correct
54 Correct 75 ms 20768 KB Output is correct
55 Correct 7 ms 15708 KB Output is correct
56 Correct 7 ms 15708 KB Output is correct
57 Correct 7 ms 16120 KB Output is correct
58 Correct 7 ms 16220 KB Output is correct
59 Correct 4 ms 15196 KB Output is correct
60 Correct 64 ms 17696 KB Output is correct
61 Correct 7 ms 15448 KB Output is correct
62 Correct 78 ms 21072 KB Output is correct
63 Correct 400 ms 59564 KB Output is correct
64 Correct 392 ms 59800 KB Output is correct
65 Correct 391 ms 59472 KB Output is correct
66 Correct 83 ms 17408 KB Output is correct
67 Correct 134 ms 24144 KB Output is correct
68 Correct 78 ms 17492 KB Output is correct
69 Correct 213 ms 24400 KB Output is correct
70 Correct 81 ms 17300 KB Output is correct
71 Correct 95 ms 17304 KB Output is correct
72 Correct 163 ms 24816 KB Output is correct
73 Correct 195 ms 24660 KB Output is correct
74 Correct 601 ms 51280 KB Output is correct
75 Correct 359 ms 30268 KB Output is correct
76 Correct 455 ms 40468 KB Output is correct
77 Correct 418 ms 40436 KB Output is correct
78 Correct 132 ms 26196 KB Output is correct
79 Correct 211 ms 28292 KB Output is correct
80 Correct 141 ms 24912 KB Output is correct
81 Correct 234 ms 26296 KB Output is correct
82 Correct 437 ms 45400 KB Output is correct
83 Correct 451 ms 45808 KB Output is correct
84 Correct 429 ms 60984 KB Output is correct
85 Correct 486 ms 60944 KB Output is correct
86 Correct 81 ms 16468 KB Output is correct
87 Correct 119 ms 17536 KB Output is correct
88 Correct 213 ms 24688 KB Output is correct
89 Correct 448 ms 37800 KB Output is correct