結果
| 問題 |
No.2861 Slime Party
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-08-27 02:10:22 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3,445 ms / 4,000 ms |
| コード長 | 31,407 bytes |
| コンパイル時間 | 19,479 ms |
| コンパイル使用メモリ | 347,108 KB |
| 最終ジャッジ日時 | 2025-02-24 02:24:52 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 76 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = static_modint<1234567891>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif
//【デカルト木】
/*
* Cartesian_tree(vT a, greater = false) : O(n)
* a[0..n) の最小要素の位置を根とするデカルト木を構築する.
* 根から順に小さい要素での区間の分割を表す(同じ要素は左のものほど小さいとする.)
* greater = true とすると,大小関係を逆転して木の構築を行う.
*/
template <class T>
struct Cartesian_tree {
struct Node {
T val; // 区間の最小値
int l, r; // 区間 [l..r) に対応するノードであることを表す.
int p = -1; // 親(なければ -1)
int lc = -1; // 左の子(なければ -1)
int rc = -1; // 右の子(なければ -1)
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Node& v) {
os << "[" << v.l << "," << v.r << "):" << v.val
<< ", lc:" << v.lc << ", rc:" << v.rc << ", p:" << v.p;
return os;
}
#endif
};
int n; // 頂点の数
int rt; // 根
vector<Node> v; // 頂点
// 数列 a[0..n) で初期化する.
Cartesian_tree(const vector<T>& a, bool greater = false) : n(sz(a)), rt(0), v(n) {
// verify : https://judge.yosupo.jp/problem/cartesian_tree
if (n == 0) {
rt = -1;
return;
}
// 木の構造を決定する.
repi(i, 1, n - 1) {
// pt : i-1 の祖先で値が a[i] 以下であるもののうち最も深いもの(なければ -1)
int pt = i - 1;
while (pt != -1 && (!greater ? a[pt] > a[i] : a[pt] < a[i])) pt = v[pt].p;
// pt の右の子を i,i の左の子を pt の元の右の子とする.
if (pt != -1) {
v[i].p = pt;
if (v[pt].rc != -1) v[v[pt].rc].p = i;
v[i].lc = v[pt].rc;
v[pt].rc = i;
}
// pt がなければ i を根とする.
else {
v[i].lc = rt;
v[rt].p = i;
rt = i;
}
}
// ノードの情報を決定する.
function<void(int, int, int)> dfs = [&](int s, int l, int r) {
v[s].val = a[s];
v[s].l = l;
v[s].r = r;
if (v[s].lc != -1) dfs(v[s].lc, l, s);
if (v[s].rc != -1) dfs(v[s].rc, s + 1, r);
};
dfs(rt, 0, n);
}
Cartesian_tree() : n(0), rt(-1) {} // ダミー
// アクセス
inline Node const& operator[](int i) const { return v[i]; }
inline Node& operator[](int i) { return v[i]; }
// 大きさ
int size() const { return n; }
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Cartesian_tree& ct) {
rep(i, sz(ct)) os << i << ": " << ct[i] << endl;
return os;
}
#endif
};
//【static top tree】
/*
* Static_top_tree<S, F, get_val, get_fnc, add_edge, merge, add_vtx, comp, act>(Graph g, int r) : O(n (log n)^2)
* r を根とする根付き木 g で初期化する.
*
* set(int s) : O((log n)^2)
* 頂点 s の情報の更新を反映する.
*
* S get() : O(1)
* 根付き木全体の値を返す.
*
* なおテンプレート引数が表す関数は以下の通りとする:
*
* S get_val(int s) :
* 葉 s のみからなる部分木についての値を返す.
*
* F get_fnc(int s) :
* 節点 s のみからなる欠損部分木(子が 1 つ欠けた部分木)について,
* 関数 : (欠けた部分木の値 → 部分木 s の値) を返す.
*
* S add_edge(S x, int s) :
* 部分木 s の値が x でその親が p のとき,
* 辺 p'→s を追加した開部分木 p' についての答えを返す(記号 ' は開頂点を表す)
*
* S merge(S x, S y) :
* 開根を共有する開部分木 2 つの値がそれぞれ x, y のとき,それらをマージした開部分木の値を返す.
*
* F add_vtx(S x, int s) :
* 開根 s' をもつ開部分木 s' の値が x のとき,
* 根 s を追加した欠損部分木 s についての関数 : (欠けた部分木の値 → 部分木 s の値) を返す.
*
* F comp(F f, F g) :
* 合成関数 f o g を返す.
*
* S act(F f, S x) :
* f(x) を返す.
*/
template <class S, class F, S(*get_val)(int), F(*get_fnc)(int), S(*add_edge)(const S&, int), S(*merge)(const S&, const S&), F(*add_vtx)(const S&, int), F(*comp)(const F&, const F&), S(*act)(const F&, const S&)>
class Static_top_tree {
// 参考 : https://atcoder.jp/contests/abc351/editorial/9868
struct Node {
// tp : ノードのタイプ
// A:act, C:comp, V:add_vtx, M:merge, E:add_edge, f:get_fnc, x:get_val
char tp = '?';
// id : heavy path ならその根,light child ならその親
// ただし二分木の構築後は不要になるので,使い回して参照すべき頂点番号を表す.
int id = -1;
// [l..r) : heavy path, light child 共にどの範囲を見ているか
int l = -1, r = -1;
// pp : 親ノードへのポインタ,lp[rp] : 左[右]の子ノードへのポインタ
// ただし子が 1 つの場合は lp のみを使用する.
Node* pp = nullptr, * lp = nullptr, * rp = nullptr;
// f : 関数
F f;
// x : 値
S x;
};
// root : 根(根付き木全体に対応する)
Node* root;
// st[s] : 頂点 s の変更があったとき,どのノードから更新を始めればいいか
vector<Node*> st;
public:
// r を根とする根付き木 g で初期化する.
Static_top_tree(const Graph& g, int rt) {
// verify : https://atcoder.jp/contests/abc351/tasks/abc351_g
int n = sz(g);
// j_max[s] : s の重さ最大の部分木が何番目か
vi j_max(n, -1);
// 部分木の重さを調べる.
function<int(int, int)> dfs_wgt = [&](int s, int p) {
int ws = 0; int wt_max = -INF;
rep(j, sz(g[s])) {
auto t = g[s][j];
if (t == p) continue;
int wt = dfs_wgt(t, s);
ws += wt + 1;
if (chmax(wt_max, wt)) j_max[s] = j;
}
return ws;
};
dfs_wgt(rt, -1);
// hp[s] : 根を s とする heavy path を成す頂点の列(深さ降順)
vvi hp(n);
// lc[s] : 頂点 s の light child のリスト
vvi lc(n);
// HL 分解を行う.
function<void(int, int, int)> dfs_hld = [&](int s, int p, int r) {
hp[r].push_back(s);
if (j_max[s] != -1) {
int t = g[s][j_max[s]];
dfs_hld(t, s, r);
}
rep(j, sz(g[s])) {
int t = g[s][j];
if (t == p || j == j_max[s]) continue;
lc[s].push_back(t);
dfs_hld(t, s, t);
}
};
dfs_hld(rt, -1, rt);
root = new Node{ 'A', rt, 0, sz(hp[rt]) };
st.resize(n);
// トップダウンに二分木を構築する.
function<void(Node*)> dfs_btree = [&](Node* p) {
if (p->tp == 'A' || p->tp == 'C') {
if (p->r - p->l > 1) {
int m = (p->l + p->r) / 2;
p->lp = new Node{ 'C', p->id, p->l, m, p };
dfs_btree(p->lp);
p->rp = new Node{ p->tp, p->id, m, p->r, p };
dfs_btree(p->rp);
if (p->tp == 'A') p->x = act(p->lp->f, p->rp->x);
else p->f = comp(p->lp->f, p->rp->f);
}
else {
p->id = hp[p->id][p->l]; // 使い回して頂点番号を入れておく
st[p->id] = p;
int r = sz(lc[p->id]);
if (r > 0) {
p->tp = 'V';
p->lp = new Node{ 'M', p->id, 0, r, p };
dfs_btree(p->lp);
p->f = add_vtx(p->lp->x, p->id);
}
else {
if (p->tp == 'A') {
p->tp = 'x';
p->x = get_val(p->id);
}
else {
p->tp = 'f';
p->f = get_fnc(p->id);
}
}
}
}
else if (p->tp == 'M') {
if (p->r - p->l > 1) {
int m = (p->l + p->r) / 2;
p->lp = new Node{ 'M', p->id, p->l, m, p };
dfs_btree(p->lp);
p->rp = new Node{ 'M', p->id, m, p->r, p };
dfs_btree(p->rp);
p->x = merge(p->lp->x, p->rp->x);
}
else {
p->id = lc[p->id][p->l]; // 使い回して頂点番号を入れておく
int r = sz(hp[p->id]);
p->tp = 'E';
p->lp = new Node{ 'A', p->id, 0, r, p };
dfs_btree(p->lp);
p->x = add_edge(p->lp->x, p->id);
}
}
};
dfs_btree(root);
}
// 頂点 s の情報の更新を反映する.
void set(int v) {
// verify : https://atcoder.jp/contests/abc351/tasks/abc351_g
Node* p = st[v];
// ボトムアップに必要な箇所のみ更新する.
while (p) {
if (p->tp == 'A') {
p->x = act(p->lp->f, p->rp->x);
}
else if (p->tp == 'C') {
p->f = comp(p->lp->f, p->rp->f);
}
else if (p->tp == 'V') {
p->f = add_vtx(p->lp->x, p->id);
}
else if (p->tp == 'M') {
p->x = merge(p->lp->x, p->rp->x);
}
else if (p->tp == 'E') {
p->x = add_edge(p->lp->x, p->id);
}
else if (p->tp == 'x') {
p->x = get_val(v);
}
else if (p->tp == 'f') {
p->f = get_fnc(v);
}
p = p->pp;
}
}
// 根付き木全体の値を返す.
S get() {
// verify : https://atcoder.jp/contests/abc351/tasks/abc351_g
return root->x;
}
/* 雛形
using S = mint;
struct F {
mint a, b;
};
S get_val(int s) {
return a[s];
}
F get_fnc(int s) {
return { 1, a[s] };
}
S add_edge(const S& x, int s) {
return x;
}
S merge(const S& x, const S& y) {
return x * y;
}
F add_vtx(const S& x, int s) {
return { x, a[s] };
}
F comp(const F& f, const F& g) {
return { f.a * g.a, f.a * g.b + f.b };
}
S act(const F& f, const S& x) {
return f.a * x + f.b;
}
Static_top_tree<S, F, get_val, get_fnc, add_edge, merge, add_vtx, comp, act> G(g, 0);
*/
};
vl lv, ex; ll L; int st = -1;
struct S {
ll lv; // 現在レベルが lv で,これまでの総経験値が ex である,という状態
ll ex;
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const S& x) {
os << '(' << x.lv << "," << x.ex << ')';
return os;
}
#endif
};
struct F {
ll lv; // 現在レベルが lv より大きいなら経験値を ex 取得してレベルが ad 上がる,という関数
ll ad;
ll ex;
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const F& x) {
os << '(' << x.lv << "," << x.ad << "," << x.ex << ')';
return os;
}
#endif
};
S get_val(int s) {
return s == st ? S{ L, 0 } : S{ 0, 0 };
}
F get_fnc(int s) {
dump("!!");
return F{ -INFL, 0, 0 }; // 不要
}
S add_edge(const S& x, int s) {
return x;
}
S merge(const S& x, const S& y) {
dump("!!");
return S{ max(x.lv, y.lv), x.ex + y.ex }; // 不要
}
F add_vtx(const S& x, int s) {
F res;
if (x.lv == 0) {
res = F{ lv[s], ex[s] + x.ex, ex[s] + x.ex };
}
else {
if (x.lv > lv[s]) {
res = F{ -INFL, x.lv + ex[s], x.ex + ex[s] };
}
else {
res = F{ -INFL, x.lv, x.ex + ex[s] };
}
}
dump("add_vtx:", x, s, ":", res);
return res;
}
F comp(const F& f, const F& g) {
F res;
if (g.lv == -INFL) {
if (f.lv == -INFL) {
res = F{ -INFL, f.ad + g.ad, f.ex + g.ex };
}
else {
res = F{ f.lv - g.ad, f.ad + g.ad, f.ex + g.ex };
}
}
else {
if (f.lv == -INFL) {
res = F{ g.lv, f.ad + g.ad, f.ex + g.ex };
}
else {
// f.lv > g.lv
res = F{ max(g.lv, f.lv - g.ad), f.ad + g.ad, f.ex + g.ex }; // 間違い
}
}
return res;
}
S act(const F& f, const S& x) {
S res;
if (x.lv > f.lv) {
res = S{ x.lv + f.ad, x.ex + f.ex };
}
else {
res = S{ x.lv, x.ex + f.ex };
}
return res;
}
void WA() {
dump(INFL = 99);
int n, q;
cin >> n >> q >> L;
lv.resize(n);
ex.resize(n);
cin >> lv >> ex;
Cartesian_tree G(lv, 1);
dump(G);
Graph g(2 * n + 1); int r = -1;
rep(i, n) {
if (G[i].lc != -1) {
g[i].push_back(G[i].lc);
g[G[i].lc].push_back(i);
}
else {
g[i].push_back(n + i);
g[n + i].push_back(i);
}
if (G[i].rc != -1) {
g[i].push_back(G[i].rc);
g[G[i].rc].push_back(i);
}
else {
g[i].push_back(n + i + 1);
g[n + i + 1].push_back(i);
}
if (G[i].p == -1) r = i;
}
dumpel(g);
Static_top_tree<S, F, get_val, get_fnc, add_edge, merge, add_vtx, comp, act> S(g, r);
dump(S.get());
rep(hoge, q) {
int tp;
cin >> tp;
if (tp == 1) {
int a; ll b;
cin >> a >> b;
a--;
ex[a] = b;
S.set(a);
}
else {
int c;
cin >> c;
c--;
if (st != -1) {
int pst = st;
st = -1;
S.set(pst);
}
st = n + c;
S.set(st);
auto [l, e] = S.get();
cout << l << endl;
}
}
}
//【[部分木,パス]辺作用/[部分木,パス]辺総和(M-可換モノイド)】
/*
* Edge_apply_sum_query<S, op, o, F, act, comp, id>(Graph g, int rt) : O(n)
* rt を根とする根付き木 g と辺値 v[0..n) = o() で初期化する.
* 要素は M-可換モノイド (S, op, o, F, act, comp, id) の元とする.
*
* Edge_apply_sum_query<S, op, o, F, act, comp, id>(Graph g, int rt, vS a) : O(n)
* rt を根とする根付き木 g と辺値 v[0..n) = a[0..n) で初期化する.
* 辺値 v[s] は頂点 s に入る辺の値を表す(v[rt] は無視)
*
* set(int s, S x) : O(log n)
* 頂点 s に入る辺の値を x にする.
*
* S get(int s) : O(log n)
* 頂点 s に入る辺の値を返す.
*
* S sum_subtree(int s) : O(log n)
* 部分木 s の辺の値の総和を返す.
*
* S sum_path(int s, int t) : O((log n)^2)
* パス s→t 上の辺の値の総和を返す.
*
* apply(int s, F f) : O(log n)
* 頂点 s に入る辺の値に f を作用させる.
*
* apply_subtree(int v, F f) : O(log n)
* 部分木 s の辺の値に f を作用させる.
*
* apply_path(int s, int t, F f) : O((log n)^2)
* パス s→t 上の辺の値に f を作用させる.
*/
template <class S, S(*op)(S, S), S(*o)(), class F, S(*act)(F, S), F(*comp)(F, F), F(*id)()>
class Edge_apply_sum_query {
// 参考:https://qiita.com/Pro_ktmr/items/4e1e051ea0561772afa3
int n;
// in[s] : 根からの DFS で頂点 s に最初に入った時刻
// out[s] : 根からの DFS で頂点 s から最後に出た時刻
// top[s] : 頂点 s を含む heavy path の最も浅い頂点
// wgt[s] : 頂点 s の重さ(部分木 s のもつ辺の数)
// p[s] : 頂点 s の親
vi in, out, top, wgt, p;
// v[i] : 時刻 t に居た頂点に入る辺の値
using SEG = lazy_segtree<S, op, o, F, act, comp, id>;
SEG v;
// 各頂点の重さと親を求めるための DFS を行う.
void dfs1(const Graph& g, int rt) {
function<void(int)> rf = [&](int s) {
repe(t, g[s]) {
if (t == p[s]) continue;
p[t] = s;
rf(t);
wgt[s] += wgt[t] + 1;
}
};
p[rt] = -1;
rf(rt);
};
// 最も重い子を優先して DFS を行う.
void dfs2(const Graph& g, int rt) {
int time = 0;
function<void(int, int)> rf = [&](int s, int tp) {
in[s] = time;
top[s] = tp;
time++;
// 重さ最大の頂点を得る.
int w_max = -INF, t_max = -1;
repe(t, g[s]) {
if (t == p[s]) continue;
if (chmax(w_max, wgt[t])) t_max = t;
}
// 重さ最大の頂点を優先的になぞる.
if (t_max != -1) rf(t_max, tp);
// 残りの頂点をなぞる.
repe(t, g[s]) {
if (t == p[s] || t == t_max) continue;
rf(t, t);
}
// s から最後に離れる
out[s] = time;
};
rf(rt, rt);
}
public:
// rt を根とする根付き木 g と辺値 v[0..n) = o() で初期化する.
Edge_apply_sum_query(const Graph& g, int rt) : n(sz(g)), in(n), out(n), top(n), wgt(n), p(n), v(n) {
dfs1(g, rt);
dfs2(g, rt);
// 重み付きグラフの場合
//vector<S> ini(n);
//rep(s, n) repe(t, g[s]) if (t != p[s]) ini[in[t.to]] = t.cost;
//v = SEG(ini);
}
// rt を根とする根付き木 g と辺値 v[0..n) = a[0..n) で初期化する.
Edge_apply_sum_query(const Graph& g, int rt, const vector<S>& a) : n(sz(g)), in(n), out(n), top(n), wgt(n), p(n) {
dfs1(g, rt);
dfs2(g, rt);
vector<S> ini(n);
rep(s, n) ini[in[s]] = a[s];
v = SEG(ini);
}
Edge_apply_sum_query() : n(0) {}
// 頂点 s に入る辺の値を x にする.
void set(int s, S x) {
v.set(in[s], x);
}
// 頂点 s に入る辺の値を返す.
S get(int s) {
return v.get(in[s]);
}
// 部分木 s の辺の値の総和を返す.
S sum_subtree(int s) {
return v.prod(in[s] + 1, out[s]);
}
// パス s→t 上の辺の値の総和を返す.
S sum_path(int s, int t) {
// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_5_E
S res = o();
// s と t が異なる連結成分に属している限りループを回す.
while (top[s] != top[t]) {
// s の方が浅い連結成分に属しているとする.
if (in[top[s]] > in[top[t]]) swap(s, t);
// t を含む連結成分は v で並んで配置されているので,
// 最も浅い頂点 top[t] から t までの範囲の和を求める.
res = op(res, v.prod(in[top[t]], in[t] + 1));
// 一つ浅い連結成分に移動する.
t = p[top[t]];
}
// ここまできたら s と t は同じ連結成分に属するので,
// その間の辺のみの和を res に加算する.
if (in[s] > in[t]) swap(s, t);
res = op(res, v.prod(in[s] + 1, in[t] + 1));
return res;
}
// 頂点 s に入る辺に f を作用させる.
void apply(int s, F f) {
v.apply(in[s], f);
}
// 部分木 s の辺の値に f を作用させる.
void apply_subtree(int s, F f) {
v.apply(in[s] + 1, out[s], f);
}
// パス s→t 上の辺の値に f を作用させる.
void apply_path(int s, int t, F f) {
// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_5_E
// s と t が異なる連結成分に属している限りループを回す.
while (top[s] != top[t]) {
// s の方が浅い連結成分に属しているとする.
if (in[top[s]] > in[top[t]]) swap(s, t);
// t を含む連結成分は v で並んで配置されている.
v.apply(in[top[t]], in[t] + 1, f);
// 一つ浅い連結成分に移動する.
t = p[top[t]];
}
// ここまできたら s と t は同じ連結成分に属する.
if (in[s] > in[t]) swap(s, t);
v.apply(in[s] + 1, in[t] + 1, f);
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, Edge_apply_sum_query& q) {
rep(s, q.n) os << q.get(s) << " ";
return os;
}
#endif
};
//【加算 作用付き min 可換モノイド】
/* verify : https://atcoder.jp/contests/abc245/tasks/abc245_e */
using S106 = ll;
S106 op106(S106 x, S106 y) { return min(x, y); }
S106 e106() { return INFL; }
using F106 = ll;
S106 act106(F106 f, S106 x) { return f + x; }
F106 comp106(F106 f, F106 g) { return f + g; }
F106 id106() { return 0; }
#define Add_Min_mmonoid S106, op106, e106, F106, act106, comp106, id106
//【めぐる式二分探索】O(log|ok - ng|)
/*
* 条件 okQ() を満たす要素 ok と満たさない要素 ng との境界を二分探索する.
* 境界に隣り合うような条件を満たす要素(ok 側)の位置を返す.
* debug_mode = true にして実行すると手元では単調かどうかチェックしながら全探索する.
*/
template <class T, class FUNC>
T meguru_search(T ok, T ng, const FUNC& okQ, bool debug_mode = false) {
// 参考 : https://twitter.com/meguru_comp/status/697008509376835584
// verify : https://atcoder.jp/contests/typical90/tasks/typical90_a
Assert(ok != ng);
#ifdef _MSC_VER
// 単調かどうか自信がないとき用
if (debug_mode) {
T step = ok < ng ? 1 : -1; T res = ok; bool is_ok = true;
for (T i = ok; i != ng + step; i += step) {
auto b = okQ(i);
if (b) {
if (!is_ok) {
cout << "not monotony!" << endl;
for (T i = ok; i != ng + step; i += step) {
cout << i << " : " << okQ(i) << endl;
}
exit(1);
}
}
else {
if (is_ok) res = i - step;
is_ok = false;
}
}
return res;
}
#endif
// 境界が決定するまで
while (abs(ok - ng) > 1) {
// 区間の中間
T mid = (ok + ng) / 2;
// 中間が OK かどうかに応じて区間を縮小する.
if (okQ(mid)) ok = mid;
else ng = mid;
}
return ok;
/* okQ の定義の雛形
auto okQ = [&](ll x) {
return true || false;
};
*/
}
//【木の重さ】O(n)
/*
* 各 s∈[0..n) について,r を根とする木 g の頂点 s の重さを格納したリストを返す.
* s の重さとは,部分木 s に含まれる辺の本数(s 自身を除く子孫の数)のことである.
*/
vl weight_of_tree(const Graph& g, int r) {
// verify : https://atcoder.jp/contests/tessoku-book/tasks/tessoku_book_bm
int n = sz(g);
vl w(n);
function<ll(int, int)> dfs = [&](int s, int p) {
w[s] += ex[s];
repe(t, g[s]) {
if (t == p) continue;
w[s] += dfs(t, s);
}
return w[s];
};
dfs(r, -1);
return w;
}
//【オイラーツアー】
/*
* Euler_tour(Graph g, int rt) : O(n)
* rt を根とする根付き木 g で初期化する.
*
* int lca(int s, int t) : O(log n)
* 頂点 s, t の最小共通祖先を返す.
*
* int dist(int s, int t) : O(log n)
* 頂点 s, t 間の距離を返す.
*
* int jump(int s, int t, int i) : O(log n)
* 頂点 s から t までのパスの i 番目(0-indexed)の頂点を返す(なければ -1)
*
* sort_by_DFS_order(vi& vs) : O(log |vs|)
* 頂点集合 vs を DFS 昇順にソートする.
*/
ll op_ET(ll a, ll b) { return min(a, b); }
ll e_ET() { return INFL; }
struct Euler_tour {
int n;
// in[s] : rt からの DFS で最初に頂点 s を訪れた時刻(根なら 0)
// out[s] : rt からの DFS で最後に頂点 s から離れた時刻(根なら 2n-1)
// pos[t] : rt からの DFS で時刻 t に居た頂点の番号(長さ 2n-1)
// dep[s] : 頂点 s の深さ
vi in, out, pos, dep;
// seg[t] : 時刻 t に居た頂点の (深さ, 番号)
using SEG = segtree<ll, op_ET, e_ET>;
SEG seg;
void dfs(const Graph& g, int rt) {
int time = 0;
function<void(int, int)> rf = [&](int s, int p) {
// s を最初に訪れた
in[s] = time;
pos[time] = s;
time++;
repe(t, g[s]) {
if (t == p) continue;
dep[t] = dep[s] + 1;
rf(t, s);
pos[time] = s;
time++;
}
// s から最後に離れる
out[s] = time;
};
// 根から順に探索する.
rf(rt, -1);
}
public:
// rt を根とする根付き木 g で初期化する.
Euler_tour(const Graph& g, int rt) : n(sz(g)), in(n), out(n), pos(2 * n - 1), dep(n) {
// verify : https://judge.yosupo.jp/problem/lca
dfs(g, rt);
vl ini(2 * n - 1);
rep(t, 2 * n - 1) ini[t] = ((ll)dep[pos[t]] << 32) | pos[t];
seg = SEG(ini);
}
Euler_tour() : n(0) {}
// 頂点 s, t の最小共通祖先を返す.
int lca(int s, int t) const {
// verify : https://judge.yosupo.jp/problem/lca
// 初めて s または t に訪れたとき
int l = min(in[s], in[t]);
// 最後に s または t から離れたとき
int r = max(out[s], out[t]);
// その途中で訪れたことのある最も浅い頂点が最小共通祖先
return (int)seg.prod(l, r);
}
// 頂点 s, t 間の距離を返す.
int dist(int s, int t) const {
// verify : https://yukicoder.me/problems/no/2337
int p = lca(s, t);
// 根からの距離(深さ)の和を求め,ダブっている分を引く.
return dep[s] + dep[t] - 2 * dep[p];
}
// 頂点 s から t までのパスの i 番目(0-indexed)の頂点を返す(なければ -1)
int jump(int s, int t, int i) const {
// verify : https://judge.yosupo.jp/problem/jump_on_tree
int p = lca(s, t);
int ds = dep[s], dt = dep[t], dp = dep[p];
int dist = ds + dt - 2 * dp;
int res;
if (i < 0 || i > dist) {
res = -1;
}
else if (i <= ds - dp) {
int j = seg.max_right(out[s] - 1, [&](ll tmp) { return (tmp >> 32) > ds - i; });
res = pos[j];
}
else {
int j = seg.min_left(in[t] + 1, [&](ll tmp) { return (tmp >> 32) >= dt - (dist - i); });
res = pos[j];
}
return res;
}
// 頂点集合 vs を DFS 昇順にソートする.
void sort_by_DFS_order(vi& vs) const {
// verify : https://yukicoder.me/problems/no/2588
sort(all(vs), [&](int s, int t) { return in[s] < in[t]; });
}
};
//【木の深さ】O(n)
/*
* 各 s∈[0..n) について,r を根とする木 g の頂点 s の深さを格納したリストを返す.
* s の深さとは,根から s までの辺の本数のことである.
*/
vi depth_of_tree(const Graph& g, int r) {
// verify : https://algo-method.com/tasks/529
int n = sz(g);
vi d(n);
function<void(int, int)> dfs = [&](int s, int p) {
repe(t, g[s]) {
if (t == p) continue;
d[t] = d[s] + 1;
dfs(t, s);
}
};
dfs(r, -1);
return d;
}
//【木の親】O(n)
/*
* 各 s∈[0..n) について,r を根とする木 g の頂点 s の親を格納したリストを返す(なければ -1)
*/
vi parent_of_tree(const Graph& g, int r) {
int n = sz(g);
vi p(n);
function<void(int)> dfs = [&](int s) {
repe(t, g[s]) {
if (t == p[s]) continue;
p[t] = s;
dfs(t);
}
};
p[r] = -1;
dfs(r);
return p;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
//【解説 AC】
// L:fixed なのである頂点まで上ってきたときの値は始点に依らず予め決まっている.
// これを活かせば関数の合成みたいなことはする必要がない.
int n, q; ll L;
cin >> n >> q >> L;
vl lv(n); ex.resize(n);
cin >> lv >> ex;
Cartesian_tree G(lv, 1);
dump(G);
Graph g(2 * n + 1); int r = -1;
rep(i, n) {
if (G[i].lc != -1) {
g[i].push_back(G[i].lc);
g[G[i].lc].push_back(i);
}
else {
g[i].push_back(n + i);
g[n + i].push_back(i);
}
if (G[i].rc != -1) {
g[i].push_back(G[i].rc);
g[G[i].rc].push_back(i);
}
else {
g[i].push_back(n + i + 1);
g[n + i + 1].push_back(i);
}
if (G[i].p == -1) r = i;
}
dumpel(g);
auto dep = depth_of_tree(g, r);
auto par = parent_of_tree(g, r);
ex.resize(2 * n + 1);
auto ini = weight_of_tree(g, r);
rep(i, 2 * n + 1) {
if (i != r) ini[i] = ini[i] + L - lv[par[i]];
else ini[i] = ini[i] + L;
}
Edge_apply_sum_query<Add_Min_mmonoid> V(g, r, ini);
dump(V);
Euler_tour E(g, r);
ll ex_all = accumulate(all(ex), 0LL);
rep(hoge, q) {
int tp;
cin >> tp;
if (tp == 1) {
int a; ll b;
cin >> a >> b;
a--;
V.apply_path(a, r, b - ex[a]);
ex_all += b - ex[a];
ex[a] = b;
dump(V);
}
else {
int c;
cin >> c;
c += n;
auto okQ = [&](int x) {
int s = E.jump(r, c, x);
return V.sum_path(c, s) > 0;
};
int x = meguru_search(dep[c], -1, okQ);
dump("x:", x);
int s = E.jump(r, c, x);
dump("s:", s);
ll res = s == r ? ex_all + L : V.get(s) + lv[par[s]];
cout << res << "\n";
}
}
}