結果

問題 No.902 Query ζone
ユーザー niuezniuez
提出日時 2019-10-06 23:11:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,864 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 137,300 KB
実行使用メモリ 31,424 KB
最終ジャッジ日時 2024-04-20 00:19:07
合計ジャッジ時間 9,128 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;
template< typename G=vector<vector<int>> >
struct HeavyLightDecomposition {
  G &g;
  vector< int > sz, in, out, head, rev, par;

  HeavyLightDecomposition(G &g) :
      g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {}

  void dfs_sz(int idx, int p) {
    par[idx] = p;
    sz[idx] = 1;
    if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());
    for(auto &to : g[idx]) {
      if(to == p) continue;
      dfs_sz(to, idx);
      sz[idx] += sz[to];
      if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);
    }
  }

  void dfs_hld(int idx, int par, int &times) {
    in[idx] = times++;
    rev[in[idx]] = idx;
    for(auto &to : g[idx]) {
      if(to == par) continue;
      head[to] = (g[idx][0] == to ? head[idx] : to);
      dfs_hld(to, idx, times);
    }
    out[idx] = times;
  }

  void build() {
    dfs_sz(0, -1);
    int t = 0;
    dfs_hld(0, -1, t);
  }

  int la(int v, int k) {
    while(1) {
      int u = head[v];
      if(in[v] - k >= in[u]) return rev[in[v] - k];
      k -= in[v] - in[u] + 1;
      v = par[u];
    }
  }

  int lca(int u, int v) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) return u;
    }
  }

  template< typename T, typename Q, typename F >
  T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) {
    T l = ti, r = ti;
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v), swap(l, r);
      if(head[u] == head[v]) break;
      l = f(q(in[head[v]], in[v] + 1), l);
    }
    return f(f(q(in[u] + edge, in[v] + 1), l), r);
  }

  template< typename Q >
  void add(int u, int v, const Q &q, bool edge = false) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) break;
      q(in[head[v]], in[v] + 1);
    }
    q(in[u] + edge, in[v] + 1);
  }
};
vector<vector<int>> G;
vector<vector<P>> g;
ll d[100010];
void dfs(int x, int p){
	for(auto q:g[x]){
		int y=q.first;
		ll w=q.second;
		if(y!=p){
			d[y]=d[x]+w;
			dfs(y, x);
		}
	}
}
int main()
{
	int n; cin>>n;
  set<pair<pair<int,int>, ll>> st;
  for(int i=0; i<n-1; i++) {
    int u, v;
    ll w;
    cin>>u>>v>>w;
    st.insert({{min(u, v), max(u, v)}, w});
  }
  int Q;
  cin >> Q;
  for(int q = 0; q < Q;q++) {
    int type;
    cin >> type;
    if(type == 1) {
      int u, v, w;
      ll x;
      cin >> u >> v >> w >> x;
      st.erase(st.lower_bound({{min(u, v), max(u, v)}, 0}));
      st.insert({{min(v, w), max(v, w)}, x});
    }
    else if(type == 2){
      g.assign(n, vector<P>()); G.assign(n, vector<int>());
      for(auto p: st){
        int u = p.first.first, v = p.first.second;
        ll w = p.second;
        G[u].push_back(v);
        G[v].push_back(u);
        g[u].push_back({v, w});
        g[v].push_back({u, w});
      }
      HeavyLightDecomposition<> hld(G);
      hld.build();
      dfs(0, -1);
      int k; cin>>k;
      vector<int> v(k), ind(k);
      for(int j=0; j<k; j++){
        ind[j]=j;
        cin>>v[j];
      }
      sort(ind.begin(), ind.end(), [&](int p, int q){ return hld.in[v[p]]<hld.in[v[q]];});
      ll ans=0;
      for(int j=0; j<k; j++){
        int nx=j+1;
        if(nx==k) nx=0;
        int x=v[ind[j]], y=v[ind[nx]];
        int p=hld.lca(x, y);
        ans+=d[x]+d[y]-2*d[p];
      }
      ans/=2;
      cout<<ans<<endl;
    }
  }
}
0