結果

問題 No.235 めぐるはめぐる (5)
ユーザー mamekinmamekin
提出日時 2017-06-11 11:19:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,890 bytes
コンパイル時間 1,578 ms
コンパイル使用メモリ 130,992 KB
実行使用メモリ 67,384 KB
最終ジャッジ日時 2023-10-24 20:53:24
合計ジャッジ時間 7,286 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 882 ms
67,384 KB
testcase_02 AC 1,090 ms
49,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int MOD = 1000000007;

class SegmentTree
{
public:
    typedef long long T1;
    typedef long long T2;

    // データの初期値、以下の条件を満たすこと
    //   uniteData(v, INIT_DATA) == v
    static const T1 INIT_DATA;

private:
    // 遅延の初期値、以下の条件を満たすこと
    //   updateData(prev, size, INIT_DELAY) == prev
    //   updateDelay(x, INIT_DELAY) == x
    static const T2 INIT_DELAY;

    // 長さがlenの区間における前回の計算結果prevに対して、
    // 区間の各要素にパラメータxを用いた更新処理を適用した後の計算結果を返す
    T1 updateData(T1 prev, int len, T2 x, T1 c){
        return (prev + x * c) % MOD;
    }
    // パラメータx1,x2による2つの更新処理を順に適用した場合に対して、
    // 同じ結果になるような1つの更新処理のパラメータを返す
    T2 updateDelay(T2 x1, T2 x2){
        return (x1 + x2) % MOD;
    }
    // 2つの区間の計算結果v1,v2に対して、
    // その2つの区間を統合した区間における計算結果を返す
    T1 uniteData(T1 v1, T1 v2){
        return (v1 + v2) % MOD;
    }

    int n;
    vector<T1> data;
    vector<T2> delay;
    vector<T1> coef;
    void updateTree(int a, int b, int k, int l, int r, T2 x){
        if(a <= l && r <= b){
            data[k] = updateData(data[k], r - l + 1, x, coef[k]);
            delay[k] = updateDelay(delay[k], x);
        }
        else if(a <= r && l <= b){
            int len = (r - l + 1) / 2;
            for(int i=0; i<2; ++i){
                data[k*2+1+i] = updateData(data[k*2+1+i], len, delay[k], coef[k*2+1+i]);
                delay[k*2+1+i] = updateDelay(delay[k*2+1+i], delay[k]);
            }
            delay[k] = INIT_DELAY;
            updateTree(a, b, k*2+1, l, (l+r)/2, x);
            updateTree(a, b, k*2+2, (l+r+1)/2, r, x);
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
        }
    }
    T1 getValue(int a, int b, int k, int l, int r){
        if(a <= l && r <= b){
            return data[k];
        }
        else if(a <= r && l <= b){
            int len = (r - l + 1) / 2;
            for(int i=0; i<2; ++i){
                data[k*2+1+i] = updateData(data[k*2+1+i], len, delay[k], coef[k*2+1+i]);
                delay[k*2+1+i] = updateDelay(delay[k*2+1+i], delay[k]);
            }
            delay[k] = INIT_DELAY;
            T1 v1 = getValue(a, b, k*2+1, l, (l+r)/2);
            T1 v2 = getValue(a, b, k*2+2, (l+r+1)/2, r);
            return uniteData(v1, v2);
        }
        else{
            return INIT_DATA;
        }
    }

public:
    SegmentTree(const vector<T1>& v, const vector<T1>& coef0)
    {
        int n0 = v.size();
        n = 1;
        while(n < n0)
            n *= 2;
        data.assign(2*n-1, INIT_DATA);
        delay.assign(2*n-1, INIT_DELAY);
        coef.resize(2*n-1);
        for(int i=0; i<n; ++i){
            data[n-1+i] = v[i];
            coef[n-1+i] = coef0[i];
        }
        for(int k=n-2; k>=0; --k){
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
            coef[k] = coef[k*2+1] + coef[k*2+2];
        }
    }
    // 区間[a,b]の要素にパラメータxによる更新処理を適用
    void update(int a, int b, T2 x){
        updateTree(a, b, 0, 0, n-1, x);
    }
    // 区間[a,b]の計算結果を返す
    T1 get(int a, int b){
        return getValue(a, b, 0, 0, n-1);
    }
    T1 get(int a, int b, T1 leftPrev, T1 rightPrev){
        return uniteData(uniteData(leftPrev, get(a, b)), rightPrev);
    }
};
const SegmentTree::T1 SegmentTree::INIT_DATA = 0;
const SegmentTree::T2 SegmentTree::INIT_DELAY = 0;

class HeavyLightDecomposition
{
private:
    vector<SegmentTree> st;
    typedef SegmentTree::T1 T1;
    typedef SegmentTree::T2 T2;

    // 新たにデータ構造のインスタンスを追加する
    void addDataStructure(const vector<T1>& init, const vector<T1>& coef){
        st.push_back(SegmentTree(init, coef));
    }
    // データ構造の更新処理
    void updateDataStructure(int i, int a, int b, T2 x){
        st[i].update(a, b, x);
    }
    // データ構造のデータ取得
    T1 getDataStructure(int i, int a, int b, T1 leftPrev, T1 rightPrev){
        return st[i].get(a, b, leftPrev, rightPrev);
    }
    // 左右に区別があるデータ形式に対して、左右を入れ替えた結果を返す
    T1 reverseData(T1 x){
        return x;
    }

    vector<int> depth;
    vector<int> parent;
    vector<vector<int> > node;
    vector<pair<int, int> > index;

    pair<int, int> dfs(const vector<vector<int> >& edges, int curr, int prev)
    {
        if(prev != -1){
            depth[curr] = depth[prev] + 1;
            parent[curr] = prev;
        }

        int sum = 1;
        pair<int, int> p(-1, -1);
        for(int next : edges[curr]){
            if(next == prev)
                continue;
            pair<int, int> p2 = dfs(edges, next, curr);
            sum += p2.first;
            p = max(p, p2);
        }

        if(p.first == -1){
            p.second = node.size();
            node.resize(p.second + 1);
        }
        node[p.second].push_back(curr);
        return make_pair(sum, p.second);
    }
public:
    HeavyLightDecomposition(const vector<vector<int> >& edges, int root, const vector<T1>& init, const vector<T1>& coef)
    {
        int n = edges.size();
        depth.assign(n, 0);
        parent.assign(n, -1);
        dfs(edges, root, -1);
        index.resize(n);
        for(unsigned i=0; i<node.size(); ++i){
            vector<T1> v(node[i].size());
            vector<T1> c(node[i].size());
            for(unsigned j=0; j<node[i].size(); ++j){
                index[node[i][j]] = make_pair(i, j);
                v[j] = init[node[i][j]];
                c[j] = coef[node[i][j]];
            }
            addDataStructure(v, c);
        }
    }
    void update(int a, int b, T2 x){
        while(index[a].first != index[b].first){
            int a2 = node[index[a].first].back();
            int b2 = node[index[b].first].back();
            if(depth[a2] < depth[b2]){
                swap(a, b);
                swap(a2, b2);
            }
            updateDataStructure(index[a].first, index[a].second, index[a2].second, x);
            a = parent[a2];
        }
        if(depth[a] < depth[b])
            swap(a, b);
        updateDataStructure(index[a].first, index[a].second, index[b].second, x);
    }
    T1 get(int a, int b){
        T1 leftPrev  = SegmentTree::INIT_DATA;
        T1 rightPrev = SegmentTree::INIT_DATA;
        while(index[a].first != index[b].first){
            int a2 = node[index[a].first].back();
            int b2 = node[index[b].first].back();
            if(depth[a2] > depth[b2]){
                leftPrev = getDataStructure(index[a].first, index[a].second, index[a2].second,
                                            leftPrev, SegmentTree::INIT_DATA);
                a = parent[a2];
            }
            else{
                rightPrev = getDataStructure(index[b].first, index[b].second, index[b2].second,
                                             rightPrev, SegmentTree::INIT_DATA);
                b = parent[b2];
            }
        }
        if(depth[a] > depth[b]){
            return getDataStructure(index[a].first, index[a].second, index[b].second,
                                    leftPrev, reverseData(rightPrev));
        }
        else{
            T1 ans = getDataStructure(index[a].first, index[b].second, index[a].second,
                                      rightPrev, reverseData(leftPrev));
            return reverseData(ans);
        }
    }
};

int main()
{
    int n;
    cin >> n;
    vector<long long> s(n), c(n);
    for(int i=0; i<n; ++i)
        cin >> s[i];
    for(int i=0; i<n; ++i)
        cin >> c[i];

    vector<vector<int> > edges(n);
    for(int i=0; i<n-1; ++i){
        int a, b;
        cin >> a >> b;
        -- a;
        -- b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }

    HeavyLightDecomposition hl(edges, 0, s, c);
    int q;
    cin >> q;
    while(--q >= 0){
        int type, x, y;
        cin >> type >> x >> y;
        -- x;
        -- y;
        if(type == 0){
            int z;
            cin >> z;
            hl.update(x, y, z);
        }
        else{
            cout << hl.get(x, y) << endl;
        }
    }

    return 0;
}
0