結果

問題 No.650 行列木クエリ
ユーザー mamekinmamekin
提出日時 2018-03-18 21:52:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 7,449 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 131,900 KB
実行使用メモリ 69,512 KB
最終ジャッジ日時 2023-08-28 18:05:59
合計ジャッジ時間 3,621 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 66 ms
12,640 KB
testcase_02 AC 187 ms
50,808 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 67 ms
12,844 KB
testcase_05 AC 184 ms
50,708 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 74 ms
17,984 KB
testcase_09 AC 165 ms
69,512 KB
testcase_10 AC 1 ms
4,380 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 Data
{
public:
    // [ a b ]
    // [ c d ]
    long long a, b, c, d;
    Data(){
        a = d = 1;
        b = c = 0;
    }
    Data(long long a, long long b, long long c, long long d){
        this->a = a;
        this->b = b;
        this->c = c;
        this->d = d;
    }
    Data operator*(const Data& other) const{
        Data ret;
        ret.a = (a * other.a + b * other.c) % MOD;
        ret.b = (a * other.b + b * other.d) % MOD;
        ret.c = (c * other.a + d * other.c) % MOD;
        ret.d = (c * other.b + d * other.d) % MOD;
        return ret;
    }
};

// セグメント木
class SegmentTree
{
public:
    typedef Data T1;
    typedef Data T2;

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

    // 2つの区間の計算結果v1,v2に対して、
    // その2つの区間を統合した区間における計算結果を返す
    static T1 uniteData(T1 v1, T1 v2){
        return v1 * v2;
    }

private:
    // 前回の値がprevである要素に対して、
    // パラメータxを用いた更新処理を適用した後の計算結果を返す
    T1 updateData(T1 prev, T2 x){
        return x;
    }

    int n;
    vector<T1> data;
    void updateTree(int a, int k, int l, int r, T2 x){
        if(a == l && a == r){
            data[k] = updateData(data[k], x);
        }
        else if(l <= a && a <= r){
            updateTree(a, k*2+1, l, (l+r)/2, x);
            updateTree(a, 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){
            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(int n0){
        n = 1;
        while(n < n0)
            n *= 2;
        data.assign(2*n-1, INIT_DATA);
    }
    // a番目の要素にパラメータxによる更新処理を適用
    void update(int a, T2 x){
        updateTree(a, 0, 0, n-1, x);
    }
    // 区間[a,b]の計算結果を返す
    T1 get(int a, int b){
        return getValue(a, b, 0, 0, n-1);
    }
};
const SegmentTree::T1 SegmentTree::INIT_DATA = Data();

// HL分解
class HeavyLightDecomposition
{
private:
    typedef SegmentTree DataStructure;
    typedef DataStructure::T1 T1;
    typedef DataStructure::T2 T2;
    static const T1 INIT_DATA;

    // 長さnの配列に対するデータ構造を生成
    DataStructure makeDataStructure(int n){
        return DataStructure(n);
    }
    // データ構造の更新処理
    void updateDataStructure(DataStructure& ds, int a, T2 x){
        ds.update(a, x);
    }
    // データ構造のデータ取得
    T1 getValFromDataStructure(DataStructure& ds, int a, int b, T1 leftPrev, T1 rightPrev){
        T1 ans = ds.get(a, b);
        ans = DataStructure::uniteData(leftPrev, ans);
        ans = DataStructure::uniteData(ans, rightPrev);
        return ans;
    }

    vector<DataStructure> dsList;
    vector<DataStructure> dsRevList;
    vector<int> depth;
    vector<int> parent;
    vector<vector<int> > node;
    vector<pair<int, int> > index;

    // 深さ優先探索によりHL分解を実行
    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)
    {
        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){
            for(unsigned j=0; j<node[i].size(); ++j)
                index[node[i][j]] = make_pair(i, j);
            dsList.push_back(makeDataStructure(node[i].size()));
        }
        dsRevList = dsList;
    }
    // ノードaに対して、パラメータxによる更新を適用
    void update(int a, T2 x){
        int n = node[index[a].first].size();
        updateDataStructure(dsList[index[a].first], index[a].second, x);
        updateDataStructure(dsRevList[index[a].first], n-1-index[a].second, x);
    }
    // a,b間のパスの結果を取得
    T1 get(int a, int b){
        T1 leftPrev  = INIT_DATA;
        T1 rightPrev = 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 = getValFromDataStructure(dsList[index[a].first], index[a].second, index[a2].second, leftPrev, INIT_DATA);
                a = parent[a2];
            }
            else{
                int n = node[index[b].first].size();
                rightPrev = getValFromDataStructure(dsRevList[index[b].first], n-1-index[b2].second, n-1-index[b].second, INIT_DATA, rightPrev);
                b = parent[b2];
            }
        }

        if(depth[a] > depth[b]){
            return getValFromDataStructure(dsList[index[a].first], index[a].second, index[b].second, leftPrev, rightPrev);
        }
        else{
            int n = node[index[a].first].size();
            return getValFromDataStructure(dsRevList[index[a].first], n-1-index[a].second, n-1-index[b].second, leftPrev, rightPrev);
        }
    }
};
const HeavyLightDecomposition::T1 HeavyLightDecomposition::INIT_DATA = SegmentTree::INIT_DATA;

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

    int q;
    cin >> q;
    while(--q >= 0){
        char ope;
        int i;
        cin >> ope >> i;

        if(ope == 'x'){
            Data x;
            cin >> x.a >> x.b >> x.c >> x.d;
            hl.update(n+i, x);
        }
        else{
            int j;
            cin >> j;
            Data ans = hl.get(i, j);
            cout << ans.a << ' ' << ans.b << ' ' << ans.c << ' ' << ans.d << endl;
        }
    }

    return 0;
}
0