結果
問題 | No.1054 Union add query |
ユーザー | penguinshunya |
提出日時 | 2020-05-15 22:55:27 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,379 bytes |
コンパイル時間 | 3,153 ms |
コンパイル使用メモリ | 222,268 KB |
実行使用メモリ | 27,768 KB |
最終ジャッジ日時 | 2024-09-19 12:18:16 |
合計ジャッジ時間 | 8,131 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 620 ms
19,428 KB |
testcase_07 | WA | - |
testcase_08 | AC | 556 ms
19,424 KB |
testcase_09 | AC | 872 ms
25,088 KB |
testcase_10 | AC | 397 ms
25,088 KB |
ソースコード
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repc(i, n) for (int i = 0; i <= int(n); i++) #define rrepc(i, n) for (int i = int(n); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define repic(i, a, b) for (int i = int(a); i <= int(b); i++) #define all(a) (a).begin(), (a).end() #define bit32(x) (1 << (x)) #define bit64(x) (1ll << (x)) #define sz(v) ((int) v.size()) using namespace std; using i64 = long long; using f80 = long double; using vi32 = vector<int>; using vi64 = vector<i64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << endl; exit(0); } inline void no() { cout << "No" << endl; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { x = max(x, y); } template <typename T> inline void amin(T &x, T y) { x = min(x, y); } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> istream& operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } struct UnionFind { vector<int> data; UnionFind(int n) { data.assign(n, -1); } int root(int k) { if (data[k] < 0) return k; return data[k] = root(data[k]); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } bool same(int x, int y) { return root(x) == root(y); } int size(int k) { return -data[root(k)]; } }; template <typename T, typename S> struct LazySegmentTree { vector<T> v; vector<S> z; function<T(T, T)> f; function<T(T, S, int)> g; function<S(S, S)> h; T e; S d; int n; LazySegmentTree( int size, function<T(T, T)> f, function<T(T, S, int)> g, function<S(S, S)> h, T e, S d ) : f(f), g(g), h(h), e(e), d(d) { n = 1; while (n < size) n <<= 1; v.resize(n * 2, e); z.resize(n * 2, d); } void set(int k, T x) { v[k + n] = x; } void build() { for (int i = n - 1; i >= 0; i--) { v[i] = f(v[i * 2 + 0], v[i * 2 + 1]); } } void propagate(int k, int l, int r) { v[k] = g(v[k], z[k], r - l); if (k < n) { z[k * 2 + 0] = h(z[k * 2 + 0], z[k]); z[k * 2 + 1] = h(z[k * 2 + 1], z[k]); } z[k] = d; } T update(int a, int b, S x, int k = 1, int l = 0, int r = -1) { if (r == -1) r = n; propagate(k, l, r); if (b <= l || r <= a) return v[k]; if (a <= l && r <= b) { z[k] = x; propagate(k, l, r); return v[k]; } return v[k] = f( update(a, b, x, k * 2 + 0, l, (l + r) / 2), update(a, b, x, k * 2 + 1, (l + r) / 2, r) ); } T query(int a, int b, int k = 1, int l = 0, int r = -1) { if (r == -1) r = n; propagate(k, l, r); if (b <= l || r <= a) return e; if (a <= l && r <= b) return v[k]; return f( query(a, b, k * 2 + 0, l, (l + r) / 2), query(a, b, k * 2 + 1, (l + r) / 2, r) ); } }; void solve() { int n, q; cin >> n >> q; vi32 t(q), a(q), b(q); rep(i, q) { cin >> t[i] >> a[i] >> b[i]; a[i]--, b[i]--; } using T = tuple<int, int, int, int>; vector<T> history; UnionFind uf(n); rep(i, q) if (t[i] == 1) { history.emplace_back(uf.root(a[i]), uf.root(b[i]), uf.size(a[i]), uf.size(b[i])); uf.unite(a[i], b[i]); } reverse(all(history)); int last = 0; vi32 indices(n, -1); rep(i, sz(history)) { int u, v, usize, vsize; tie(u, v, usize, vsize) = history[i]; if (indices[u] == -1) { swap(u, v); swap(usize, vsize); } if (indices[u] == -1) { indices[u] = last; indices[v] = last + usize; last += usize + vsize; } else if (indices[v] == -1) { indices[v] = indices[u] + usize; } } rep(i, n) if (indices[i] == -1) { indices[i] = last++; } LazySegmentTree<int, int> lst(n, [](int a, int b) { return a + b; }, [](int a, int b, int k) { return a + b * k; }, [](int a, int b) { return a + b; }, 0, 0); UnionFind uf2(n); vi32 mins(n); rep(i, n) { mins[i] = indices[i]; } rep(i, q) { if (t[i] == 1) { int mi = min(mins[uf2.root(a[i])], mins[uf2.root(b[i])]); mins[a[i]] = mins[b[i]] = mi; uf2.unite(a[i], b[i]); } else if (t[i] == 2) { int l = mins[uf2.root(a[i])]; int r = l + uf2.size(a[i]); lst.update(l, r, b[i] + 1); } else { int l = indices[a[i]]; int ans = lst.query(l, l + 1); cout << ans << endl; } } }