結果

問題 No.900 aδδitivee
ユーザー chocoruskchocorusk
提出日時 2019-10-04 21:52:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 4,952 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 131,664 KB
実行使用メモリ 20,608 KB
最終ジャッジ日時 2024-04-14 10:30:35
合計ジャッジ時間 12,220 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 407 ms
17,920 KB
testcase_08 AC 405 ms
17,920 KB
testcase_09 AC 408 ms
17,920 KB
testcase_10 AC 405 ms
18,048 KB
testcase_11 AC 416 ms
18,176 KB
testcase_12 AC 406 ms
18,000 KB
testcase_13 AC 404 ms
17,920 KB
testcase_14 AC 409 ms
17,948 KB
testcase_15 AC 401 ms
17,920 KB
testcase_16 AC 416 ms
17,920 KB
testcase_17 AC 416 ms
18,048 KB
testcase_18 AC 408 ms
18,164 KB
testcase_19 AC 401 ms
18,048 KB
testcase_20 AC 401 ms
17,920 KB
testcase_21 AC 409 ms
17,920 KB
testcase_22 AC 338 ms
20,556 KB
testcase_23 AC 345 ms
20,480 KB
testcase_24 AC 338 ms
20,608 KB
testcase_25 AC 335 ms
20,500 KB
testcase_26 AC 343 ms
20,480 KB
testcase_27 AC 341 ms
20,524 KB
testcase_28 AC 338 ms
20,480 KB
権限があれば一括ダウンロードができます

ソースコード

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, int> P;

template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
	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;
	const G g;
	const H h;
	const Monoid e1;
	const OperatorMonoid e0;

	LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){
		sz=1;
		while(sz<n) sz<<=1;
		data.resize(2*sz-1, e1);
		lazy.resize(2*sz-1, e0);
	}

	void build(vector<Monoid> v){
		for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i];
		for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]);
	}

	void eval(int k, int l, int r){
		if(lazy[k]!=e0){
			data[k]=g(data[k], lazy[k], r-l);
			if(k<sz-1){
				lazy[2*k+1]=h(lazy[2*k+1], lazy[k]);
				lazy[2*k+2]=h(lazy[2*k+2], lazy[k]);
			}
		}
		lazy[k]=e0;
	}

	void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){
		eval(k, l, r);
		if(r<=a || b<=l) return;
		if(a<=l && r<=b){
			lazy[k]=h(lazy[k], x);
			eval(k, l, r);
		}else{
			update(a, b, x, 2*k+1, l, (l+r)/2);
			update(a, b, x, 2*k+2, (l+r)/2, r);
			data[k]=f(data[2*k+1], data[2*k+2]);
		}
	}
	void update(int a, int b, const OperatorMonoid &x){
		return update(a, b, x, 0, 0, sz);
	}

	Monoid find(int a, int b, int k, int l, int r){
		eval(k, l, r);
		if(b<=l || r<=a) return e1;
		if(a<=l && r<=b) return data[k];
		else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r));
	}
	Monoid find(int a, int b){
		return find(a, b, 0, 0, sz);
	}
	Monoid operator[](const int &k){
		return find(k, k+1);
	}
};

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);
  }
};
int main()
{
	int n;
	cin>>n;
	vector<vector<int>> G(n);
	int a[100010], b[100010], w[100010];
	for(int i=0; i<n-1; i++){
		cin>>a[i]>>b[i]>>w[i];
		G[a[i]].push_back(b[i]);
		G[b[i]].push_back(a[i]);
	}
	auto f=[](ll a, ll b){ return a+b;};
	auto g=[](ll a, ll x, int len){ return a+x*len;};
	auto h=[](ll x, ll y){ return x+y;};
	LazySegmentTree<ll> seg(n, f, g, h, 0, 0);
	vector<ll> v(n);
	HeavyLightDecomposition<> hld(G);
	hld.build();
	for(int i=0; i<n-1; i++){
		if(hld.in[a[i]]>hld.in[b[i]]) swap(a[i], b[i]);
		v[hld.in[b[i]]]=w[i];
	}
	seg.build(v);
	int Q; cin>>Q;
	for(int z=0; z<Q; z++){
		int t; cin>>t;
		if(t==2){
			int u;
			cin>>u;
			auto q=[&](int i, int j){ return seg.find(i, j);};
			cout<<hld.query(0, u, 0ll, q, f, true)<<endl;
		}else{
			int v; ll w;
			cin>>v>>w;
			seg.update(hld.in[v]+1, hld.out[v], w);
		}
	}
	return 0;
}
0