結果

問題 No.898 tri-βutree
ユーザー milanis48663220milanis48663220
提出日時 2019-10-04 22:02:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 559 ms / 4,000 ms
コード長 3,095 bytes
コンパイル時間 1,357 ms
コンパイル使用メモリ 84,960 KB
実行使用メモリ 28,528 KB
最終ジャッジ日時 2023-08-08 15:59:16
合計ジャッジ時間 12,476 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 336 ms
28,528 KB
testcase_01 AC 4 ms
8,776 KB
testcase_02 AC 3 ms
8,532 KB
testcase_03 AC 4 ms
8,536 KB
testcase_04 AC 4 ms
8,568 KB
testcase_05 AC 3 ms
8,560 KB
testcase_06 AC 4 ms
8,700 KB
testcase_07 AC 537 ms
24,712 KB
testcase_08 AC 543 ms
24,920 KB
testcase_09 AC 542 ms
24,864 KB
testcase_10 AC 546 ms
24,712 KB
testcase_11 AC 544 ms
24,896 KB
testcase_12 AC 548 ms
24,752 KB
testcase_13 AC 559 ms
24,720 KB
testcase_14 AC 548 ms
24,776 KB
testcase_15 AC 546 ms
24,868 KB
testcase_16 AC 545 ms
24,824 KB
testcase_17 AC 552 ms
24,696 KB
testcase_18 AC 553 ms
24,924 KB
testcase_19 AC 553 ms
24,892 KB
testcase_20 AC 555 ms
24,760 KB
testcase_21 AC 542 ms
24,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct edge{
    long to, cost;
};

vector<long> G[100000];
vector<edge> Ge[100000];
vector<long> et;
bool used[100000];
long depth[100001];
long idx[100000];

template <typename T>
struct segtree{
    long n;
    T UNIT;
    vector<T> dat;
    segtree(long n_, T unit){
        UNIT = unit;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(long i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    //これを変更
    T calc(T a, T b){
        if(depth[a] < depth[b]){
            return a;
        }else{
            return b;
        }
    }
    void insert(long k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(long i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(long k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1, 0, 0, segtree.n)と呼ぶこと
    T query(long a, long b, long k, long l, long r){
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

long cnt = 0;

void dfs(long v, long d){
    used[v] = true;
    depth[v] = d;
    for(long i = 0; i < G[v].size(); i++){
        if(!used[G[v][i]]){
            et.push_back(G[v][i]);
            idx[G[v][i]] = et.size()-1;
            dfs(G[v][i], d+1);
            et.push_back(v);
        }
    }
}

long dist[100000];

void dfs1(long v, long d){
    used[v] = true;
    dist[v] = d;
    for(long i = 0; i < Ge[v].size(); i++){
        if(!used[Ge[v][i].to]){
            dfs1(Ge[v][i].to, d+Ge[v][i].cost);
        }
    }
}

int main(){
    long n;
    cin >> n;
    for(long i = 0; i < n-1; i++){
        long u, v, w;
        cin >> u >> v >> w;
        G[u].push_back(v);
        G[v].push_back(u);
        Ge[u].push_back(edge{v, w});
        Ge[v].push_back(edge{u, w});
    }    
    et.push_back(0);
    dfs(0, 0);
    for(int i = 0; i < n; i++) used[i] = false;
    dfs1(0, 0);
    depth[n] = n+1;
    segtree<long> sgt(et.size()+1, n);
    for(long i = 0; i < et.size(); i++){
        sgt.insert(i, et[i]);
    }
    sgt.update_all();
    long q;
    cin >> q;
    for(long i = 0; i < q; i++){
        long x, y, z;
        cin >> x >> y >> z;
        long id[3]; id[0] = idx[x]; id[1] = idx[y]; id[2] = idx[z];
        sort(id, id+3);
        long ans = 0;
        long p = sgt.query(id[0], id[1], 0, 0, sgt.n);
        ans += (dist[x]+dist[y]-dist[p]*2);
        p = sgt.query(id[0], id[2], 0, 0, sgt.n);
        ans += (dist[x]+dist[z]-dist[p]*2);
        p = sgt.query(id[1], id[2], 0, 0, sgt.n);
        ans += (dist[y]+dist[z]-dist[p]*2);
        cout << ans/2 << endl;
    }   
}
0