結果

問題 No.1637 Easy Tree Query
ユーザー kabbo ghoshkabbo ghosh
提出日時 2021-08-06 21:43:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 2,335 bytes
コンパイル時間 3,531 ms
コンパイル使用メモリ 208,304 KB
実行使用メモリ 13,308 KB
最終ジャッジ日時 2023-10-17 05:04:34
合計ジャッジ時間 7,792 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 59 ms
9,084 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 27 ms
6,444 KB
testcase_05 AC 42 ms
5,564 KB
testcase_06 AC 29 ms
5,136 KB
testcase_07 AC 12 ms
4,348 KB
testcase_08 AC 21 ms
5,756 KB
testcase_09 AC 53 ms
7,764 KB
testcase_10 AC 21 ms
4,808 KB
testcase_11 AC 63 ms
8,292 KB
testcase_12 AC 55 ms
7,236 KB
testcase_13 AC 13 ms
4,884 KB
testcase_14 AC 46 ms
8,028 KB
testcase_15 AC 68 ms
8,556 KB
testcase_16 AC 62 ms
9,084 KB
testcase_17 AC 18 ms
4,880 KB
testcase_18 AC 41 ms
5,564 KB
testcase_19 AC 42 ms
5,980 KB
testcase_20 AC 57 ms
6,992 KB
testcase_21 AC 38 ms
6,448 KB
testcase_22 AC 79 ms
9,084 KB
testcase_23 AC 19 ms
5,196 KB
testcase_24 AC 32 ms
6,460 KB
testcase_25 AC 42 ms
5,400 KB
testcase_26 AC 62 ms
7,560 KB
testcase_27 AC 78 ms
8,820 KB
testcase_28 AC 33 ms
5,392 KB
testcase_29 AC 49 ms
6,972 KB
testcase_30 AC 30 ms
6,788 KB
testcase_31 AC 25 ms
4,348 KB
testcase_32 AC 26 ms
5,468 KB
testcase_33 AC 37 ms
4,840 KB
testcase_34 AC 35 ms
13,308 KB
権限があれば一括ダウンロードができます

ソースコード

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