結果

問題 No.1054 Union add query
ユーザー SHIJOUSHIJOU
提出日時 2020-08-20 03:52:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,403 bytes
コンパイル時間 2,384 ms
コンパイル使用メモリ 200,516 KB
実行使用メモリ 23,456 KB
最終ジャッジ日時 2024-04-20 19:10:16
合計ジャッジ時間 6,144 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,296 KB
testcase_01 AC 6 ms
7,296 KB
testcase_02 AC 6 ms
7,296 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 145 ms
15,704 KB
testcase_07 AC 122 ms
15,704 KB
testcase_08 AC 147 ms
15,704 KB
testcase_09 AC 222 ms
23,456 KB
testcase_10 AC 122 ms
21,660 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 now = 0;
	int kk = -1;
	rep(i, n) {
		if(now == -1) {
			kk = i;
			break;
		}
		place[now] = i;
		now = nex[now];
	}
	if(kk != -1) {
		for(int i = 1; i < n; i++) {
			if(!place[i]) place[i] = kk++;
		}
	}

	//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