結果

問題 No.1054 Union add query
ユーザー i_mrhji_mrhj
提出日時 2020-05-15 23:15:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 1,708 bytes
コンパイル時間 773 ms
コンパイル使用メモリ 96,064 KB
実行使用メモリ 11,628 KB
最終ジャッジ日時 2023-10-19 17:01:39
合計ジャッジ時間 3,429 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,916 KB
testcase_01 AC 2 ms
7,916 KB
testcase_02 AC 2 ms
5,868 KB
testcase_03 AC 167 ms
7,044 KB
testcase_04 AC 233 ms
11,628 KB
testcase_05 AC 159 ms
6,456 KB
testcase_06 AC 153 ms
6,652 KB
testcase_07 AC 134 ms
6,652 KB
testcase_08 AC 143 ms
6,652 KB
testcase_09 AC 198 ms
10,100 KB
testcase_10 AC 107 ms
8,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <climits>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <queue>
#include <cstring>
#include <set>
#include <map>
#include <complex>

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
long long MOD = 1000000007;
long long INF = 1000000000000000; //10^15
typedef long long ll;
typedef unsigned long long ull;


ll powMod(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll t = powMod(x, n/2, mod);
  t = t * t % mod;
  if (n & 1) return t * x % mod;
  return t;
}

ll gcd(ll a, ll b) {
  if (a == 0 || b == 0) return a + b;
  if (b > a) return gcd(b, a);
  return gcd(b, a % b);
}

int parent[500100], Rank[500100];
int score[500100], ps[500100];



int get_parent(int i, int *parent) {
  while (i != parent[i]) i = parent[i];
  return i;
}

void Union(int i, int j, int *parent, int *rank) {
  i = get_parent(i, parent);
  j = get_parent(j, parent);
  if (i == j) return;
  if (rank[i] < rank[j]) swap(i, j);
  parent[j] = i;
  ps[j] = score[i];
  rank[i] = max(rank[i], rank[j] + 1);
  return;
}

int get_score(int i, int *parent) {
  int res = score[i];
  while (i != parent[i]) {
    res += score[parent[i]] - ps[i];
    i = parent[i];
  }
  return res;
}
  


int main(void) {

  
  int n, q, t, a, b;
  scanf("%d %d", &n, &q);

  rep(i, n) parent[i] = i;

  
  rep(i, q) {
    scanf("%d %d %d", &t, &a, &b);
    if (t == 1) Union(a-1, b-1, parent, Rank);
    if (t == 2) score[get_parent(a-1, parent)] += b;
    if (t == 3) printf("%d\n", get_score(a-1, parent));
  }
  return 0;

}
  
0