結果

問題 No.650 行列木クエリ
ユーザー ei1333333ei1333333
提出日時 2018-02-09 22:51:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 7,663 bytes
コンパイル時間 2,807 ms
コンパイル使用メモリ 218,572 KB
最終ジャッジ日時 2025-01-05 08:14:47
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
using namespace std;
struct CentroidPathDecomposition
{
struct Centroid
{
int ParIndex, ParDepth, Deep;
vector< int > node;
Centroid(int idx, int dep, int deep) : ParIndex(idx), ParDepth(dep), Deep(deep) {}
inline size_t size()
{
return (node.size());
}
inline int &operator[](int k)
{
return (node[k]);
}
inline pair< int, int > Up()
{
return (make_pair(ParIndex, ParDepth));
}
};
vector< vector< int > > graph;
vector< int > SubTreeSize, NextPath;
vector< int > TreeIndex, TreeDepth;
vector< Centroid > Centroids;
void BuildSubTreeSize()
{
stack< pair< int, int > > s;
s.emplace(0, -1);
while(!s.empty()) {
auto p = s.top();
s.pop();
if(~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
SubTreeSize[p.first] += SubTreeSize[to];
if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for(auto &to : graph[p.first]) {
if(p.second != to) s.emplace(to, p.first);
}
}
}
}
void BuildPath()
{
stack< pair< int, int > > s;
Centroids.emplace_back(-1, -1, 0);
s.emplace(0, -1);
TreeIndex[0] = 0;
while(!s.empty()) {
auto p = s.top();
s.pop();
TreeDepth[p.first] = (int) Centroids[TreeIndex[p.first]].size();
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
if(to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = (int) Centroids.size();
Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1);
}
s.emplace(to, p.first);
}
Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
}
}
void AddEdge(int x, int y)
{
graph[x].push_back(y);
graph[y].push_back(x);
}
virtual void Build()
{
BuildSubTreeSize();
BuildPath();
}
inline size_t size()
{
return (Centroids.size());
}
inline pair< int, int > Information(int idx)
{
return (make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k)
{
return (Centroids[k]);
}
inline int LCA(int a, int b)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
return (Centroids[TreeIdxA][TreeDepthA]);
}
inline virtual void query(int a, int b, const function< void(int, int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(TreeIdxA, 0, TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(TreeIdxB, 0, TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
}
CentroidPathDecomposition(int SZ)
{
graph.resize(SZ);
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
struct TreeArray : CentroidPathDecomposition
{
TreeArray(int sz) : CentroidPathDecomposition(sz) {}
vector< int > index;
void Build()
{
CentroidPathDecomposition::Build();
int ptr = 0;
for(auto &centroid : Centroids) {
index.emplace_back(ptr);
ptr += centroid.size();
}
}
inline int get(int a)
{
auto p = Information(a);
return (index[p.first] + p.second);
}
inline void query(int a, int b, const function< void(int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(index[TreeIdxA] + TreeDepthA + 1, index[TreeIdxA] + TreeDepthB + 1);
}
};
const int mod = 1e9 + 7;
struct Matrix
{
int a[2][2];
Matrix operator+(const Matrix &kj)
{
Matrix ret;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
ret.a[i][j] = a[i][j] + kj.a[i][j];
ret.a[i][j] %= mod;
}
}
return (ret);
}
Matrix operator*(const Matrix &kj)
{
Matrix ret = Matrix::Zero();
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
ret.a[i][j] = (ret.a[i][j] + 1LL * a[i][k] * kj.a[k][j]) % mod;
}
}
}
return (ret);
}
Matrix operator^(int n)
{
Matrix ret = Matrix::I();
Matrix x = *this;
while(n > 0) {
if(n & 1) (ret = ret * x);
x = x * x;
n >>= 1;
}
return (ret);
}
static Matrix Zero()
{
Matrix ret;
memset(ret.a, 0, sizeof(ret.a));
return (ret);
}
static Matrix I()
{
Matrix ret;
memset(ret.a, 0, sizeof(ret.a));
for(int i = 0; i < 2; i++) ret.a[i][i] = 1;
return (ret);
}
};
struct SegmentTree
{
vector< Matrix > seg, add;
int sz;
SegmentTree(int n)
{
sz = 1;
while(sz < n) sz <<= 1;
seg.assign(2 * sz - 1, Matrix::I());
}
Matrix query(int a, int b, int k, int l, int r)
{
if(a >= r || b <= l) return (Matrix::I());
if(a <= l && r <= b) return (seg[k]);
Matrix L = query(a, b, 2 * k + 1, l, (l + r) >> 1);
Matrix R = query(a, b, 2 * k + 2, (l + r) >> 1, r);
return (L * R);
}
void update(int k, Matrix x)
{
k += sz - 1;
seg[k] = x;
while(k > 0) {
k = (k - 1) >> 1;
seg[k] = seg[2 * k + 1] * seg[2 * k + 2];
}
}
Matrix query(int a, int b)
{
return (query(a, b, 0, 0, sz));
}
};
int main()
{
int N, X[100000], Y[100000];
cin >> N;
TreeArray g(N);
for(int i = 1; i < N; i++) {
cin >> X[i] >> Y[i];
g.AddEdge(X[i], Y[i]);
}
g.Build();
for(int i = 1; i < N; i++) {
int a = g.get(X[i]);
int b = g.get(Y[i]);
if(a > b) swap(X[i], Y[i]);
}
SegmentTree seg(N);
int Q;
cin >> Q;
while(Q--) {
char x;
cin >> x;
if(x == 'x') {
int v;
cin >> v;
++v;
Matrix m;
cin >> m.a[0][0] >> m.a[0][1] >> m.a[1][0] >> m.a[1][1];
seg.update(g.get(Y[v]), m);
} else {
int y, z;
cin >> y >> z;
Matrix mat = Matrix::I();
g.query(y, z, [&](int a, int b)
{
mat = seg.query(a, b) * mat;
});
cout << mat.a[0][0] << " " << mat.a[0][1] << " " << mat.a[1][0] << " " << mat.a[1][1] << endl;
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0