이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double;
const string FILENAME = "input";
const int MAXN = 200228;
const int LOG = 20;
const int MAXD = 40 + 7;
int n, l, q;
int a[MAXN];
vector<int> g[MAXN];
int anc[LOG][MAXN], mul[MAXN][MAXD];
void build(int u, int p = 0) {
anc[0][u] = p;
for (int i = 1; i < LOG; i++) {
anc[i][u] = anc[i - 1][anc[i - 1][u]];
}
for (auto h: g[u]) {
if (h != p) {
build(h, u);
}
}
}
void change(int x, int d, int w) {
if (x == 0 || d == -1) {
return;
}
change(anc[0][x], d - 1, w);
if (x == 1) {
for (int i = 0; i <= d; i++) {
mul[x][i] = 1LL * mul[x][i] * w % l;
}
} else {
mul[x][d] = 1LL * mul[x][d] * w % l;
if (d != 0) {
mul[x][d - 1] = 1LL * mul[x][d - 1] * w % l;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//read(FILENAME);
cin >> n >> l;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
build(1);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 40; j++) {
mul[i][j] = 1;
}
}
cin >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int x, d, w;
cin >> x >> d >> w;
change(x, d, w);
} else {
int x;
cin >> x;
int res = 1;
int u = x;
for (int j = 0; j <= 40; j++) {
res = (1LL * res * mul[u][j]) % l;
u = anc[0][u];
}
cout << 1LL * a[x] * res % l << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |