結果

問題 No.650 行列木クエリ
ユーザー parukiparuki
提出日時 2018-02-09 23:22:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 6,146 bytes
コンパイル時間 2,953 ms
コンパイル使用メモリ 196,360 KB
実行使用メモリ 50,448 KB
最終ジャッジ日時 2024-04-17 15:04:13
合計ジャッジ時間 4,301 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 162 ms
14,592 KB
testcase_02 AC 294 ms
50,348 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 169 ms
14,592 KB
testcase_05 AC 309 ms
50,448 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 131 ms
14,592 KB
testcase_09 AC 254 ms
50,336 KB
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
#define vv(a,b,c,d) vector<vector<a> >(b,vector<a>(c,d))
#define vvv(a,b,c,d,e) vector<vector<vector<a> > >(b,vv(a,c,d,e))
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

const int MOD = (int)1e9 + 7;

vector<vector<int> > mulMatMod(const vector<vector<int> > &A, const vector<vector<int> > &B) {
    auto n = A.size(), m = A[0].size(), p = B[0].size();
    vector<vector<int> > res(n, vector<int>(p));
    for (int i = 0; i < n; ++i)for (int j = 0; j < p; ++j) for (int k = 0; k < m; ++k)
        res[i][j] = (int)((res[i][j] + (long long)A[i][k] * B[k][j]) % MOD);
    return res;
}

typedef vector<vi> mat;

/*
行列の部分積
を求める。
*/
class PartialMatMul {
    int n;
    vector<vector<int> > E;
    vector<vector<vector<int> >> dat;

    vector<vector<int> > query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return E;
        if (a <= l&&r <= b) return dat[k];
        else {
            vector<vector<int> > vl, vr;
            vl = query(a, b, k << 1, l, (l + r) >> 1);
            vr = query(a, b, (k << 1) | 1, (l + r) >> 1, r);
            return mulMatMod(vl, vr);
        }
    }

public:
    PartialMatMul() {}

    PartialMatMul(int segSize, int matSize) :n(1) {
        for (; n<segSize; n <<= 1);
        E = vector<vector<int> >(matSize, vector<int>(matSize));
        for (int i = 0; i < matSize; ++i)E[i][i] = 1;
        dat = vector<vector<vector<int> >>(n << 1, E);
    }

    void update(int k, vector<vector<int> > a) {
        k += n;
        dat[k] = a;
        while (k>1) {
            k >>= 1;
            // dat[k] = mulMatMod(dat[(k << 1) | 1], dat[k << 1]);
            dat[k] = mulMatMod(dat[k << 1], dat[(k << 1) | 1]);
        }
    }

    vector<vector<int> > query(int a, int b) {
        return query(a, b, 1, 0, n);
    }
};

class HLDecomposition {
    int cur = 0;
    void bfs(const vector<vector<int> > &G) {
        queue<tuple<int, int, int> > que;
        vector<int> order;
        que.push(make_tuple(0, -1, 0));
        while (!que.empty()) {
            int u, pre, d;
            tie(u, pre, d) = que.front();
            que.pop();
            parent[u] = pre;
            depth[u] = d;
            order.push_back(u);
            for (int v : G[u])if (v != pre) {
                que.push(make_tuple(v, u, d + 1));
            }
        }
        reverse(order.begin(), order.end());
        for (int u : order) {
            sub[u] = 1;
            for (int v : G[u])if (v != parent[u])sub[u] += sub[v];
        }
    }

    void dfs_stk(const vector<vector<int> > & G) {
        stack<pair<int, int> > stk;
        stk.push(make_pair(0, 0));
        while (!stk.empty()) {
            int u, h, pre;
            tie(u, h) = stk.top();
            stk.pop();
            head[u] = h;
            id[u] = cur++;
            pre = parent[u];
            int heavy = -1, maxi = 0;
            for (int v : G[u]) {
                if (v != pre && maxi < sub[v]) {
                    maxi = sub[v];
                    heavy = v;
                }
            }
            for (int v : G[u]) {
                if (v != pre&&v != heavy) {
                    stk.push(make_pair(v, v));
                }
            }
            if (heavy != -1)stk.push(make_pair(heavy, h));
        }
    }
public:
    vector<int> parent, depth, sub, id, head;

    HLDecomposition(const vector<vector<int> > &G) {
        parent = depth = sub = id = head = vector<int>(G.size());
        bfs(G);
        dfs_stk(G);
    }

    /*
    パス(u, v)で通る頂点を半開区間の集合に変換する。
    O(log(|V|))
    */
    vector<pair<int, int> > goUpToLCA(int u, int v) {
        vector<pair<int, int> > res;
        while (true) {
            if (head[u] == head[v]) {
                if (depth[u] > depth[v])swap(u, v);
                res.emplace_back(id[u], id[v] + 1);
                break;
            } else {
                if (depth[head[u]] > depth[head[v]])swap(u, v);
                res.emplace_back(id[head[v]], id[v] + 1);
                v = parent[head[v]];
            }
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int n;
    cin >> n;
    vector<vi> G(n);
    vi U(n - 1), V(n - 1);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
        U[i] = a;
        V[i] = b;
    }

    HLDecomposition hld(G);
    rep(i, n - 1) {
        if (hld.depth[U[i]] > hld.depth[V[i]])swap(U[i], V[i]);
    }
    // depth[U[i]] < depth[V[i]]

    PartialMatMul pmm(n, 2);
    int q;
    cin >> q;
    vector<vi> A(2, vi(2));
    vector<vi> E(2, vi(2));
    rep(i, 2)E[i][i] = 1;
    rep(ITER, q) {
        char c;
        cin >> c;
        if (c == 'x') {
            int i;
            cin >> i;
            rep(j, 2)rep(k, 2)cin >> A[j][k];
            pmm.update(hld.id[V[i]], A);
        } else if (c == 'g') {
            int i, j;
            cin >> i >> j; // depth[i] < depth[j]
            auto p = hld.goUpToLCA(i, j);
            reverse(all(p));
            auto X = E;
            rep(k, sz(p)) {
                if (k == 0) {
                    if (p[k].second - p[k].first > 1) {
                        X = mulMatMod(X, pmm.query(p[k].first + 1, p[k].second));
                    }
                } else {
                    X = mulMatMod(X, pmm.query(p[k].first, p[k].second));
                }
            }
            rep(k, 2)rep(l, 2) {
                cout << X[k][l] << (k == 1 && l == 1 ? '\n' : ' ');
            }
        }
    }
}
0