結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
T1610
|
| 提出日時 | 2020-06-25 11:55:19 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 570 ms / 2,000 ms |
| コード長 | 2,321 bytes |
| コンパイル時間 | 1,734 ms |
| コンパイル使用メモリ | 174,676 KB |
| 実行使用メモリ | 343,148 KB |
| 最終ジャッジ日時 | 2024-07-03 20:51:57 |
| 合計ジャッジ時間 | 5,729 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}
#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " "
#define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x)
#define dumpL(x)
#define LINE
#define LINEL
#define dumpV(v)
#define dumpVL(v)
#define STOP assert(false)
#endif
#define mp make_pair
namespace std {
template<class S, class T>
ostream &operator <<(ostream& out, const pair<S, T>& a) {
out << '(' << a.fi << ", " << a.se << ')';
return out;
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
vi par(n, -1);
iota(all(par), 0);
vector<queue<int>> ch(n);
vi v(n);
rep(_, q) {
int t, a, b;
cin >> t >> a >> b;
--a;
if(t == 1) {
--b;
a = par[a];
b = par[b];
if(a == b) continue;
if(ch[a].size() > ch[b].size()) swap(a, b);
while(ch[a].size()) {
int c = ch[a].front(); ch[a].pop();
int d = v[a] - v[c];
ch[b].emplace(c);
par[c] = b;
v[c] = v[b] - d;
}
ch[b].emplace(a);
par[a] = b;
v[a] = v[b] - v[a];
} else if(t == 2) {
int p = par[a];
v[p] += b;
} else {
if(par[a] == a) cout << v[a] << '\n';
else cout << v[par[a]] - v[a] << '\n';
}
}
return 0;
}
T1610