結果

問題 No.1054 Union add query
ユーザー KoseTKoseT
提出日時 2020-05-15 22:54:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,011 bytes
コンパイル時間 747 ms
コンパイル使用メモリ 83,900 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-19 16:09:17
合計ジャッジ時間 4,718 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,980 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <stdio.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;

constexpr ll LLINF {1001002003004005006};//ll = 9*LLINF
constexpr int INTINF {1000000000};//int = 2*INTINF

#define  rep(i,n)   for(int i = 0; i < (n); ++i)
#define rrep(i,n)   for(int i = 1; i <= (n); ++i)
#define drep(i,n)   for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for(int i = s; i < t;  ++i)

template<typename T>
void maxs(T& x, T&& y) {
  x=std::max(x,y);
}
template<typename T>
void maxs(T& x, T& y) {
  x=std::max(x,y);
}

template<typename T>
void mins(T& x, T&& y) {
  x=std::min(x,y);
}
template<typename T>
void mins(T& x, T& y) {
  x=std::min(x,y);
}

struct Query{
  int t, a, b;
};

struct Node {
  ull number;
  unsigned parent; // parent == 0 => not exist
};
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, q_cnt;
  scanf("%d%d", &n, &q_cnt);
  vector<Query> X(q_cnt);
  rep(i, q_cnt) {
    scanf("%d%d%d", &X[i].t, &X[i].a, &X[i].b);
  }

  vector<Node> Party(n+1);
  rep(i, n+1) {
    Party[i].number = 0;
    Party[i].parent = 0;
  }
  int Parent_size {n};
  rep(i, q_cnt) {
    if (X[i].t == 1) {
      Node tmp;
      tmp.number = 0; tmp.parent = 0;
      Party.push_back(tmp);
      ++Parent_size;
      int index {X[i].a};
      while (Party[index].parent!=0) index = Party[index].parent;
      Party[index].parent = Parent_size;
      index = X[i].b;
      while (Party[index].parent!=0) index = Party[index].parent;
      Party[index].parent = Parent_size;
    } else if (X[i].t == 2) {
      int index {X[i].a};
      while (Party[index].parent!=0) index = Party[index].parent;
      Party[index].number += X[i].b;
    } else {//X[i].t == 3
      int ans {};
      int index {X[i].a};
      do {
        ans  += Party[index].number;
        index = Party[index].parent;
      } while (index != 0);
      cout << ans << '\n';
    }
  }
}
0