結果

問題 No.1637 Easy Tree Query
ユーザー noya2noya2
提出日時 2021-08-06 21:25:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 8,981 bytes
コンパイル時間 5,319 ms
コンパイル使用メモリ 288,432 KB
実行使用メモリ 45,284 KB
最終ジャッジ日時 2023-10-17 04:48:11
合計ジャッジ時間 11,558 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 174 ms
41,852 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 79 ms
23,216 KB
testcase_05 AC 122 ms
17,656 KB
testcase_06 AC 82 ms
13,932 KB
testcase_07 AC 31 ms
4,560 KB
testcase_08 AC 62 ms
19,036 KB
testcase_09 AC 143 ms
32,452 KB
testcase_10 AC 59 ms
11,820 KB
testcase_11 AC 179 ms
35,496 KB
testcase_12 AC 157 ms
28,272 KB
testcase_13 AC 38 ms
12,348 KB
testcase_14 AC 125 ms
33,144 KB
testcase_15 AC 183 ms
36,796 KB
testcase_16 AC 173 ms
40,712 KB
testcase_17 AC 48 ms
12,348 KB
testcase_18 AC 115 ms
17,636 KB
testcase_19 AC 121 ms
20,560 KB
testcase_20 AC 161 ms
27,556 KB
testcase_21 AC 108 ms
23,680 KB
testcase_22 AC 216 ms
41,320 KB
testcase_23 AC 53 ms
14,460 KB
testcase_24 AC 94 ms
23,744 KB
testcase_25 AC 120 ms
16,664 KB
testcase_26 AC 169 ms
29,692 KB
testcase_27 AC 214 ms
39,736 KB
testcase_28 AC 93 ms
15,516 KB
testcase_29 AC 136 ms
26,124 KB
testcase_30 AC 86 ms
25,996 KB
testcase_31 AC 64 ms
4,348 KB
testcase_32 AC 75 ms
17,104 KB
testcase_33 AC 103 ms
12,084 KB
testcase_34 AC 111 ms
45,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using bint = mp::cpp_int;
*/
#include <atcoder/all>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define repp(i,n,m) for (int i = m; i < int(n); ++i)
#define repb(i,n) for (int i = int(n)-1; i >= 0; --i)
#define fi first
#define se second
#define endl "\n"
using namespace std;
using namespace atcoder;
//using namespace internal;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<long double, long double>;
const int INF = 1001001007;
const long long mod1 = 1000000007LL;
const long long mod2 = 998244353LL;
const ll inf = 2e18;
const ld pi = 3.14159265358979323;
template<typename T>void priv(vector<T> &v){if(v.size()==0){cout<<endl;}else{rep(i,v.size()-1)cout<<v[i]<<" ";cout<<v[v.size()-1]<<endl;}}
template<typename T>void privv(vector<vector<T>> &v){rep(i,v.size()){rep(j,v[i].size()-1)cout<<v[i][j]<<" ";cout<<v[i][v[i].size()-1]<<endl;}}
template<typename T>bool range(T a,T b,T x){return (a<=x&&x<b);}
template<typename T>bool rrange(pair<T,T> a,pair<T,T> b,pair<T,T> x){return (range(a.fi,b.fi,x.fi)&&range(a.se,b.se,x.se));}
template<typename T>void rev(vector<T> &v){reverse(v.begin(),v.end());}
template<typename T>void sor(vector<T> &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);}
template<typename T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<typename T>void eru(vector<T> &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());}
template<typename T>T cel(T a,T b){if (a%b==0)return a/b;return a/b +1;}
template<typename T,typename U> void pout(pair<T,U> p){cout<<p.fi<<" "<<p.se<<endl;}
template<typename T>void myswap(T &a,T &b){if(a>b)swap(a,b);}
void yes(){cout << "Yes" << endl;}
void no (){cout << "No" << endl;}
void yn (bool t){if(t)yes();else no();}
void Yes(){cout << "YES" << endl;}
void No (){cout << "NO" << endl;}
void YN (bool t){if(t)Yes();else No();}
void dout() {cout << setprecision(20);}
void deb(ll h = INF-1) {cout << (h == INF-1 ? "!?" : to_string(h)) << endl;}
void revs(string &s) {reverse(s.begin(),s.end());}
vector<int> dx = {0,1,0,-1};
vector<int> dy = {1,0,-1,0};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string num = "0123456789";

ll gcds(ll a, ll b){
    a = abs(a); b = abs(b);
    if (b == 0) return a;
    ll c = a % b;
    while (c != 0){
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

ll tentou(vector<ll> ar){
    int n = ar.size();
    set<ll> st;
    rep(i,n) st.insert(ar[i]);
    map<ll,int> mp;
    int ind = 0;
    for (ll x : st){
        mp[x] = ind;
        ind++;
    }
    fenwick_tree<ll> fw(ind);
    ll ans = 0;
    rep(i,n){
        int a = mp[ar[i]];
        ans += i - fw.sum(0,a+1);
        fw.add(a,1);
    }
    return ans;
}

/*
alias g++='g++ -I/mnt/c/Users/Owner/Desktop/ac-library'
*/


struct vs{
    vector<int> to;
    vector<ll> cost;
};

//頂点の2色彩色を書き足すこと
struct Tree{
    int n; // 頂点の数
    int root; // 根付き木とみたときの根
    vector<int> par; // par[i] : 頂点 i の親 (i が根なら -1)
    vector<vs> edges; // edges[i] : 頂点 i から出る辺の情報 (隣接リスト)
    vector<int> subtreesize; // subtreesize[i] : 頂点 i を根とする部分木のサイズ
    vector<int> depth; // depth[i] : 頂点 i の深さ (i が根なら 0)
    vector<int> shallow; // shallow[i] : 頂点 i が属する連結成分の最も浅い頂点の番号
    int hldindex; // HLD ですでに訪れた頂点の数
    vector<int> ikigake; // ikigake[i] : 頂点 root から HLD したとき i 番目に訪れた頂点
    vector<int> pre; // 頂点 root から HLD したとき pre[i] 番目に訪れた頂点が i
    vector<vector<int>> par2; // par2[i][j] : 頂点 j から 2^i 回登った時の親 (根を通り過ぎたら -1)
    vector<ll> dists; // dist[i] : 頂点 root からの距離
    // 根を ROOT として、par[i] (i\in [0,N)) を求める
    void parents(int ROOT){
        par[ROOT] = -1;
        queue<int> que;
        que.push(ROOT);
        while (!que.empty()){
            int p = que.front(); que.pop();
            for (int x : edges[p].to){
                if (par[x] == -2){
                    par[x] = p;
                    que.push(x);
                }
            }
        }
    }
    // 頂点 v を根とする部分木のサイズを求める
    int siz(int v){
        if (subtreesize[v] != 0) return subtreesize[v];
        subtreesize[v] = 1;
        for (int x : edges[v].to){
            if (par[v] == x) continue;
            subtreesize[v] += siz(x);
        }
        return subtreesize[v];
    }
    // 頂点 v の深さを求める
    int dep(int v){
        if (depth[v] != -1) return depth[v];
        if (par[v] == -1) return depth[v] = 0;
        return depth[v] = 1 + dep(par[v]);
    }
    // 頂点 v から HLD をする (s は v が属する連結成分の最も浅い頂点の番号)
    void HLD(int v, int s){
        ikigake[hldindex] = v;
        pre[v] = hldindex;
        hldindex++;
        shallow[v] = s;
        int _maxsubtreesize = 0;
        int _index = -1;
        if (siz(v) == 1) return;
        for (int x : edges[v].to){
            if (par[v] == x) continue;
            if (chmax(_maxsubtreesize, siz(x))){
                _index = x;
            } 
        }
        HLD(_index, s);
        for (int x : edges[v].to){
            if (par[v] == x) continue;
            if (x != _index) HLD(x, x);
        }
    }
    // par2[i][j] を求める O(N log N)
    void lcainit(int N){
        for (int i = 0; i < N; i++) par2[0][i] = par[i];
        for (int i = 0; i < 29 ; i++) {
            for (int j = 0; j < N; j++) {
                if (par2[i][j] < 0) par2[i+1][j] = -1;
                else par2[i+1][j] = par2[i][par2[i][j]];
            }
        }
    }
    // dist[i] を求める O(N)
    void distinit(int ROOT){
        dists[ROOT] = 0;
        queue<int> que;
        que.push(ROOT);
        while (!que.empty()){
            int p = que.front(); que.pop();
            int ind = 0;
            for (int x : edges[p].to){
                if (dists[x] == -1) {
                    ll addcost = 1;
                    if (ind < edges[p].cost.size()){
                        addcost = edges[p].cost[ind];
                    }
                    dists[x] = dists[p] + addcost;
                    que.push(x);
                }
                ind++;
            }
        }
    }
    void init(bool hld){
        parents(root);
        siz(root);
        if (hld) HLD(root,root);
        lcainit(n);
        distinit(root);
    }
    // コンストラクタ
    Tree (int N, vector<vs> EDGES, int ROOT = 0, bool hld = false) : n(N), edges(EDGES), root(ROOT), 
    par(n,-2), subtreesize(n,0), depth(n,-1), shallow(n,-1), ikigake(n,-1), pre(n,-1), 
    hldindex(0), par2(30,vector<int>(N,-1)), dists(N,-1) {
        init(hld);
    }
    // u~v パスに含まれる頂点たち
    vector<P> solve(int u, int v){
        vector<P> leftright; 
        while (shallow[u] != shallow[v]){
            if (dep(shallow[u]) <= dep(shallow[v])){
                leftright.emplace_back(P(pre[shallow[v]],pre[v]));
                v = par[shallow[v]];
            }
            else {
                leftright.emplace_back(P(pre[shallow[u]],pre[u]));
                u = par[shallow[u]];
            }
        }
        if (pre[u] > pre[v]) swap(u,v);
        leftright.emplace_back(P(pre[u],pre[v]));
        return leftright;
    }
    // 根を root としたときの 頂点 (u,v) の最小共通祖先LCA
    int lca(int u, int v){
        if (dep(u) > dep(v)) swap(u,v);
        for (int i = 0; i < 30; i++) if ((dep(v) - dep(u)) >> i & 1) v = par2[i][v];
        if (u == v) return u;
        for (int k = 29; k >= 0; k--){
            if (par2[k][u] != par2[k][v]) {
                u = par2[k][u];
                v = par2[k][v];
            }
        }
        return par2[0][u];
    }
    ll dist(int u, int v){
        int w = lca(u,v);
        return dists[u] + dists[v] - 2 * dists[w];
    }
};

int main(){
    int n, q; cin >> n >> q;
    vector<vs> ar(n);
    rep(i,n-1){
        int a, b; cin >> a >> b;
        a--; b--;
        ar[a].to.emplace_back(b);
        ar[b].to.emplace_back(a);
    }
    Tree tree(n,ar);
    ll ima = 0;
    vector<ll> ans(q);
    rep(i,q){
        int x; cin >> x;
        ll v; cin >> v;
        ima += ll(tree.siz(x-1)) * v;
        ans[i] = ima;
    }
    rep(i,q) cout << ans[i] << endl;
}
0