結果

問題 No.900 aδδitivee
ユーザー fumiphysfumiphys
提出日時 2019-10-04 22:17:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 471 ms / 2,000 ms
コード長 7,954 bytes
コンパイル時間 2,626 ms
コンパイル使用メモリ 187,500 KB
実行使用メモリ 30,432 KB
最終ジャッジ日時 2024-04-14 11:02:14
合計ジャッジ時間 13,588 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 460 ms
24,360 KB
testcase_08 AC 456 ms
24,372 KB
testcase_09 AC 459 ms
24,372 KB
testcase_10 AC 461 ms
24,368 KB
testcase_11 AC 466 ms
24,340 KB
testcase_12 AC 452 ms
24,368 KB
testcase_13 AC 452 ms
24,376 KB
testcase_14 AC 462 ms
24,352 KB
testcase_15 AC 458 ms
24,368 KB
testcase_16 AC 463 ms
24,248 KB
testcase_17 AC 455 ms
24,364 KB
testcase_18 AC 463 ms
24,336 KB
testcase_19 AC 471 ms
24,488 KB
testcase_20 AC 457 ms
24,364 KB
testcase_21 AC 458 ms
24,236 KB
testcase_22 AC 331 ms
30,432 KB
testcase_23 AC 330 ms
30,428 KB
testcase_24 AC 334 ms
30,304 KB
testcase_25 AC 333 ms
30,304 KB
testcase_26 AC 337 ms
30,428 KB
testcase_27 AC 330 ms
30,428 KB
testcase_28 AC 334 ms
30,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes
#include <bits/stdc++.h>
using namespace std;

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define bit(n) (1LL<<(n))
// functions
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << p.first << " " << p.second; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << ":" << itr->second; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << itr->first << ":" << itr->second; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20);}
} fast_io_;

template<typename T, typename E>
struct LazySegmentTree_ {
  function<T(T, T)> f; // aggregation
  function<E(E, E)> h; // update lazy element
  function<T(T, E, int)> p; // update element with lazy element
  int n;
  T def;
  E l_def;
  vector<T> vec;
  vector<E> lazy;
  LazySegmentTree_(){}
  LazySegmentTree_(int n_, function<T(T, T)> f, T def,
                   function<E(E, E)> h, E l_def, function<T(T, E, int)> p,
                   vector<T> v=vector<T>()): f(f), def(def), h(h), l_def(l_def), p(p){

    // initialize vector
    n = 1;
    while(n < n_){
      n *= 2;
    }
    vec = vector<T>(2*n-1, def);
    lazy = vector<E>(2*n-1, l_def);

    // initialize segment tree
    for(int i = 0; i < v.size(); i++){
      vec[i + n - 1] = v[i];
    }
    for(int i = n - 2; i >= 0; i--){
      vec[i] = f(vec[2*i+1], vec[2*i+2]);
    }
  }
  void eval(int k, int len){
    if(lazy[k] != l_def){
      if(k < n - 1){
        lazy[2*k+1] = h(lazy[2*k+1], lazy[k]);
        lazy[2*k+2] = h(lazy[2*k+2], lazy[k]);
      }
      vec[k] = p(vec[k], lazy[k], len);
      lazy[k] = l_def;
    }
  }
  E update(int a, int b, const E &val, int k, int l, int r){
    eval(k, r - l);
    if(r <= a || b <= l){
      return vec[k];
    }else if(a <= l && r <= b){
      lazy[k] = h(lazy[k], val);
      eval(k, r - l);
      return vec[k];
    }else{
      return vec[k] = f(update(a, b, val, 2*k+1, l, (l+r)/2),
                        update(a, b, val, 2*k+2, (l+r)/2, r));
    }
  }
  E update(int a, int b, E val){
    return update(a, b, val, 0, 0, n);
  }
  // [l, r) -> [a, b) (at k)
  T query(int a, int b, int k, int l, int r){
    eval(k, r - l);
    if(r <= a || b <= l)return def;
    if(a <= l && r <= b)return vec[k];
    T ld = query(a, b, 2*k+1, l, (l+r)/2);
    T rd = query(a, b, 2*k+2, (l+r)/2, r);
    return f(ld, rd);
  }
  T query(int a, int b){
    return query(a, b, 0, 0, n);
  }
};

template<typename T, typename E>
using LazySegmentTree = struct LazySegmentTree_<T, E>;
using LazySegmentTreeI = LazySegmentTree<int, int>;
using LazySegmentTreeL = LazySegmentTree<long long, long long>;

vector<int> v1, v2, V1, V2;

struct EulerTourTree{
  int n = 0;
  vector<vector<int>> edges;
  vector<int> b, e;
  vector<int> v;
  EulerTourTree(){}
  explicit EulerTourTree(int n): n(n){
    edges.resize(n);
    b.resize(n);
    e.resize(n);
    v.reserve(2 * n - 1);
  }
  void adde(int from, int to){
    edges[from].emplace_back(to);
  }
  void dfs(int i){
    v.emplace_back(i);
    b[i] = int(v.size()) - 1;
    v1.pb(i);
    V1.pb(b[i]);
    for(auto c: edges[i]){
      if(b[c] == -1){
        dfs(c);
        v.emplace_back(i);
      }
    }
    e[i] = int(v.size()) - 1;
    v2.pb(i);
    V2.pb(e[i]);
  }
  void build(int r = 0){
    b.assign(n, -1);
    e.assign(n, -1);
    v.resize(0);
    dfs(r);
  }
};

vector<pair<int, ll>> edge[100010];

int main(int argc, char const* argv[])
{
  int n; cin >> n;
  EulerTourTree t(n);
  rep(i, n - 1){
    int u, v;
    ll w;
    cin >> u >> v >> w;
    t.adde(u, v);
    edge[u].pb(v, w);
  }
  t.build();
  LazySegmentTreeL seg1 = LazySegmentTreeL(n, [](ll a, ll b){return a + b;},
                                          0, [](ll a, ll b){return a + b;},
                                          0, [](ll a, ll b, int c){return a + b * c;},
                                           vector<ll>());
  LazySegmentTreeL seg2 = LazySegmentTreeL(n, [](ll a, ll b){return a + b;},
                                           0, [](ll a, ll b){return a + b;},
                                           0, [](ll a, ll b, int c){return a + b * c;},
                                           vector<ll>());
  vector<int> mp1(n), mp2(n);
  rep(i, n)mp1[v1[i]] = i;
  rep(i, n)mp2[v2[i]] = i;
  rep(i, n){
    for(auto e: edge[i]){
      int m1 = mp1[e.first], m2 = mp2[e.first];
      seg1.update(m1, m1 + 1, e.second);
      seg2.update(m2, m2 + 1, e.second);
    }
  }

  /*cout << t.v << endl;
  cout << t.b << endl;
  cout << t.e << endl;*/
  /*cout << v1 << endl;
  cout << v2 << endl;
  cout << V1 << endl;
  cout << V2 << endl;
  rep(j, n){
    cout << seg1.query(j, j + 1) << "\n "[j + 1 != n];
  }
  rep(j, n){
    cout << seg2.query(j, j + 1) << "\n "[j + 1 != n];
  }
  cout << "------------------" << endl;*/
  int q; cin >> q;
  rep(i, q){
    /*rep(j, n){
      cout << seg1.query(j, j + 1) << "\n "[j + 1 != n];
    }
    rep(j, n){
      cout << seg2.query(j, j + 1) << "\n "[j + 1 != n];
      }*/
    int c; cin >> c;
    if(c == 1){
      int a;
      ll x;
      cin >> a >> x;
      int l = mp1[a] + 1;
      int r = upper_bound(all(V1), t.e[a]) - V1.begin();
      seg1.update(l, r, x);
      r = mp2[a];
      l = upper_bound(all(V2), t.b[a]) - V2.begin();
      seg2.update(l, r, x);
    }else{
      int b; cin >> b;
      /*if(b == 0){
        cout << 0 << endl;
        continue;
        }*/
      ll res = 0;
      res += seg1.query(0, mp1[b] + 1);
      int r = lower_bound(all(V2), t.b[b]) - V2.begin();
      res -= seg2.query(0, r);
      cout << res << endl;
    }
  }
  return 0;
}
0