結果

問題 No.399 動的な領主
ユーザー momoyuumomoyuu
提出日時 2023-03-21 21:03:01
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,113 ms / 2,000 ms
コード長 3,843 bytes
コンパイル時間 3,673 ms
コンパイル使用メモリ 263,100 KB
実行使用メモリ 23,724 KB
最終ジャッジ日時 2023-10-18 18:35:30
合計ジャッジ時間 13,884 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,688 KB
testcase_01 AC 3 ms
5,688 KB
testcase_02 AC 3 ms
5,692 KB
testcase_03 AC 3 ms
5,692 KB
testcase_04 AC 6 ms
5,792 KB
testcase_05 AC 63 ms
6,716 KB
testcase_06 AC 1,107 ms
15,804 KB
testcase_07 AC 1,080 ms
15,804 KB
testcase_08 AC 1,053 ms
16,068 KB
testcase_09 AC 1,066 ms
16,068 KB
testcase_10 AC 8 ms
5,796 KB
testcase_11 AC 46 ms
6,756 KB
testcase_12 AC 724 ms
16,332 KB
testcase_13 AC 686 ms
16,332 KB
testcase_14 AC 185 ms
23,724 KB
testcase_15 AC 329 ms
23,724 KB
testcase_16 AC 476 ms
19,764 KB
testcase_17 AC 1,113 ms
16,068 KB
testcase_18 AC 1,089 ms
16,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

template< typename T1, typename T2 = T1 >
struct lazysegtree{
    using F1 = function<T1(T1,T1)>;
    using F2 = function<T2(T2,T2)>;
    //using F3 = function<T1(T2,T1)>;
    using F3 = function<T1(T2,T1,int,int)>;
    int n;
    F1 f1;
    F2 f2;
    F3 f3;
    T1 e1;
    T2 e2,e3;
    vector<T1> data;
    vector<T2> lazy;//莨晄成繝・・繧ソ
    vector<int> p;//莨晄成縺ョ繝輔Λ繧ーW
    /*
     * n:鬆らせ謨ー
     * f1:隕∫エ蜷悟」ォ縺ョ貍皮ョ・f2:菴懃畑邏蜷悟」ォ,f3:菴懃畑
     * e:蜊倅ス榊・
     */
    lazysegtree(int _n,F1 f1,F2 f2,F3 f3,T1 e1,T2 e2,T2 e3):f1(f1),f2(f2),f3(f3),e1(e1),e2(e2),e3(e3){
        n = 1;
        while(n<_n) n<<=1;
        data.assign(2*n,e1);
        lazy.assign(2*n,e3);
        p.assign(2*n,0);
    }

    void set(int i,T1 x){
        data[i+n-1] = x;
    }


    void build(){
        for(int i = n - 2;i>=0;i--){
            data[i] = f1(data[2*i+1],data[2*i+2]);
        }
    }

    void rangeupdate(T2 x,int a,int b,int ni,int l,int r){
        update(ni,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            p[ni] = 1;
            lazy[ni] = f2(x,lazy[ni]);
            update(ni,l,r);
        }else{
            rangeupdate(x,a,b,2*ni+1,l,(l+r)/2);
            rangeupdate(x,a,b,2*ni+2,(l+r)/2,r);
            if(l+1<r) data[ni] = f1(data[2*ni+1],data[2*ni+2]);
        }
    }

    void rangeupdate(T2 x,int a,int b){
        rangeupdate(x,a,b,0,0,n);
    }

    void update(int ni,int l,int r){
        if(!p[ni])return;
        p[ni] = 0;
        //data[ni] = f3(lazy[ni],data[ni]);
        data[ni] = f3(lazy[ni],data[ni],l,r);
        if(l+1<r){
            p[2*ni+1] = 1;
            p[2*ni+2] = 1;
            lazy[2*ni+1] = f2(lazy[ni],lazy[2*ni+1]);
            lazy[2*ni+2] = f2(lazy[ni],lazy[2*ni+2]);
        }
        lazy[ni] = e3;
    }

    T1 query(int a,int b,int ni,int l,int r){
        update(ni,l,r);
        if(b<=l||r<=a) return e1;
        if(a<=l&&r<=b) return data[ni];
        else return f1(query(a,b,2*ni+1,l,(l+r)/2),query(a,b,2*ni+2,(r+l)/2,r));
    }

    T1 query(int a,int b){
        return query(a,b,0,0,n);
    }

    T1 operator[](const int i) const {
        return data[i+n-1];
    }
};


const int mx = 1e5 + 10;
int in[mx],out[mx],par[mx],head[mx],sz[mx];
vector<int> g[mx];
int n;
ll c1(ll a,ll b){
    return a+ b;
}
ll c2(ll a,ll b){
    return a + b;
}
ll c3(ll a,ll b,ll l,ll r){
    return a * (r-l) + b;
}

int sdfs(int ni,int p){
    sz[ni] = 1;
    par[ni] = p;
    if(g[ni].size()&&g[ni][0]==p) swap(g[ni][0],g[ni].back());
    for(auto&to:g[ni]){
        if(to==p) continue;
        sz[ni] += sdfs(to,ni);
        if(sz[to]>sz[g[ni][0]]) swap(to,g[ni][0]);
    }
    return sz[ni];
}

void hdfs(int ni,int p,int&time){
    in[ni] = time++;
    for(auto&to:g[ni]){
        if(to==p) continue;
        head[to] = (g[ni][0]==to?head[ni]:to);
        hdfs(to,ni,time);
    }
    out[ni] = time;
}

int main(){
    cin>>n;
    for(int i = 1;i<n;i++){
        int u,v;
        cin>>u>>v;
        u--;v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    lazysegtree<ll> seg(n,c1,c2,c3,0LL,0LL,0LL);
    seg.rangeupdate(1,0,n);
    sdfs(0,-1);
    int time = 0;
    hdfs(0,-1,time);
    ll ans = 0;
    int q;
    cin>>q;
    while(q--){
        //cout<<q<<endl;
        int u,v;
        cin>>u>>v;
        u--;v--;
        for(;;v=par[head[v]]){
            //cout<<u<<" "<<v<<endl;
            if(in[u]>in[v]) swap(u,v);
            if(head[u]==head[v]) break;
            ans += seg.query(in[head[v]],in[v]+1);
            seg.rangeupdate(1,in[head[v]],in[v]+1);
        }
        ans += seg.query(in[u],in[v]+1);
        seg.rangeupdate(1,in[u],in[v]+1);
    }
    cout<<ans<<endl;
}
0