結果

問題 No.1054 Union add query
ユーザー SHIJOUSHIJOU
提出日時 2020-08-20 03:58:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 317 ms / 2,000 ms
コード長 3,326 bytes
コンパイル時間 2,798 ms
コンパイル使用メモリ 199,948 KB
実行使用メモリ 23,456 KB
最終ジャッジ日時 2024-04-20 19:18:09
合計ジャッジ時間 5,657 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,168 KB
testcase_01 AC 6 ms
7,296 KB
testcase_02 AC 6 ms
7,040 KB
testcase_03 AC 181 ms
14,016 KB
testcase_04 AC 317 ms
23,456 KB
testcase_05 AC 148 ms
12,868 KB
testcase_06 AC 159 ms
15,576 KB
testcase_07 AC 129 ms
15,576 KB
testcase_08 AC 153 ms
15,576 KB
testcase_09 AC 260 ms
23,328 KB
testcase_10 AC 136 ms
21,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

struct UnionFind {
	vector<int> par; // 親を指すvector,-par[親]は木のサイズ
	UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり
	inline void init() {
		rep(i, par.size()) par[i] = -1;
	}
	int root(int x) { // 親をたどる&データの整理
		if(par[x] < 0) return x;
		return par[x] = root(par[x]);
	}
	bool unite(int x, int y) { // データの結合
		x = root(x);
		y = root(y);
		if(x == y) return false;
		//if(par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}
	bool same(int x, int y) {return root(x) == root(y);} // 所属判定
	int size(int x) {return -par[root(x)];} // 木のサイズ
};

const int N = 5e5+10;

//head

int n, q;
string t;
int a[N], b[N];
UnionFind uft(N);
int last[N], front[N];
int nex[N];
int place[N];
P info[N];
int bit[N];
void add(int i, int x) {
	while(i <= n) {
		bit[i] += x;
		i += i&-i;
	}
}
int sum(int i) {
	int res = 0;
	while(i) {
		res += bit[i];
		i &= i-1;
	}
	return res;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	memset(nex, -1, sizeof(nex));
	cin >> n >> q;
	rep(i, n) {
		info[i] = P(i+1, i+2);
		last[i] = i;
		front[i] = i;
	}
	rep(i, q) {
		char u;
		cin >> u >> a[i] >> b[i];
		a[i]--; b[i]--;
		t += u;
		if(u == '1' and !uft.same(a[i], b[i])) {
			a[i] = uft.root(a[i]);
			b[i] = uft.root(b[i]);
			if(a[i] > b[i]) swap(a[i], b[i]);
			nex[last[uft.root(a[i])]] = front[uft.root(b[i])];
			last[uft.root(a[i])] = last[uft.root(b[i])];
			front[uft.root(b[i])] = front[uft.root(a[i])];
			uft.unite(a[i], b[i]);
		}
	}


	int cnt = 0;
	rep(i, n) if(i == uft.root(i)) {
		int now = i;
		while(now != -1) {
			place[now] = cnt++;
			now = nex[now];
		}
	}

	//rep(i, n) cout << place[i] << endl;


	uft.init();
	rep(i, q) {
		if(t[i] == '1') {
			a[i] = place[a[i]];
			b[i] = place[b[i]];
			if(uft.same(a[i], b[i])) continue;
			P &u = info[uft.root(a[i])], &v = info[uft.root(b[i])];
			u.first = v.first = min(u.first, v.first);
			u.second = v.second = max(u.second, v.second);
			uft.unite(a[i], b[i]);
		}
		else if(t[i] == '2') {
			a[i] = uft.root(place[a[i]]);
			b[i]++;
			add(info[a[i]].first, b[i]);
			add(info[a[i]].second, -b[i]);
		}
		else {
			a[i] = place[a[i]];
			cout << sum(a[i]+1) << '\n';
		}

		//cout << i << endl;
		//rep(i, n) cout << sum(i+1) << (i == n-1?'\n':' ');
	}
}
0