結果
| 問題 |
No.235 めぐるはめぐる (5)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-02 18:50:54 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,788 ms / 10,000 ms |
| コード長 | 8,468 bytes |
| コンパイル時間 | 2,030 ms |
| コンパイル使用メモリ | 196,600 KB |
| 実行使用メモリ | 52,068 KB |
| 最終ジャッジ日時 | 2024-10-03 06:02:12 |
| 合計ジャッジ時間 | 10,490 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 3 |
ソースコード
#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using Graph = vector<vector<int>>;
struct HLDecomposition {
using pii = pair<int, int>;
int n;
Graph G;
vector<int> vid, inv, par, depth, subsize, head, prev, next, type;
HLDecomposition(const Graph& G_) :
n(G_.size()), G(G_),
vid(n, -1), inv(n), par(n), depth(n), subsize(n, 1),
head(n), prev(n, -1), next(n, -1), type(n) {}
void build(vector<int> roots = { 0 }) {
int curtype = 0, pos = 0;
for (int root : roots) {
decide_heavy_edge(root);
reconstruct(root, curtype++, pos);
}
}
void decide_heavy_edge(int root) {
stack<pii> st;
par[root] = -1, depth[root] = 0;
st.emplace(root, 0);
while (!st.empty()) {
int now = st.top().first;
int& way = st.top().second;
if (way < G[now].size()) {
int child = G[now][way++];
if (child == par[now])continue;
par[child] = now;
depth[child] = depth[now] + 1;
st.emplace(child, 0);
}
else {
st.pop();
int maxsize = 0;
for (auto child : G[now]) {
if (child == par[now])continue;
subsize[now] += subsize[child];
if (maxsize < subsize[child]) {
maxsize = subsize[child];
prev[child] = now;
next[now] = child;
}
}
}
}
}
void reconstruct(int root, int curtype, int& pos) {
stack<int> st({ root });
while (!st.empty()) {
int start = st.top(); st.pop();
for (int v = start; v != -1; v = next[v]) {
type[v] = curtype;
vid[v] = pos++;
inv[vid[v]] = v;
head[v] = start;
for (auto child : G[v]) {
if (child != par[v] && child != next[v]) {
st.push(child);
}
}
}
}
}
// node query [u, v], f([left, right])
void foreach_nodes(int u, int v, const function<void(int, int)>& f) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
f(max(vid[head[v]], vid[u]), vid[v]);
if (head[u] != head[v])v = par[head[v]];
else break;
}
}
// edge query[u,v] f([left, right])
// seg_node[vid[i]] := edge(par[i] -> i)
void foreach_edges(int u, int v, const function<void(int, int)>& f) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
if (head[u] != head[v]) {
f(vid[head[v]], vid[v]);
v = par[head[v]];
}
else {
if (u != v)f(vid[u] + 1, vid[v]);
break;
}
}
}
int lca(int u, int v) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
if (head[u] == head[v])return u;
v = par[head[v]];
}
}
};
template<typename Monoid, typename OperatorMonoid = Monoid>
class LazySegmentTree {
private:
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid, int)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
int sz; // 対応する配列の幅
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f; // 2区間マージ演算(data-data-ボトムアップマージ)
const G g; // 要素,作用素マージ演算(lazy->data同位置変換時の、(data,lazy,len)の計算)
const H h; // 作用素マージ演算 (query->lazyトップダウン伝搬時の、(lazy,query_value)の計算)
const Monoid M1; // モノイド単位元 (data単位元)
const OperatorMonoid OM0; // 作用素単位元 (lazy単位元)
void propagate(int idx, int len) {
// 幅lenのlazy[idx]が存在するとき、値を下に流す
if (lazy[idx] != OM0) {
if (idx < sz) {
lazy[(idx << 1) | 0] = h(lazy[(idx << 1) | 0], lazy[idx]);
lazy[(idx << 1) | 1] = h(lazy[(idx << 1) | 1], lazy[idx]);
}
data[idx] = g(data[idx], lazy[idx], len);
lazy[idx] = OM0;
}
}
Monoid update_impl(int a, int b, const OperatorMonoid& val, int idx, int l, int r) {
propagate(idx, r - l);
if (r <= a || b <= l)return data[idx];
else if (a <= l && r <= b) {
lazy[idx] = h(lazy[idx], val);
propagate(idx, r - l);
return data[idx];
}
else return data[idx] = f(
update_impl(a, b, val, (idx << 1) | 0, l, (l + r) >> 1),
update_impl(a, b, val, (idx << 1) | 1, (l + r) >> 1, r)
);
}
Monoid query_impl(int a, int b, int idx, int l, int r) {
propagate(idx, r - l);
if (r <= a || b <= l)return M1;
else if (a <= l && r <= b)return data[idx];
else return f(
query_impl(a, b, (idx << 1) | 0, l, (l + r) >> 1),
query_impl(a, b, (idx << 1) | 1, (l + r) >> 1, r)
);
}
public:
// init忘れに注意
LazySegmentTree(int n, const F f, const G g, const H h,
const Monoid& M1, const OperatorMonoid OM0)
:f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
while (sz < n)sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void build(const vector<Monoid>& vals) {
rep(idx, vals.size())data[idx + sz] = vals[idx];
for (int idx = sz - 1; idx > 0; idx--) {
data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
}
}
Monoid update(int a, int b, const OperatorMonoid& val) {
return update_impl(a, b, val, 1, 0, sz);
}
Monoid query(int a, int b) {
return query_impl(a, b, 1, 0, sz);
}
Monoid operator[](const int& idx) {
return query(idx, idx + 1);
}
};
int main()
{
int N, Q;
cin >> N;
vector<pair<ll, ll>> sc(N);
Graph graph(N);
rep(i, N)cin >> sc[i].first;
rep(i, N)cin >> sc[i].second;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
// 金額和, 倍率和
using pll = pair<ll, ll>;
constexpr ll MOD = 1000000007;
auto f = [&](pll vl, pll vr) {
return make_pair(
(vl.first + vr.first) % MOD,
(vl.second + vr.second) % MOD
);
};
auto g = [&](pll data, ll lazy, int len) {
return make_pair(
(data.first + data.second*lazy) % MOD,
data.second
);
};
auto h = [&](ll lazy, ll query) {
return (lazy + query) % MOD;
};
LazySegmentTree<pll, ll> lst(N, f, g, h, { 0ll,0ll }, 0);
HLDecomposition hld(graph);
hld.build();
vector<pll> nsc(N);
rep(i, N) {
nsc[hld.vid[i]] = sc[i];
}
lst.build(nsc);
cin >> Q;
rep(q, Q) {
/*
vector<pll> par(N);
rep(i, N)par[i] = make_pair(hld.vid[i], i);
sort(par.begin(), par.end());
rep(i, N) {
// vid -> normal id -> cost -> parent -> size
int nid = par[i].second;
assert(hld.inv[i] == nid);
cerr
<< i
<< ":"
<< par[i].second
<< "..."
<< lst[i].first
<< "..."
<< hld.par[i]
<< "..."
<< hld.subsize[i]
<< endl;
}
*/
int type, x, y, z;
cin >> type >> x >> y;
x--; y--;
if (type == 0) {
cin >> z;
hld.foreach_nodes(x, y, [&](int l, int r) {
//cerr << l << "..." << r << endl;
lst.update(l, r + 1, z);
});
}
else {
ll res = 0;
hld.foreach_nodes(x, y, [&](int l, int r) {
//cerr << l << "..." << r << endl;
(res += lst.query(l, r + 1).first) %= MOD;
});
cout << res << endl;
}
}
return 0;
}