結果
| 問題 |
No.650 行列木クエリ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-11 20:58:13 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 11,731 bytes |
| コンパイル時間 | 4,102 ms |
| コンパイル使用メモリ | 229,916 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-25 07:12:02 |
| 合計ジャッジ時間 | 6,893 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 1 |
| other | RE * 10 |
ソースコード
//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountll((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzll(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)
#define mp(x, y) make_pair(x, y)
constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18;
inline void Yes(bool condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int x, y; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class itr> void array_output(itr start, itr goal){ for(auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); cout << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(b) return gcd(b, a % b); else return a; }
template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r){ if(r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q){ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }
template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b){ if(b < a){ a = b; return 1; } return 0; }
inline int at(lint i, int j){ return (i >> j) & 1; }
random_device rnd;
bool is_in_board(lint y, lint x, lint H, lint W){ return (0 <= y && y < H && 0 <= x && x < W); }
lint inv2 = power(2, mod - 2, mod);
struct io_init {
io_init() {
cin.tie(nullptr); cout.tie(nullptr);
std::ios::sync_with_stdio(false);
}
} io_init;
template<class T>
struct Matrix{
int h, w;
vector<vector<T>> data;
Matrix(){ Matrix(1); };
Matrix(int n) : h(n), w(n), data(n, vector<T>(n)){};
Matrix(int h, int w) : h(h), w(w), data(h, vector<T>(w)){};
Matrix operator +(Matrix a){ return Matrix(*this) += a; }
Matrix operator -(Matrix a){ return Matrix(*this) -= a; }
Matrix operator *(Matrix a){ return Matrix(*this) *= a; }
Matrix& operator +=(Matrix a){
rep(i, h) rep(j, w){
data[i][j] += a[i][j];
}
return *this;
}
Matrix& operator -=(Matrix a){
rep(i, h) rep(j, w){
data[i][j] -= a[i][j];
}
return *this;
}
Matrix& operator *=(Matrix a){
Matrix ans(h, a.w);
rep(i, h) rep(j, a.w){
ans[i][j] = 0;
rep(k, w){
ans[i][j] += data[i][k] * a[k][j];
}
}
(*this) = ans;
return *this;
}
vector<T>& operator [](int k){
return data[k];
}
Matrix pow(lint exp){
Matrix ans(1), powed = (*this);
while(exp){
if(exp % 2) ans *= powed;
powed *= powed;
exp /= 2;
}
return ans;
}
};
struct Modint{
lint x;
Modint(): x(0) {}
Modint(lint x): x(x >= 0 || x % mod == 0 ? x % mod : mod - (-x) % mod) {}
Modint operator +(Modint a){ return Modint(*this) += a; }
Modint operator -(Modint a){ return Modint(*this) -= a; }
Modint operator *(Modint a){ return Modint(*this) *= a; }
Modint operator /(Modint a){ return Modint(*this) /= a; }
Modint operator -(){ return Modint(0) - Modint(*this); }
Modint& operator +=(Modint a){
x += a.x;
if(x >= mod) x -= mod;
return *this;
}
Modint& operator -=(Modint a){
if(x < a.x) x += mod;
x -= a.x;
return *this;
}
Modint& operator *=(Modint a){
x = x * a.x % mod;
return *this;
}
Modint operator /=(Modint a){
(*this) *= a.inv();
return *this;
}
Modint inv(){
return pow(mod - 2);
}
Modint pow(lint exp){
Modint ans(1), powed = (*this);
while(exp){
if(exp % 2) ans *= powed;
powed *= powed;
exp /= 2;
}
return ans;
}
bool operator ==(Modint a){ return x == a.x; }
bool operator !=(Modint a){ return x != a.x; }
};
ostream& operator <<(ostream& os, Modint a){
os << a.x;
return os;
}
istream &operator >>(istream &is, Modint& a){
lint x;
is >> x;
a = Modint(x);
return is;
}
template<class T>
struct SegmentTree{
using F = function<T(T, T)>;
int sz;
vector<T> data;
F f;
T id;
SegmentTree(): sz(1), f([](T a, T b){ return a + b; }), data(2, id) {}
SegmentTree(int n, F f, T id): f(f), id(id) {
sz = 1;
while(sz < n) sz *= 2;
data.resize(sz * 2, id);
}
void update(int k, T x){
k += sz;
data[k] = x;
while(k > 1){
k /= 2;
data[k] = f(data[k * 2], data[k * 2 + 1]);
}
}
T query(int a, int b, int l, int r, int at){
if(a <= l && r <= b){
return data[at];
}else if(r <= a || b <= l){
return id;
}
return f(query(a, b, l, (l + r) / 2, at * 2), query(a, b, (l + r) / 2, r, at * 2 + 1));
}
T query(int a, int b){
return query(a, b, 0, sz, 1);
}
T operator [](int k){
return data[k + sz];
}
};
template<class T, class Op = T>
struct HLD{
using F = function<T(T, T)>;
F f;
T id;
int sz;
SegmentTree<T> seg, seg_rev;
vector<vector<int>> graph;
vector<vector<int>> child;
vector<int> size, depth, par;
vector<int> in, head;
vector<int> tour, tour_id;
SegmentTree<int> rmq;
HLD(int n, F f, T id): sz(n), f(f), id(id), graph(n), child(n), size(n), depth(n), par(n), in(n), head(n), tour_id(n) {
seg = SegmentTree<T>(n, f, id);
seg_rev = SegmentTree<T>(n, f, id);
}
T seg_rev_query(int a, int b){ return seg_rev.query(sz - b, sz - a); }
void add_edge(int a, int b){
graph[a].push_back(b);
graph[b].push_back(a);
}
void dfs_sz(int at, int back){
size[at] = 1;
par[at] = back;
for(int to: graph[at]){
if(to == back) continue;
depth[to] = depth[at] + 1;
dfs_sz(to, at);
child[at].push_back(to);
size[at] += size[to];
}
}
void dfs_hl(int at, int par, int &num){
in[at] = num;
num++;
if(child[at].empty()) return;
for(int &c: child[at]){
if(size[c] > size[child[at][0]]){
swap(c, child[at][0]);
}
}
int heavy = child[at][0];
for(int c: child[at]){
if(c == heavy){
head[c] = head[at];
}else{
head[c] = c;
}
dfs_hl(c, at, num);
}
}
void dfs_lca(int at, int back){
tour_id[at] = int(tour.size());
tour.push_back(at);
for(int to: graph[at]){
if(to == back) continue;
dfs_lca(to, at);
tour.push_back(at);
}
}
void build(){
depth.resize(sz);
depth[0] = 0;
dfs_sz(0, -1);
head[0] = 0;
int tmp = 0;
dfs_hl(0, -1, tmp);
tour.clear();
dfs_lca(0, -1);
depth.push_back(INT_MAX);
auto f = [&](int a, int b){ if(depth[a] < depth[b]) return a; else return b; };
rmq = SegmentTree<int>(int(tour.size()), f, sz);
rep(i, tour.size()){
rmq.update(i, tour[i]);
}
}
// s -> t, [s, t)
T query_up(int s, int t){
if(s == t) return id;
T ans = id;
while(depth[head[s]] > depth[t]){
int top = head[s];
ans = f(ans, seg_rev_query(in[top], in[s] + 1));
s = par[top];
}
ans = f(ans, seg_rev_query(in[t] + 1, in[s] + 1));
return ans;
}
// s -> t, (s, t]
T query_down(int s, int t){
if(s == t) return id;
swap(s, t);
vector<T> items;
while(depth[head[s]] > depth[t]){
int top = head[s];
items.push_back(seg.query(in[top], in[s] + 1));
s = par[top];
}
items.push_back(seg.query(in[t] + 1, in[s] + 1));
T ans = id;
repr(i, items.size()){
ans = f(ans, items[i]);
}
return ans;
}
int lca(int a, int b){
if(tour_id[a] > tour_id[b]) swap(a, b);
return rmq.query(tour_id[a], tour_id[b] + 1);
}
// s -> t, [s, t]
T query(int s, int t, bool is_edge = false){
int l = lca(s, t);
T ans = query_up(s, l);
if(!is_edge) ans = f(ans, seg.query(in[l], in[l] + 1));
ans = f(ans, query_down(l, t));
return ans;
}
void update(int k, T x){
seg.update(in[k], x);
}
};
struct edge{
int to, num;
};
vector<edge> graph[101010];
int edge_child[101010];
void dfs(int at, int back){
for(edge e: graph[at]){
if(e.to == back) continue;
edge_child[e.num] = e.to;
dfs(e.to, at);
}
}
int main(){
int n;
cin >> n;
Matrix<Modint> E(2);
E[0][0] = E[1][1] = 1;
auto f = [](Matrix<Modint> a, Matrix<Modint> b){ return a * b; };
HLD<Matrix<Modint>> g(n, f, E);
rep(i, n - 1){
int u, v;
cin >> u >> v;
graph[u].push_back({v, i});
graph[v].push_back({u, i});
g.add_edge(u, v);
}
dfs(0, -1);
g.build();
int q;
cin >> q;
rep(_, q){
char t;
cin >> t;
if(t == 'x'){
int i, a, b, c, d;
cin >> i >> a >> b >> c >> d;
Matrix<Modint> x(2);
x[0][0] = a;
x[0][1] = b;
x[1][0] = c;
x[1][1] = d;
g.update(edge_child[i], x);
}else{
int i, j;
cin >> i >> j;
auto ans = g.query(i, j);
cout << ans[0][0] << " ";
cout << ans[0][1] << " ";
cout << ans[1][0] << " ";
cout << ans[1][1] << endl;
}
}
}