結果
| 問題 |
No.650 行列木クエリ
|
| コンテスト | |
| ユーザー |
ei1333333
|
| 提出日時 | 2018-02-09 23:05:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 6,708 bytes |
| コンパイル時間 | 2,862 ms |
| コンパイル使用メモリ | 218,748 KB |
| 最終ジャッジ日時 | 2025-01-05 08:15:25 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 1 WA * 9 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:256:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
256 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
main.cpp:259:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
259 | scanf("%d %d", &X[i], &Y[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:273:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
273 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
main.cpp:276:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
276 | scanf(" %c", &x);
| ~~~~~^~~~~~~~~~~
main.cpp:279:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
279 | scanf("%d", &v);
| ~~~~~^~~~~~~~~~
main.cpp:281:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
281 | scanf("%d %d %d %d", &m.a[0][0], &m.a[0][1], &m.a[1][0], &m.a[1][1]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:286:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
286 | scanf("%d %d", &y, &z);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#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 + 1, TreeDepthB + 1);
}
CentroidPathDecomposition(int SZ)
{
graph.resize(SZ);
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
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];
scanf("%d", &N);
CentroidPathDecomposition g(N);
for(int i = 1; i < N; i++) {
scanf("%d %d", &X[i], &Y[i]);
g.AddEdge(X[i], Y[i]);
}
g.Build();
for(int i = 1; i < N; i++) {
auto p = g.Information(X[i]);
auto q = g.Information(X[i]);
if(p > q) swap(X[i], Y[i]);
}
vector< SegmentTree > segs;
for(int i = 0; i < g.size(); i++) {
segs.emplace_back(SegmentTree(g[i].size()));
}
int Q;
scanf("%d", &Q);
while(Q--) {
char x;
scanf(" %c", &x);
if(x == 'x') {
int v;
scanf("%d", &v);
Matrix m;
scanf("%d %d %d %d", &m.a[0][0], &m.a[0][1], &m.a[1][0], &m.a[1][1]);
auto p = g.Information(Y[v + 1]);
segs[p.first].update(p.second, m);
} else {
int y, z;
scanf("%d %d", &y, &z);
Matrix mat = Matrix::I();
g.query(y, z, [&](int a, int b, int c) { mat = segs[a].query(b, c) * mat; });
printf("%d %d %d %d\n", mat.a[0][0], mat.a[0][1], mat.a[1][0], mat.a[1][1]);
}
}
}
ei1333333