結果

問題 No.1637 Easy Tree Query
ユーザー kabbo ghoshkabbo ghosh
提出日時 2021-08-06 21:43:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 2,335 bytes
コンパイル時間 2,011 ms
コンパイル使用メモリ 200,092 KB
最終ジャッジ日時 2025-01-23 14:56:22
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
* @Author: kabbo
* @Date:   2020-06-24 08:40:07
* @Last Modified by:   kabbo
* @Last Modified time: 2020-06-24 08:49:58
*/
#include <bits/stdc++.h>
using namespace std;
#define pii pair<long long, long long>
#define endl '\n'
#define ull unsigned long long
#define ll int64_t
#define ar array
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template <class Fun>
class y_combinator_result
{
    Fun fun_;

public:
    template <class T>
    explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}

    template <class... Args>
    decltype(auto) operator()(Args &&...args)
    {
        return fun_(std::ref(*this), std::forward<Args>(args)...);
    }
};

template <class Fun>
decltype(auto) y_combinator(Fun &&fun)
{
    return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
//const int mod = 1e9 + 7;
using u64 = uint64_t;
using u128 = __uint128_t;
#define sc1(x) scanf("%lld", &(x));
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifndef NEAL_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
/*Well, probably you won't understand anything,
because you didn't try to understand anything in your life,
you expect all hard work to be done for you by someone else. 
Let's start*/
void solve()
{
    int n, m;
    cin >> n >> m;
    vector adj(n, vector(0, 0));
    for (int i(0), u, v; i + 1 < n; ++i)
    {
        cin >> u >> v;
        --u, --v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    ll ans = 0;
    vector child(n, 0);
    y_combinator(
        [&](auto self, int u, int par) -> void
        {
            child[u] = 1;
            for (auto v : adj[u])
            {
                if (v != par)
                {
                    self(v, u);
                    child[u] += child[v];
                }
            }
        })(0, -1);
    while (m--)
    {
        int u;
        ll sum;
        cin >> u >> sum;
        ans += (sum * (ll)child[u - 1]);
        cout << ans << endl;
    }
}
int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
0