結果
| 問題 | No.1054 Union add query |
| コンテスト | |
| ユーザー |
C
|
| 提出日時 | 2026-10-07 14:52:13 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 113 ms / 2,000 ms |
| + 852µs | |
| コード長 | 2,095 bytes |
| 記録 | |
| コンパイル時間 | 6,838 ms |
| コンパイル使用メモリ | 388,084 KB |
| 実行使用メモリ | 13,112 KB |
| 最終ジャッジ日時 | 2026-10-07 14:52:26 |
| 合計ジャッジ時間 | 12,291 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
#include <boost/polygon/point_data.hpp>
#include <boost/polygon/voronoi.hpp>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(x) begin(x),end(x)
#define sz(x) (int)(x).size()
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vi>;
using pll=pair<ll,ll>;
// using mint=modint;
// using BoostPoint=boost::polygon::point_data<int>;
// using Voronoi=boost::polygon::voronoi_diagram<double>;
#line 2 "data-structure/union-find-with-potential.hpp"
template <class T>
struct UnionFindWithPotential {
vector<int> dat;
vector<T> pot;
UnionFindWithPotential(int N) : dat(N, -1), pot(N, T()) {}
int root(int x) {
if (dat[x] < 0) return x;
int r = root(dat[x]);
pot[x] += pot[dat[x]];
return dat[x] = r;
}
// return P(x) - P(root(x))
T potential(int x) {
root(x);
return pot[x];
}
bool same(int x, int y) { return root(x) == root(y); }
// return P(x) - P(y)
T diff(int x, int y) {
return potential(x) - potential(y);
}
// s.t. P(x) = P(y) + p
// return Satisfiablility
bool merge(int x, int y, T p) {
p += potential(y) - potential(x);
x = root(x), y = root(y);
if (x == y) return p == T();
if (dat[x] < dat[y]) swap(x, y), p = -p;
dat[y] += dat[x];
dat[x] = y;
pot[x] = p;
return true;
}
// return size of CC including x
int size(int x) { return -dat[root(x)]; }
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N,Q;
cin>>N>>Q;
vll add(N,0);
UnionFindWithPotential<ll>uf(N);
while(Q--){
ll t,a,b;
cin>>t>>a>>b;
--a;
if(t==1){
--b;
ll x=uf.root(a),y=uf.root(b);
if(x==y)continue;
uf.merge(x,y,add[x]-add[y]);
}else if(t==2){
add[uf.root(a)]+=b;
}else{
cout<<add[uf.root(a)]+uf.potential(a)<<'\n';
}
}
}
C