結果

問題 No.1054 Union add query
ユーザー uzzyuzzy
提出日時 2020-05-15 22:12:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,923 bytes
コンパイル時間 2,437 ms
コンパイル使用メモリ 180,480 KB
実行使用メモリ 7,640 KB
最終ジャッジ日時 2023-10-19 14:40:53
合計ジャッジ時間 4,126 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,656 KB
testcase_01 AC 3 ms
5,656 KB
testcase_02 AC 2 ms
5,656 KB
testcase_03 AC 41 ms
6,176 KB
testcase_04 AC 54 ms
7,640 KB
testcase_05 AC 39 ms
5,984 KB
testcase_06 AC 33 ms
6,472 KB
testcase_07 AC 26 ms
6,472 KB
testcase_08 AC 27 ms
6,472 KB
testcase_09 AC 62 ms
7,640 KB
testcase_10 AC 15 ms
6,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	int pn = 1;
	if (*ci == '-') {
		pn = -pn;
		ci++;
	}
	ll tmp = *(ll*)ci;
	int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0;
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += (72 - dig >> 3);
	return pn * tmp;
}

int P[500001], kotae[500001];

int Find(int A) {
	if (P[A] < 0) return A;
	return Find(P[A]);
}
int query(int A) {
	if (P[A] < 0) return kotae[A];
	return kotae[A] + query(P[A]);
}

bool Unite(int A, int B) {
	int a = Find(A);
	int b = Find(B);
	if (a == b) return false;
	if (P[a] > P[b]) swap(a, b);
	if (P[a] == P[b]) P[a]--;
	kotae[b] -= kotae[a];
	P[b] = a;
	return true;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N = getint(), Q = getint();
	rep1(i, N) P[i] = -1;
	rep(i, Q) {
		int t = getint(), a = getint(), b = getint();

		if (t == 1) {
			Unite(a, b);
		}
		else if (t == 2) {
			kotae[Find(a)] += b;
		}
		else {
			co(query(a));
		}
	}



	Would you please return 0;
}
0