結果

問題 No.1054 Union add query
ユーザー niuezniuez
提出日時 2020-05-16 21:30:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 2,724 bytes
コンパイル時間 2,664 ms
コンパイル使用メモリ 179,904 KB
実行使用メモリ 18,600 KB
最終ジャッジ日時 2023-10-24 02:23:21
合計ジャッジ時間 4,457 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,740 KB
testcase_01 AC 3 ms
9,740 KB
testcase_02 AC 3 ms
9,740 KB
testcase_03 AC 32 ms
18,320 KB
testcase_04 AC 33 ms
16,552 KB
testcase_05 AC 29 ms
18,124 KB
testcase_06 AC 29 ms
18,600 KB
testcase_07 AC 22 ms
18,600 KB
testcase_08 AC 26 ms
16,552 KB
testcase_09 AC 34 ms
16,552 KB
testcase_10 AC 14 ms
13,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target ("avx2")
#pragma GCC optimize ("Ofast")

#include <vector>
#include <tuple>
#include <iostream>

#include <unistd.h>

namespace niu {

  struct fastin {
    static const int bufsize = 1 << 24;
    char buf[bufsize];
    char* iter;
    fastin() {
      iter = buf;
      for(int t = 0, k; (k = read(STDIN_FILENO, buf + t, sizeof(buf)) - t) > 0; t += k);
    }
    void operator>>(int& num) {
      num = 0;
      bool neg = false;
      while(*iter < '+') iter++;
      if(*iter == '-') { neg = true; iter++; }
      else if(*iter == '+') iter++;
      while(*iter >= '0') num = 10 * num + *(iter++) - '0';
      if(neg) num = -num;
    } 
  } fin;
  struct fastout {
    static const int bufsize = 1 << 24;
    char buf[bufsize];
    char* iter;
    fastout() {
      iter = buf;
    }
    ~fastout() {
      for(int t = 0, k; (k = write(STDOUT_FILENO, buf + t, iter - buf - t)) > 0; t += k);
    }
    void operator<<(int num) {
      static char tmp[20];
      if(num == 0) {
        *(iter++) = '0';
      }
      if(num < 0) {
        *(iter++) = '-';
        num = -num;
      }
      int i = 0;
      while(num) {
        tmp[i++] = num % 10;
        num /= 10;
      }
      while(i--) {
        *(iter++) = tmp[i] + '0';
      }
    }
    void operator<<(char c) {
      *(iter++) = c;
    }
  } fout;
}

int par[505050];
int Sigma[505050];

struct union_find {
  union_find(int N) {
    std::fill(std::begin(par), std::end(par), -1);
  }
  int root(int x) {
    if(par[x] < 0) {
      return x;
    }
    int v = x;
    while(par[par[v]] >= 0) {
      v = par[v];
      Sigma[x] += Sigma[v];
    }
    par[x] = par[v];
    return par[v];
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) return;
    if(par[x] > par[y]) std::swap(x, y);
    par[x] += par[y];
    par[y] = x;
    Sigma[y] -= Sigma[x];
    return;
  }
  void add(int v, int a) {
    Sigma[root(v)] += a;
  }
  int value(int v) {
    int r = root(v);
    if(r == v) {
      return Sigma[v];
    }
    else {
      return Sigma[v] + Sigma[r];
    } 
  }
};

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

int main() {
  int N, Q;
  niu::fin >> N;
  niu::fin >> Q;
  union_find uf(N);
  while(Q--) {
    int t, a, b;
    niu::fin >> t;
    niu::fin >> a;
    niu::fin >> b;
    a--;
    if(t == 1) {
      b--;
      uf.unite(a, b);
    }
    else if(t == 2) {
      uf.add(a, b);
    }
    else {
      niu::fout << uf.value(a);
      niu::fout << '\n';
    }
    /*
    cout << Q << endl;
    rep(i,0,N) {
      cout << i << " = " << uf.Sigma[i] << " " << uf.par[i] << endl;
    }*/
  }
}
0