結果

問題 No.1054 Union add query
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-14 18:02:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 4,334 bytes
コンパイル時間 1,185 ms
コンパイル使用メモリ 131,344 KB
実行使用メモリ 38,912 KB
最終ジャッジ日時 2024-09-16 12:11:16
合計ジャッジ時間 5,117 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
18,816 KB
testcase_01 AC 7 ms
18,688 KB
testcase_02 AC 8 ms
18,688 KB
testcase_03 AC 163 ms
23,552 KB
testcase_04 AC 260 ms
38,912 KB
testcase_05 AC 146 ms
21,248 KB
testcase_06 AC 152 ms
27,368 KB
testcase_07 AC 142 ms
27,368 KB
testcase_08 AC 140 ms
27,236 KB
testcase_09 AC 185 ms
38,400 KB
testcase_10 AC 136 ms
36,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
using namespace std;
template<int m> struct modint
{
	private:
	unsigned int value;
	static constexpr int mod() {return m;}

	public:
	constexpr modint(const long long x = 0) noexcept
	{
		long long y = x;
		if(y < 0 || y >= mod())
		{
			y %= mod();
			if(y < 0) y += mod();
		}
		value = (unsigned int)y;
	}
	constexpr unsigned int val() noexcept {return value;}
	constexpr modint &operator+=(const modint &other) noexcept
	{
		value += other.value;
		if(value >= mod()) value -= mod();
		return *this;
	}
	constexpr modint &operator-=(const modint &other) noexcept
	{
		unsigned int x = value;
		if(x < other.value) x += mod();
		x -= other.value;
		value = x;
		return *this;
	}
	constexpr modint &operator*=(const modint &other) noexcept
	{
		unsigned long long x = value;
		x *= other.value;
		value = (unsigned int) (x % mod());
		return *this;
	}
	constexpr modint &operator/=(const modint &other) noexcept
	{
		return *this *= other.inverse();
	}
	constexpr modint inverse() const noexcept
	{
		assert(value);
		long long a = value,b = mod(),x = 1,y = 0;
		while(b)
		{
			long long q = a/b;
			a -= q*b; swap(a,b);
			x -= q*y; swap(x,y);
		}
		return modint(x);
	}
	constexpr modint power(long long N) const noexcept
	{
		assert(N >= 0);
		modint p = *this,ret = 1;
		while(N)
		{
			if(N & 1) ret *= p;
			p *= p;
			N >>= 1;
		}
		return ret;
	}
	constexpr modint operator+() {return *this;}
	constexpr modint operator-() {return modint() - *this;}
	constexpr modint &operator++(int) noexcept {return *this += 1;}
	constexpr modint &operator--(int) noexcept {return *this -= 1;}
	friend modint operator+(const modint& lhs, const modint& rhs) {return modint(lhs) += rhs;}
	friend modint operator-(const modint& lhs, const modint& rhs) {return modint(lhs) -= rhs;}
	friend modint operator*(const modint& lhs, const modint& rhs) {return modint(lhs) *= rhs;}
	friend modint operator/(const modint& lhs, const modint& rhs) {return modint(lhs) /= rhs;}
	friend ostream &operator<<(ostream &os,const modint &x) {return os << x.value;}
};
using mint = modint<998244353>;
//using mint = modint<1000000007>;

#include<algorithm>
#include<vector>
#include<cassert>
using namespace std;
struct UnionFind
{
	private:
	int n;
	vector<int> par,siz;

	public:
	UnionFind(int n) :n(n),par(n,-1),siz(n,1) {}

	int root(int u)
	{
		assert(0 <= u && u < n);
		return (par[u] < 0 ? u:par[u] = root(par[u]));
	}

	bool same(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		return root(u) == root(v);
	}

	bool unite(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		u = root(u),v = root(v);
		if(u == v) return false;

		if(siz[u] < siz[v]) swap(u,v);

		siz[u] += siz[v];
		par[v] = u;

		return true;
	}

	int size(int u)
	{
		assert(0 <= u && u < n);
		return siz[root(u)];
	}

	vector<vector<int>> components()
	{
		vector<vector<int>> ret(n);
		for(int u = 0;u < n;u++) ret[root(u)].push_back(u);

		ret.erase(remove_if(ret.begin(),ret.end(),[](vector<int> v) { return v.empty();}),ret.end());

		return ret;
	}
};

int N,Q;
int val[5 << 17],P[5 << 17];
vector<int> V[5 << 17];
void solve()
{
	cin >> N >> Q;
	for(int i = 0;i < N;i++) P[i] = -1,V[i].push_back(i);
	auto root = [&](auto self,int u) -> int
	{
		return P[u] < 0 ? u:P[u] = self(self,P[u]);
	};
	for(;Q--;)
	{
		int op;
		cin >> op;
		if(op == 1)
		{
			int u,v;
			cin >> u >> v;
			u--; v--;
			u = root(root,u),v = root(root,v);
			if(u == v) continue;
			if(V[u].size() > V[v].size()) swap(u,v);
			int prev = val[u];
			for(int x:V[u])
			{
				if(x != u) val[x] += prev-val[v];
				else val[x] -= val[v];
				V[v].push_back(x);
			}
			V[u].clear();
			P[u] = v;
		}
		else if(op == 2)
		{
			int a,b;
			cin >> a >> b;
			a--;
			val[root(root,a)] += b;
		}
		else
		{
			int u,v;
			cin >> u >> v;
			u--;
			int res = val[u];
			int r = root(root,u);
			if(u == r);
			else res += val[r];
			cout << res << "\n";
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
}
0