結果

問題 No.1637 Easy Tree Query
ユーザー popofypopofy
提出日時 2021-08-06 22:06:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 6,047 bytes
コンパイル時間 4,850 ms
コンパイル使用メモリ 272,912 KB
実行使用メモリ 13,972 KB
最終ジャッジ日時 2023-10-17 05:10:23
合計ジャッジ時間 8,680 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 59 ms
9,484 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 23 ms
6,316 KB
testcase_05 AC 41 ms
5,524 KB
testcase_06 AC 28 ms
4,996 KB
testcase_07 AC 13 ms
4,348 KB
testcase_08 AC 18 ms
5,788 KB
testcase_09 AC 43 ms
7,900 KB
testcase_10 AC 21 ms
4,592 KB
testcase_11 AC 54 ms
8,428 KB
testcase_12 AC 49 ms
7,372 KB
testcase_13 AC 12 ms
4,732 KB
testcase_14 AC 35 ms
8,164 KB
testcase_15 AC 53 ms
8,692 KB
testcase_16 AC 48 ms
9,220 KB
testcase_17 AC 16 ms
4,732 KB
testcase_18 AC 39 ms
5,524 KB
testcase_19 AC 39 ms
6,052 KB
testcase_20 AC 50 ms
7,108 KB
testcase_21 AC 32 ms
6,580 KB
testcase_22 AC 63 ms
9,484 KB
testcase_23 AC 17 ms
4,996 KB
testcase_24 AC 28 ms
6,580 KB
testcase_25 AC 41 ms
5,268 KB
testcase_26 AC 53 ms
7,372 KB
testcase_27 AC 69 ms
9,220 KB
testcase_28 AC 32 ms
5,260 KB
testcase_29 AC 42 ms
6,844 KB
testcase_30 AC 26 ms
6,844 KB
testcase_31 AC 25 ms
4,372 KB
testcase_32 AC 24 ms
5,524 KB
testcase_33 AC 36 ms
4,796 KB
testcase_34 AC 35 ms
13,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
#define rep(i, n) for (decltype(n) i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (decltype(n) i = 1, i##_len = (n); i <= i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pl(s) cout << (s) << "\n";
#define plx(s) {cout << (s) << "\n"; exit(0);}
#define yes(s) cout << ((s)?"Yes":"No") << "\n";
#define bit(n) (1LL << ((int)(n)))
#define get1bit(x,n) (((x) >> (int)(n)) & 1)
#define pcnt(x) __builtin_popcountll(x)
#define flog(x) (63 - __builtin_clzll(x))
#define clog(x) (((x)==1)?0:(64-__builtin_clzll((x)-1)))
#define cdiv(x,y) (((x)+(y)-1)/(y))
#define lb(a,x) distance((a).begin(),lower_bound((a).begin(),(a).end(),(x)))
#define ub(a,x) distance((a).begin(),upper_bound((a).begin(),(a).end(),(x)))
#ifdef __LOCAL
#include <dump.hpp>
#define dump(...) DUMPOUT << "  " << string(#__VA_ARGS__) << ": " << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl << "    ", dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif
using ll = long long; using ld = long double; template<class T> using V = vector<T>;
template<class T> using PQ = priority_queue<T,V<T>,greater<T>>;
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> istream &operator>>(istream &is, complex<T> &v) {T x, y; is >> x >> y; v.real(x); v.imag(y); return is;}
template<class T> istream &operator>>(istream &is, V<T> &v) {for (auto&& e : v) is >> e;return is;}
template<class T, class U> istream &operator>>(istream &is, pair<T, U> &v) {is >> v.first >> v.second;return is;}
template<class T, size_t n> istream &operator>>(istream &is, array<T, n> &v) {for (auto&& e : v) is >> e;return is;}
template<class T> inline string join(const T& v, string sep = " ") {if (v.size() == 0) return "" ;stringstream ss;for (auto&& e : v) ss << sep << e;return ss.str().substr(1);}
template<class T> inline void uniq(T& a, bool presort = true){if (presort) sort(all(a));a.erase(unique(all(a)),a.end());}
template <class T> vector<T> compress(vector<T> &x){auto ret = x; uniq(ret); rep(i,sz(x)) x[i] = lb(ret, x[i]); return ret;}
template<class T> constexpr bool between(T a, T x, T b) {return (a <= x && x < b);}
template<class T> constexpr bool intersect(T l1, T r1, T l2, T r2) {return max(l1,l2) <= min(r1,r2);}
template<class T> V<T> make_vec(size_t n, T a) {return V<T>(n, a);}
template<class... Ts> auto make_vec(size_t n, Ts... ts) {return V<decltype(make_vec(ts...))>(n, make_vec(ts...));}
template<class T> inline V<T> CUM(V<T> &a) {int n = sz(a); V<T> ret(n+1); rep(i,n) ret[i+1] = a[i] + ret[i]; return ret;}
template<class T> inline V<T> DIF(V<T> &a) {int n = sz(a)-1; V<T> ret(n); rep(i,n) ret[i] = a[i+1] - a[i]; return ret;}
template<class T> void chooseKFromN(const int n, const int k, T f) {int x, y; for (int i = bit(k) - 1; i < bit(n); x = i & -i, y = i + x, i = (((i & ~y) / x) >> 1) | y) f(i);}
template<class T> void chooseFromS(const int n, int s, T f) {for (int i = bit(n)-1; i >= 0; --i) {i&=s; f(i);}}
template<class T> void chooseContainS(const int n, int s, T f) {for (int i = s; i < bit(n); i=(++i)|s) f(i);}
template<class T> void chooseFromMBit(const int n, const int m, T f) {
  V<ll> powm(n+1, 1); for (int i = 0; i < n; ++i) powm[i+1] = m * powm[i];
  for (int i = 0; i < powm[n]; ++i) {V<ll> bits(n);for (int j = 0; j < n; ++j) bits[j] = (i / powm[j]) % m;f(bits);}
}
template<class T> void choosePermutation(const int n, T f) {V<int> ord(n); iota(all(ord), 0);do{f(ord);} while (next_permutation(all(ord)));}
constexpr ll TEN(int n) {return (n == 0) ? 1 : 10 * TEN(n - 1);}
constexpr ll POW(ll x, ll n) {ll ret = 1;while (n > 0) {if (n & 1) ret *= x;x *= x;n >>= 1;}return ret;}
constexpr ll MODPOW(ll x, ll n, ll m) {ll ret = 1;while (n > 0) {if (n&1) ret = ret * x % m;x = x * x % m;n >>= 1;}return ret;}
constexpr ll nC2(ll n) {assert(between(ll(0),n,ll(3037000501)));return n * (n-1)/2;}
constexpr ll NSUM(ll n) {assert(between(ll(0),n,ll(3037000500)));return n * (n+1)/2;}
constexpr ll pos1d(ll y, ll x, ll h, ll w) {assert(between(ll(0),y,h));assert(between(ll(0),x,w));return y*w + x;}
constexpr pair<ll,ll> pos2d(ll p, ll h, ll w) {ll y = p/w, x = p - y*w;assert(between(ll(0),y,h));assert(between(ll(0),x,w));return {y, x};}
V<V<ll>> buildComb(int n = 60) {V<V<ll>> v(n+1, V<ll>(n+1));rep(i,sz(v)) {v[i][0] = 1; v[i][i] = 1;}for (int j = 1; j < sz(v); ++j) for (int k = 1; k < j; ++k) v[j][k] = v[j-1][k-1] + v[j-1][k];return v;}
inline bool palindrome(const string& s){return equal(all(s), s.rbegin());}
inline string upper(string s) {for(auto&& e: s) e = between('a',e,(char)('z'+1)) ? e - ('a'-'A') : e;return s;}
inline string lower(string s) {for(auto&& e: s) e = between('A',e,(char)('Z'+1)) ? e + ('a'-'A') : e;return s;}
inline string replace(string s, map<char, int> &from, V<int> &to) {for (auto&& e: s) e = '0' + (char)(to[from[e]]);return s;}
struct IOS {IOS() {cin.tie(nullptr); ios::sync_with_stdio(false); dump("");}} IO;
constexpr int INF = (1 << 30) - 1; constexpr ll INFL = 1LL << 60;



struct Solver {
  V<V<int>> G;
  V<ll> memo;


  void solve() {
    // ll n; cin >> n;
    // pl(POW(n-1,2))
    ll n, q; cin >> n >> q;
    G.resize(n);
    rep(i,n-1) {
      int a, b; cin >> a >> b; --a, --b;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    memo.resize(n);
    dfs(0);
    dump(memo);

    ll ans = 0;
    rep(qi,q) {
      ll p, x; cin >> p >> x; --p;
      ans += memo[p] * x;
      pl(ans)
    }
  }

  ll dfs(ll v, ll p = -1, ll depth = 0) {
    dump(v,p,depth);
    ++depth;
    if (memo[v]) return memo[v];
    ll ret = 1;
    for(auto&& e: G[v]) {
      if (e != p) {
        ret += dfs(e, v, depth);
      }
    }
    --depth;
    return memo[v] = ret;
  }
} solver;
signed main(void) {solver.solve();return 0;}
0