結果
問題 | No.900 aδδitivee |
ユーザー |
|
提出日時 | 2019-10-04 22:17:07 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 363 ms / 2,000 ms |
コード長 | 7,954 bytes |
コンパイル時間 | 1,822 ms |
コンパイル使用メモリ | 191,312 KB |
実行使用メモリ | 30,564 KB |
最終ジャッジ日時 | 2024-10-03 07:55:31 |
合計ジャッジ時間 | 10,469 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 27 |
ソースコード
// 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))// functionstemplate <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; autotitr = 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;}// typesusing ll = long long int;using P = pair<int, int>;// constantsconst 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};// iostruct 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; // aggregationfunction<E(E, E)> h; // update lazy elementfunction<T(T, E, int)> p; // update element with lazy elementint 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 vectorn = 1;while(n < n_){n *= 2;}vec = vector<T>(2*n-1, def);lazy = vector<E>(2*n-1, l_def);// initialize segment treefor(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;}