#line 2 "/home/shogo314/cpp_include/ou-library/io.hpp" /** * @file io.hpp * @brief 空白区切り出力、iostreamのオーバーロード */ #include #include #include #include #include namespace tuple_io { template std::basic_istream& read_tuple(std::basic_istream& is, Tuple& t) { is >> std::get(t); if constexpr (I + 1 < std::tuple_size_v) { return read_tuple(is, t); } return is; } template std::basic_ostream& write_tuple(std::basic_ostream& os, const Tuple& t) { os << std::get(t); if constexpr (I + 1 < std::tuple_size_v) { os << CharT(' '); return write_tuple(os, t); } return os; } }; template std::basic_istream& operator>>(std::basic_istream& is, std::pair& p) { is >> p.first >> p.second; return is; } template std::basic_istream& operator>>(std::basic_istream& is, std::tuple& p) { return tuple_io::read_tuple, 0>(is, p); } template std::basic_istream& operator>>(std::basic_istream& is, std::array& a) { for(auto& e : a) is >> e; return is; } template std::basic_istream& operator>>(std::basic_istream& is, std::vector& v) { for(auto& e : v) is >> e; return is; } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::pair& p) { os << p.first << CharT(' ') << p.second; return os; } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::tuple& p) { return tuple_io::write_tuple, 0>(os, p); } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::array& a) { for(size_t i = 0; i < N; ++i) { if(i) os << CharT(' '); os << a[i]; } return os; } template std::basic_ostream& operator<<(std::basic_ostream& os, const std::vector& v) { for(size_t i = 0; i < v.size(); ++i) { if(i) os << CharT(' '); os << v[i]; } return os; } /** * @brief 空行出力 */ void print() { std::cout << '\n'; } /** * @brief 出力して改行 * * @tparam T 型 * @param x 出力する値 */ template void print(const T& x) { std::cout << x << '\n'; } /** * @brief 空白区切りで出力して改行 * * @tparam T 1つ目の要素の型 * @tparam Tail 2つ目以降の要素の型 * @param x 1つ目の要素 * @param tail 2つ目以降の要素 */ template void print(const T& x, const Tail&... tail) { std::cout << x << ' '; print(tail...); } #line 2 "/home/shogo314/cpp_include/ou-library/segtree.hpp" /** * @file segtree.hpp * @brief セグメント木 */ #include #include #include #include #line 13 "/home/shogo314/cpp_include/ou-library/segtree.hpp" /** * @brief セグメント木のCRTP基底クラス * * @tparam S モノイドの型 * @tparam ActualSegTree 派生クラス */ template class SegTreeBase { S op(const S& a, const S& b) const { return static_cast(*this).op(a, b); } S e() const { return static_cast(*this).e(); } int n, sz, height; std::vector data; void update(int k) { data[k] = op(data[2 * k], data[2 * k + 1]); } class SegTreeReference { SegTreeBase& segtree; int k; public: SegTreeReference(SegTreeBase& segtree, int k) : segtree(segtree), k(k) {} SegTreeReference& operator=(const S& x) { segtree.set(k, x); return *this; } operator S() const { return segtree.get(k); } }; protected: void construct_data() { sz = 1; height = 0; while (sz < n) { sz <<= 1; height++; } data.assign(sz * 2, e()); } void initialize(const std::vector& v) { for (int i = 0; i < n; i++) data[sz + i] = v[i]; for (int i = sz - 1; i > 0; i--) update(i); } public: // Warning: 継承先のコンストラクタでconstruct_data()を必ず呼び出す! SegTreeBase(int n = 0) : n(n) {} /** * @brief 指定された要素の値を返す * * @param k インデックス * @return S 値 */ S get(int k) const { return data[sz + k]; } /** * @brief 指定された要素の値を返す * * @param k インデックス * @return S 値 */ S operator[] (int k) const { return get(k); } /** * @brief 指定された要素への参照を返す * * @param k * @return SegTreeReference 要素への参照 代入されるとset()が呼ばれる */ SegTreeReference operator[] (int k) { return SegTreeReference(*this, k); } /** * @brief 内容を出力する * * @tparam CharT 出力ストリームの文字型 * @tparam Traits 出力ストリームの文字型特性 * @param os 出力ストリーム * @param rhs セグメント木 * @return std::basic_ostream& 出力ストリーム */ template friend std::basic_ostream& operator<<(std::basic_ostream& os, const SegTreeBase& rhs) { for(int i = 0; i < rhs.n; i++) { if(i != 0) os << CharT(' '); os << rhs[i]; } return os; } /** * @brief 指定された要素の値をxに更新する * * @param k インデックス * @param x 新しい値 */ void set(int k, const S& x) { k += sz; data[k] = x; for (int i = 1; i <= height; i++) update(k >> i); } /** * @brief 指定された要素の値にxを作用させる * * @param k インデックス * @param x 作用素 */ void apply(int k, const S& x) { set(k, op(get(k), x)); } /** * @brief [l, r)の区間の総積を返す * * @param l 半開区間の開始 * @param r 半開区間の終端 * @return S 総積 */ S prod(int l, int r) const { S left_prod = e(), right_prod = e(); l += sz; r += sz; while (l < r) { if (l & 1) left_prod = op(left_prod, data[l++]); if (r & 1) right_prod = op(data[--r], right_prod); l >>= 1; r >>= 1; } return op(left_prod, right_prod); } /** * @brief すべての要素の総積を返す * * @return S 総積 */ S all_prod() const { return data[1]; } /** * @brief (r = l or f(prod([l, r))) = true) and (r = n or f(prod([l, r+1))) = false)となるrを返す * fが単調なら、f(prod([l, r))) = trueとなる最大のr * * @tparam F * @param l 半開区間の開始 * @param f 判定関数 f(e) = true * @return int */ template int max_right(int l, F f) const { assert(f(e())); if (l == n) return n; l += sz; while (l % 2 == 0) l >>= 1; S sum = e(); while(f(op(sum, data[l]))) { sum = op(sum, data[l]); l++; while (l % 2 == 0) l >>= 1; if (l == 1) return n; } while (l < sz) { if (!f(op(sum, data[l * 2]))) l *= 2; else { sum = op(sum, data[l * 2]); l = l * 2 + 1; } } return l - sz; } /** * @brief (l = 0 or f(prod([l, r))) = true) and (l = r or f(prod([l-1, r))) = false)となるlを返す * fが単調なら、f(prod([l, r))) = trueとなる最小のl * * @tparam F * @param r 半開区間の終端 * @param f 判定関数 f(e) = true * @return int */ template int min_left(int r, F f) const { assert(f(e())); if (r == 0) return 0; r += sz; while (r % 2 == 0) r >>= 1; S sum = e(); while(f(op(data[r-1], sum))) { sum = op(data[r-1], sum); r--; while (r % 2 == 0) r >>= 1; if (r == 1) return 0; } while (r < sz) { if (!f(op(data[r * 2 - 1], sum))) r *= 2; else { sum = op(data[r * 2 - 1], sum); r = r * 2 - 1; } } return r - sz; } }; /** * @brief 積のファンクタが静的な場合のセグメント木の実装 * * @tparam S モノイドの型 * @tparam Op 積のファンクタ * @tparam E 積の単位元を返すファンクタ */ template class StaticSegTree : public SegTreeBase> { using BaseType = SegTreeBase>; inline static Op operator_object; inline static E identity_object; public: S op(const S& a, const S& b) const { return operator_object(a, b); } S e() const { return identity_object(); } /** * @brief デフォルトコンストラクタ * */ StaticSegTree() = default; /** * @brief コンストラクタ * * @param n 要素数 */ explicit StaticSegTree(int n) : BaseType(n) { this->construct_data(); } /** * @brief コンストラクタ * * @param v 初期の要素 */ explicit StaticSegTree(const std::vector& v) : StaticSegTree(v.size()) { this->initialize(v); } }; /** * @brief コンストラクタで関数オブジェクトを与えることで積を定義するセグメント木の実装 * テンプレート引数を省略することができ、ラムダ式が使える * * @tparam S モノイドの型 * @tparam Op 積の関数オブジェクトの型 */ template class SegTree : public SegTreeBase> { using BaseType = SegTreeBase>; Op operator_object; S identity; public: S op(const S& a, const S& b) const { return operator_object(a, b); } S e() const { return identity; } /** * @brief デフォルトコンストラクタ */ SegTree() = default; /** * @brief コンストラクタ * * @param n 要素数 * @param op 積の関数オブジェクト * @param identity 単位元 */ explicit SegTree(int n, Op op, const S& identity) : BaseType(n), operator_object(std::move(op)), identity(identity) { this->construct_data(); } /** * @brief コンストラクタ * * @param v 初期の要素 * @param op 積の関数オブジェクト * @param identity 単位元 */ explicit SegTree(const std::vector& v, Op op, const S& identity) : SegTree(v.size(), std::move(op), identity) { this->initialize(v); } }; namespace segtree { template struct Max { const S operator() (const S& a, const S& b) const { return std::max(a, b); } }; template struct Min { const S operator() (const S& a, const S& b) const { return std::min(a, b); } }; template >* = nullptr> struct MaxLimit { constexpr S operator() () const { return std::numeric_limits::max(); } }; template >* = nullptr> struct MinLimit { constexpr S operator() () const { return std::numeric_limits::lowest(); } }; template struct Zero { S operator() () const { return S(0); } }; } /** * @brief RangeMaxQuery * * @tparam S 型 */ template using RMaxQ = StaticSegTree, segtree::MinLimit>; /** * @brief RangeMinQuery * * @tparam S 型 */ template using RMinQ = StaticSegTree, segtree::MaxLimit>; /** * @brief RangeSumQuery * * @tparam S 型 */ template using RSumQ = StaticSegTree, segtree::Zero>; #line 1 "/home/shogo314/cpp_include/sh-library/base.hpp" #include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ull = unsigned long long; using str = string; using pl = pair; template using ml = map; using mll = map; using sl = set; using ld = long double; using pd = pair; template using vec = vector; template using vv = vector>; template using vvv = vector>>; template using vp = vector>; using vl = vec; using vvl = vv; using vvvl = vvv; using vs = vec; using vc = vec; using vi = vec; using vb = vec; using vpl = vec; using spl = set; using vd = vec; using vpd = vec; template using ary = array; template using al = array; template using aal = array, N1>; template using val = vec>; #define all(obj) (obj).begin(), (obj).end() #define reps(i, a, n) for (ll i = (a); i < ll(n); i++) #define rep(i, n) reps(i, 0, (n)) #define rrep(i, n) reps(i, 1, (n) + 1) #define repds(i, a, n) for (ll i = ((n) - 1); i >= (a); i--) #define repd(i, n) repds(i, 0, (n)) #define rrepd(i, n) repds(i, 1, (n) + 1) #define rep2(i, j, x, y) rep(i, x) rep(j, y) template inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } #define LL(x) ll x;cin >> x; #define VL(a,n) vl a(n);cin >> a; #define AL(a,k) al a;cin >> a; #define VC(a,n) vc a(n);cin >> a; #define VS(a,n) vs a(n);cin >> a; #define STR(s) str s;cin >> s; #define VPL(a,n) vpl a(n);cin >> a; #define VAL(a,n,k) val a(n);cin >> a; #define VVL(a,n,m) vvl a(n,vl(m));cin >> a; #define SL(a,n) sl a;{VL(b,n);a=sl(all(b));} template using heap_max = priority_queue, less>; template using heap_min = priority_queue, greater>; #line 4 "main.cpp" void solve() { LL(N); LL(M); LL(K); VL(S, K + 1); rep(i, K + 1) { S[i]--; } vvl g(N, vl(N, 1LL << 61)); rep(i, M) { LL(A); LL(B); LL(C); A--; B--; chmin(g[A][B], C); chmin(g[B][A], C); } rep(k, N) { rep(i, N) { rep(j, N) { chmin(g[i][j], g[i][k] + g[k][j]); } } } rep(i, N) { g[i][i] = 0; } vl init(K); rep(i, K) { // cerr << "(" << S[i] << ", " << S[i + 1] << ")" << endl; init[i] = g[S[i]][S[i + 1]]; } RSumQ seg(init); if (false) { cerr << "S = " << S << endl; cerr << "seg ="; rep(i, K) { cerr << " " << seg[i]; } cerr << endl; } LL(Q); while (Q--) { LL(T); LL(X); LL(Y); if (T == 1) { Y--; S[X] = Y; if (X > 0) { seg.set(X - 1, g[S[X - 1]][S[X]]); } if (X < K) { seg.set(X, g[S[X]][S[X + 1]]); } } else { print(seg.prod(X, Y)); } if (false) { cerr << "S = " << S << endl; cerr << "seg ="; rep(i, K) { cerr << " " << seg[i]; } cerr << endl; } } } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); solve(); }