提出 #41148690


ソースコード 拡げる

#include <atcoder/all>
using namespace atcoder;
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
using ll = long long;
const double pi = 3.14159265359;
const ll INF = 1LL << 60;

template<class T> inline bool chmin(T& a, T b){ if (a > b){a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b){ if (a < b){a = b; return true;} return false;}

//using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;  // mint::set_mod(p); later
//using mint = static_modint<1000000009>;

int N;
vector<int> u, v, w;
vector<vector<pair<int, int>>> edges;

// vs: DFS で巡った頂点の列
// es: vs に対応する辺の番号
// idx: 各頂点が vs に最初に現れるインデックス
// depth: 深さ
vector<int> vs, es, idx, depth;

void dfs(int v, int par, int e, int d)
{
  idx[v] = (int) vs.size();
  vs.push_back(v);
  depth.push_back(d);

  for (auto p : edges[v]){
    auto [nv, ne] = p;
    if (nv != par){
      es.push_back(ne);
      dfs(nv, v, ne, d+1);
      vs.push_back(v);
      depth.push_back(d);
    }
  }

  es.push_back(-e); // 帰りの辺番号
}

ll op(ll a, ll b){ return a + b; }
ll e(){ return 0; }

pair<int, int> op2(pair<int, int> a, pair<int, int> b){ return min(a, b); }
pair<int, int> e2(){ return make_pair((int) 1e9, -1); }

int main()
{
  cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);

  // 1. Input
  cin >> N;
  u.resize(N-1);
  v.resize(N-1);
  w.resize(N-1);
  edges.resize(N);
  for (int i = 0; i < N - 1; i++){
    cin >> u[i] >> v[i] >> w[i];
    u[i]--;
    v[i]--;
    edges[u[i]].emplace_back(v[i], i+1);
    edges[v[i]].emplace_back(u[i], i+1);
  }

  // 2. DFS: オイラーツアー
  idx.resize(N);
  if (N > 1)
    dfs(0, -1, -1, 0);

  // 3. 経路の距離を管理
  segtree<ll, op, e> seg((int) es.size());
  vector<vector<int>> vp(N-1), vn(N-1);
  for (int i = 0; i < (int) es.size(); i++){
    int j = es[i];
    if (j > 0){
      j--;
      seg.set(i, w[j]);
      vp[j].push_back(i);
    } else {
      j = -j;
      j--;
      seg.set(i, -w[j]);
      vn[j].push_back(i);
    }
  }

  // 4. LCA のための seg
  segtree<pair<int, int>, op2, e2> rmq((int) depth.size());
  for (int i = 0; i < (int) depth.size(); i++){
    rmq.set(i, make_pair(depth[i], i));
  }
  
  // Query
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++){
    int ty;
    cin >> ty;
    if (ty == 1){
      int i, w;
      cin >> i >> w;
      i--;
      for (int j : vp[i]){
	seg.set(j, w);
      }
      for (int j : vn[i]){
	seg.set(j, -w);
      }
    } else {
      int u, v;
      cin >> u >> v;
      u--; v--;

      if (u == v){
	cout << "0\n";
      } else {
	int l = idx[u];
	int r = idx[v];
	if (l > r) swap(l, r);

	auto [d, j] = rmq.prod(l, r+1);
	int lca = idx[vs[j]];

	ll dist = seg.prod(min(lca, l), max(lca, l)) + seg.prod(min(lca, r), max(lca, r));
	cout << dist << "\n";
      }
    }
  }
  
  return 0;
}

提出情報

提出日時
問題 G - Distance Queries on a Tree
ユーザ unnohideyuki
言語 C++ (GCC 9.2.1)
得点 600
コード長 3074 Byte
結果 AC
実行時間 436 ms
メモリ 78604 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 4
AC × 73
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt, 02_random_32.txt, 02_random_33.txt, 02_random_34.txt, 02_random_35.txt, 02_random_36.txt, 02_random_37.txt, 02_random_38.txt, 02_random_39.txt, 02_random_40.txt, 02_random_41.txt, 02_random_42.txt, 02_random_43.txt, 02_random_44.txt, 02_random_45.txt, 02_random_46.txt, 02_random_47.txt, 02_random_48.txt, 02_random_49.txt, 02_random_50.txt, 02_random_51.txt, 02_random_52.txt, 02_random_53.txt, 02_random_54.txt, 02_random_55.txt, 02_random_56.txt, 02_random_57.txt, 02_random_58.txt, 02_random_59.txt, 02_random_60.txt, 02_random_61.txt, 02_random_62.txt, 02_random_63.txt, 02_random_64.txt, 02_random_65.txt, 02_random_66.txt, 02_random_67.txt, 02_random_68.txt, 02_random_69.txt, 02_random_70.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 6 ms 3540 KiB
00_sample_01.txt AC 2 ms 3640 KiB
00_sample_02.txt AC 2 ms 3712 KiB
00_sample_03.txt AC 2 ms 3468 KiB
01_handmade_03.txt AC 411 ms 64968 KiB
01_handmade_04.txt AC 418 ms 64892 KiB
01_handmade_05.txt AC 375 ms 64636 KiB
01_handmade_06.txt AC 372 ms 78588 KiB
01_handmade_07.txt AC 387 ms 69428 KiB
01_handmade_08.txt AC 327 ms 64868 KiB
01_handmade_09.txt AC 384 ms 64492 KiB
01_handmade_10.txt AC 41 ms 3532 KiB
02_random_10.txt AC 117 ms 26952 KiB
02_random_11.txt AC 116 ms 26932 KiB
02_random_12.txt AC 209 ms 49896 KiB
02_random_13.txt AC 324 ms 63296 KiB
02_random_14.txt AC 332 ms 62972 KiB
02_random_15.txt AC 302 ms 53180 KiB
02_random_16.txt AC 88 ms 11348 KiB
02_random_17.txt AC 233 ms 34768 KiB
02_random_18.txt AC 208 ms 33448 KiB
02_random_19.txt AC 321 ms 60084 KiB
02_random_20.txt AC 260 ms 39232 KiB
02_random_21.txt AC 331 ms 77296 KiB
02_random_22.txt AC 178 ms 32660 KiB
02_random_23.txt AC 98 ms 12252 KiB
02_random_24.txt AC 127 ms 20848 KiB
02_random_25.txt AC 241 ms 65852 KiB
02_random_26.txt AC 77 ms 12248 KiB
02_random_27.txt AC 299 ms 58312 KiB
02_random_28.txt AC 146 ms 40860 KiB
02_random_29.txt AC 170 ms 30328 KiB
02_random_30.txt AC 203 ms 56288 KiB
02_random_31.txt AC 245 ms 37460 KiB
02_random_32.txt AC 181 ms 34184 KiB
02_random_33.txt AC 384 ms 60988 KiB
02_random_34.txt AC 221 ms 38932 KiB
02_random_35.txt AC 287 ms 52004 KiB
02_random_36.txt AC 292 ms 50532 KiB
02_random_37.txt AC 304 ms 59776 KiB
02_random_38.txt AC 257 ms 52860 KiB
02_random_39.txt AC 301 ms 62128 KiB
02_random_40.txt AC 200 ms 36644 KiB
02_random_41.txt AC 233 ms 44364 KiB
02_random_42.txt AC 304 ms 66492 KiB
02_random_43.txt AC 259 ms 57884 KiB
02_random_44.txt AC 348 ms 70696 KiB
02_random_45.txt AC 273 ms 55228 KiB
02_random_46.txt AC 188 ms 35488 KiB
02_random_47.txt AC 292 ms 60976 KiB
02_random_48.txt AC 267 ms 55604 KiB
02_random_49.txt AC 176 ms 34456 KiB
02_random_50.txt AC 289 ms 55340 KiB
02_random_51.txt AC 400 ms 64556 KiB
02_random_52.txt AC 387 ms 64560 KiB
02_random_53.txt AC 420 ms 64456 KiB
02_random_54.txt AC 396 ms 64380 KiB
02_random_55.txt AC 409 ms 64488 KiB
02_random_56.txt AC 391 ms 64912 KiB
02_random_57.txt AC 370 ms 64740 KiB
02_random_58.txt AC 394 ms 65036 KiB
02_random_59.txt AC 374 ms 64912 KiB
02_random_60.txt AC 394 ms 64896 KiB
02_random_61.txt AC 374 ms 73080 KiB
02_random_62.txt AC 419 ms 70868 KiB
02_random_63.txt AC 436 ms 71152 KiB
02_random_64.txt AC 414 ms 72848 KiB
02_random_65.txt AC 397 ms 78604 KiB
02_random_66.txt AC 344 ms 64876 KiB
02_random_67.txt AC 338 ms 64920 KiB
02_random_68.txt AC 353 ms 64860 KiB
02_random_69.txt AC 334 ms 64916 KiB
02_random_70.txt AC 343 ms 64932 KiB