結果

問題 No.1054 Union add query
ユーザー T1610T1610
提出日時 2020-06-25 11:55:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 599 ms / 2,000 ms
コード長 2,321 bytes
コンパイル時間 3,299 ms
コンパイル使用メモリ 171,264 KB
実行使用メモリ 343,012 KB
最終ジャッジ日時 2023-09-16 21:53:22
合計ジャッジ時間 5,933 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 199 ms
71,464 KB
testcase_04 AC 599 ms
343,012 KB
testcase_05 AC 152 ms
37,348 KB
testcase_06 AC 225 ms
139,860 KB
testcase_07 AC 220 ms
139,928 KB
testcase_08 AC 224 ms
139,792 KB
testcase_09 AC 449 ms
342,760 KB
testcase_10 AC 402 ms
342,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0