This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define D 40
#define N_ (N + D)
int *ej[N_], eo[N_];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int pp[N_];
void dfs(int p, int i) {
int o;
pp[i] = p;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs(i, j);
}
}
int main() {
static int aa[N_][D + 1];
int n, q, md, h, i, j, d;
scanf("%d%d", &n, &md);
for (i = 0; i < n + D; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
for (h = 0; h + 1 < D; h++)
append(n + h, n + h + 1), append(n + h + 1, n + h);
append(n + D - 1, 0), append(0, n + D - 1);
dfs(-1, n);
for (i = 0; i < n + D; i++)
for (d = 0; d <= D; d++)
aa[i][d] = 1;
for (i = 0; i < n; i++)
scanf("%d", &aa[i][0]);
scanf("%d", &q);
while (q--) {
int t, ans;
scanf("%d%d", &t, &i), i--;
if (t == 1) {
int x;
scanf("%d%d", &d, &x);
while (d >= 0) {
aa[i][d] = (long long) aa[i][d] * x % md;
if (d > 0)
aa[i][d - 1] = (long long) aa[i][d - 1] * x % md;
i = pp[i], d--;
}
} else {
ans = 1;
for (d = 0; d <= D; d++, i = pp[i])
ans = (long long) ans * aa[i][d] % md;
printf("%d\n", ans);
}
}
return 0;
}
Compilation message (stderr)
sprinkler.c: In function 'append':
sprinkler.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
sprinkler.c: In function 'main':
sprinkler.c:36:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d%d", &n, &md);
| ^~~~~~~~~~~~~~~~~~~~~~
sprinkler.c:40:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
sprinkler.c:51:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d", &aa[i][0]);
| ^~~~~~~~~~~~~~~~~~~~~~
sprinkler.c:52:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
sprinkler.c:56:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d", &t, &i), i--;
| ^~~~~~~~~~~~~~~~~~~~~
sprinkler.c:60:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d", &d, &x);
| ^~~~~~~~~~~~~~~~~~~~~
# | 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... |