結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
tsutaj
|
| 提出日時 | 2020-05-15 22:25:32 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 691 ms / 2,000 ms |
| コード長 | 8,028 bytes |
| コンパイル時間 | 1,727 ms |
| コンパイル使用メモリ | 130,048 KB |
| 実行使用メモリ | 108,128 KB |
| 最終ジャッジ日時 | 2024-09-19 11:14:13 |
| 合計ジャッジ時間 | 6,931 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
/**
* @brief Union-Find
* @docs ./docs/union_find.md
*/
#include <algorithm>
#include <vector>
struct UnionFind {
private:
const int n;
int size_;
vector<int> uf;
public:
// 初期化 UnionFind uni(n) のように宣言すれば良い
UnionFind(int _n) : n(_n), size_(_n), uf(_n, -1) {}
// find (木の根を求める)
int find(int x) {return (uf[x] < 0) ? x : uf[x] = find(uf[x]);}
// x と y が同じ集合に属するかどうか
bool same(int x, int y) {return find(x) == find(y);}
// x が属する集合の要素数
int size(int x) {return -uf[find(x)];}
// 集合はいくつあるか
int size() {return size_;}
// x と y の属する集合を併合
bool unite(int x, int y) {
x = find(x); y = find(y);
if(x == y) return false;
size_--;
if(-uf[x] < -uf[y]) swap(x, y);
uf[x] += uf[y]; uf[y] = x;
return true;
}
void reset() {
size_ = n;
fill(uf.begin(), uf.end(), -1);
}
};
// @category セグメント木 (Segment Tree)
// @title 遅延伝播セグメント木 (Lazy Segment Tree)
template <typename MonoidType, typename OperatorType>
struct LazySegmentTree {
using MMtoM = function< MonoidType(MonoidType, MonoidType) >;
using OOtoO = function< OperatorType(OperatorType, OperatorType) >;
using MOtoM = function< MonoidType(MonoidType, OperatorType) >;
using OItoO = function< OperatorType(OperatorType, int) >;
// node, lazy, update flag (for lazy), identity element
int n;
vector<MonoidType> node;
vector<OperatorType> lazy;
vector<bool> need_update;
MonoidType E0;
OperatorType E1;
// update / combine / lazy / accumulate function
MOtoM upd_f;
MMtoM cmb_f;
OOtoO lzy_f;
OItoO acc_f;
void build(int m, vector<MonoidType> v = vector<MonoidType>()) {
if(v != vector<MonoidType>()) m = v.size();
n = 1; while(n < m) n *= 2;
node = vector<MonoidType>(2*n-1, E0);
lazy = vector<OperatorType>(2*n-1, E1);
need_update = vector<bool>(2*n-1, false);
if(v != vector<MonoidType>()) {
for(int i=0; i<m; i++) {
node[n-1+i] = v[i];
}
for(int i=n-2; i>=0; i--) {
node[i] = cmb_f(node[2*i+1], node[2*i+2]);
}
}
}
// initialize
LazySegmentTree() {}
LazySegmentTree(int n_, MonoidType E0_, OperatorType E1_,
MOtoM upd_f_, MMtoM cmb_f_, OOtoO lzy_f_, OItoO acc_f_,
vector<MonoidType> v = vector<MonoidType>()) :
E0(E0_), E1(E1_),
upd_f(upd_f_), cmb_f(cmb_f_), lzy_f(lzy_f_), acc_f(acc_f_) {
build(n_, v);
}
void eval(int k, int l, int r) {
if(!need_update[k]) return;
node[k] = upd_f(node[k], acc_f(lazy[k], r - l));
if(r - l > 1) {
lazy[2*k+1] = lzy_f(lazy[2*k+1], lazy[k]);
lazy[2*k+2] = lzy_f(lazy[2*k+2], lazy[k]);
need_update[2*k+1] = need_update[2*k+2] = true;
}
lazy[k] = E1;
need_update[k] = false;
}
void update(int a, int b, OperatorType x, int l, int r, int k) {
eval(k, l, r);
if(b <= l or r <= a) return;
if(a <= l and r <= b) {
lazy[k] = lzy_f(lazy[k], x);
need_update[k] = true;
eval(k, l, r);
}
else {
int mid = (l + r) / 2;
update(a, b, x, l, mid, 2*k+1);
update(a, b, x, mid, r, 2*k+2);
node[k] = cmb_f(node[2*k+1], node[2*k+2]);
}
}
MonoidType query(int a, int b, int l, int r, int k) {
if(b <= l or r <= a) return E0;
eval(k, l, r);
if(a <= l and r <= b) return node[k];
int mid = (l + r) / 2;
MonoidType vl = query(a, b, l, mid, 2*k+1);
MonoidType vr = query(a, b, mid, r, 2*k+2);
return cmb_f(vl, vr);
}
// update [a, b)-th element (applied value, x)
void update(int a, int b, OperatorType x) {
update(a, b, x, 0, n, 0);
}
// range query for [a, b)
MonoidType query(int a, int b) {
return query(a, b, 0, n, 0);
}
void dump() {
fprintf(stderr, "[lazy]\n");
for(int i=0; i<2*n-1; i++) {
if(i == n-1) fprintf(stderr, "xxx ");
if(lazy[i] == E1) fprintf(stderr, " E ");
else fprintf(stderr, "%3d ", lazy[i]);
}
fprintf(stderr, "\n");
fprintf(stderr, "[node]\n");
for(int i=0; i<2*n-1; i++) {
if(i == n-1) fprintf(stderr, "xxx ");
if(node[i] == E0) fprintf(stderr, " E ");
else fprintf(stderr, "%3d ", node[i]);
}
fprintf(stderr, "\n");
}
};
int main() {
int N, Q; scanf("%d%d", &N, &Q);
const int V = 2*N;
vector< tuple<int, int, int> > queries;
vector<int> rec(V);
iota(rec.begin(), rec.end(), 0);
UnionFind uf(V);
int id = N;
vector< vector<int> > G(V);
vector<int> head(V);
fill(head.begin(), head.begin() + N, true);
while(Q--) {
int t, a, b; scanf("%d%d%d", &t, &a, &b);
queries.emplace_back(t, a, b);
if(t == 1) {
a--; b--;
int u = uf.find(a), v = uf.find(b);
int x = rec[u], y = rec[v];
if(!uf.same(u, v)) {
head[id] = true;
head[x] = head[y] = false;
G[id].emplace_back(x);
G[id].emplace_back(y);
G[x].emplace_back(id);
G[y].emplace_back(id);
uf.unite(u, v);
u = uf.find(a);
rec[u] = id++;
}
}
}
for(int i=0; i<id; i++) {
if(!head[i]) continue;
G[id].emplace_back(i);
G[i].emplace_back(id);
}
vector<int> in(V), out(V);
int e_id = 0;
auto dfs = [&](auto &&self, int cur, int par) -> void {
in[cur] = e_id++;
for(auto to : G[cur]) {
if(to == par) continue;
self(self, to, cur);
}
out[cur] = e_id;
};
dfs(dfs, id, -1);
LazySegmentTree<ll, ll> seg(V, 0, 0,
[](ll a, ll b) { return a + b; },
[](ll a, ll b) { return a + b; },
[](ll a, ll b) { return a + b; },
[](ll a, int x) { return a * x; });
uf.reset();
id = N;
iota(rec.begin(), rec.end(), 0);
for(auto q : queries) {
int t, a, b; tie(t, a, b) = q;
if(t == 1) {
a--; b--;
int u = uf.find(a), v = uf.find(b);
if(!uf.same(u, v)) {
uf.unite(u, v);
u = uf.find(a);
rec[u] = id++;
}
}
if(t == 2) {
a = uf.find(a-1);
int u = rec[a];
seg.update(in[u], out[u], b);
}
if(t == 3) {
a--;
printf("%lld\n", seg.query(in[a], in[a] + 1));
}
}
return 0;
}
tsutaj