Submission #59857183


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 5e5+5;

int n, q;

int par[N], sz[N], col[N], mn[N], mx[N], ans[N];

int find(int u) {
  return par[u] = par[u] == u? u: find(par[u]);
}

void unite(int u, int v) {
  u = find(u);
  v = find(v);
  assert(col[u] == col[v]);
  if (u == v) return ;
  if (sz[u] < sz[v]) swap(u, v);
  par[v] = u;
  sz[u] += sz[v];
  mn[u] = min(mn[u], mn[v]);
  mx[u] = max(mx[u], mx[v]);
}

void assign(int u, int c) {
  int p = find(u);
  ans[col[p]] -= sz[p];
  col[p] = c;
  ans[col[p]] += sz[p];

  int v = mn[p] - 1;
  if (v >= 0 and col[find(v)] == col[find(u)]) {
    unite(find(v), find(u));
  }
  int w = mx[p] + 1;
  if (w < n and col[find(w)] == col[find(u)]) {
    unite(find(w), find(u));
  }
}

void solve () {
  cin >> n >> q;
  iota(par, par + n, 0);
  iota(col, col + n, 0);
  iota(mn, mn + n, 0);
  iota(mx, mx + n, 0);

  fill(sz, sz + n, 1);
  fill(ans, ans + n, 1);

  while (q--) {
    int t; cin >> t;
    if (t == 1) {
      int i, c; cin >> i >> c; i--; c--;
      assign(i, c);
    }
    else {
      int c; cin >> c; c--;
      cout << ans[c] << "\n";
    }
  }
}

int main(){
  ios::sync_with_stdio(0), cin.tie(0);
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
  return 0;
}

Submission Info

Submission Time
Task E - 1D Bucket Tool
User fahimcp495
Language C++ 20 (gcc 12.2)
Score 450
Code Size 1370 Byte
Status AC
Exec Time 73 ms
Memory 15228 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 1
AC × 18
Set Name Test Cases
Sample sample_01.txt
All hand.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, sample_01.txt
Case Name Status Exec Time Memory
hand.txt AC 1 ms 3524 KiB
random_01.txt AC 20 ms 3516 KiB
random_02.txt AC 22 ms 3400 KiB
random_03.txt AC 22 ms 3400 KiB
random_04.txt AC 23 ms 3500 KiB
random_05.txt AC 28 ms 15148 KiB
random_06.txt AC 30 ms 15208 KiB
random_07.txt AC 27 ms 15188 KiB
random_08.txt AC 64 ms 15092 KiB
random_09.txt AC 71 ms 15128 KiB
random_10.txt AC 73 ms 15124 KiB
random_11.txt AC 71 ms 15212 KiB
random_12.txt AC 73 ms 15228 KiB
random_13.txt AC 28 ms 4704 KiB
random_14.txt AC 28 ms 4688 KiB
random_15.txt AC 28 ms 4700 KiB
random_16.txt AC 29 ms 4692 KiB
sample_01.txt AC 1 ms 3512 KiB