#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
int n,L,q;
vector<int> g[200005],f[200005];
int t[200005];
int h[200005],tin[200005],curtin;
int v[200005],pos[200005];
int firstson[200005][45],lastson[200005][45];
void dfs(int nod)
{
tin[nod] = ++curtin;
for (auto vecin : g[nod])
{
if (vecin != t[nod])
{
t[vecin] = nod;
f[nod].push_back(vecin);
h[vecin] = 1 + h[nod];
dfs(vecin);
}
}
}
bool cmp(int x,int y)
{
if (h[x] != h[y])
return h[x] < h[y];
return tin[x] < tin[y];
}
int aint[800005];
int vkk[200005];
int bulan = 40;
void update(int nod,int l,int r,int st,int dr,int val)
{
if (r < st or dr < l)
return;
st = max(st,l);
dr = min(dr,r);
if (st == l and r == dr)
{
aint[nod] = 1ll * aint[nod] * val % L;
//cout << nod << ' ' << l << ' ' << r << ' ' << aint[nod] << endl;
return;
}
if (dr - st + 1 <= bulan)
{
for (int i = st; i <= dr; i++)
vkk[i] = 1ll * vkk[i] * val % L;
return;
}
int mij = (l + r) / 2;
update(2 * nod,l,mij,st,dr,val);
update(2 * nod + 1,mij + 1,r,st,dr,val);
}
int paint[200005];
void build(int nod,int l,int r)
{
if (l == r)
paint[l] = nod;
else
{
int mij = (l + r) / 2;
build(2 * nod,l,mij);
build(2 * nod + 1,mij + 1,r);
}
}
signed main()
{
srand(time(NULL));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> L;
for (int i = 1; i < n; i++)
{
int x,y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(rand() % n + 1);
for (int i = 1; i <= n; i++)
v[i] = i;
sort(v + 1,v + n + 1,cmp);
for (int i = 1; i <= n; i++)
pos[v[i]] = i;
for (int i = 1; i <= 4 * n; i++)
aint[i] = 1;
build(1,1,n);
for (int i = 1; i <= n; i++)
vkk[i] = 1;
for (int i = 1; i <= n; i++)
{
int xx;
cin >> xx;
update(1,1,n,pos[i],pos[i],xx);
}
for (int i = 1; i <= n; i++)
{
int nod = i;
for (int j = 0; j <= 40; j++)
{
if (firstson[nod][j] == 0 or pos[i] < pos[firstson[nod][j]])
firstson[nod][j] = i;
if (lastson[nod][j] == 0 or pos[i] > pos[lastson[nod][j]])
lastson[nod][j] = i;
nod = t[nod];
if (nod == 0)
break;
}
}
cin >> q;
for (int i = 1; i <= q; i++)
{
int tip;
cin >> tip;
if (tip == 1)
{
int x,d,w;
cin >> x >> d >> w;
vector<int> ruta;
int nod = x;
for (int j = 0; j <= min(d,h[x]); j++)
{
ruta.push_back(nod);
nod = t[nod];
}
nod = ruta.back();
ruta.pop_back();
int dist = d - (h[x] - h[nod]);
for (int j = 0; j <= dist; j++)
{
if (firstson[nod][j] == 0)
break;
int lu = pos[firstson[nod][j]],ru = pos[lastson[nod][j]];
update(1,1,n,lu,ru,w);
}
while (!ruta.empty())
{
nod = ruta.back();
ruta.pop_back();
int dist = d - (h[x] - h[nod]);
for (int j = dist - 1; j <= dist; j++)
{
if (firstson[nod][j] == 0)
break;
int lu = pos[firstson[nod][j]],ru = pos[lastson[nod][j]];
if (ru - lu + 1 <= bulan)
{
for (int jj = lu; jj <= ru; jj++)
vkk[jj] = 1ll * vkk[jj] * w % L;
}
else
update(1,1,n,lu,ru,w);
}
}
}
else
{
int x;
cin >> x;
int vjj = vkk[pos[x]];
x = paint[pos[x]];
int rsp = 1ll * aint[1] * vjj % L;
while (x != 1)
{
rsp = 1ll * rsp * aint[x] % L;
x /= 2;
}
//cout << pos[x] << ' ';
cout << rsp << '\n';
}
}
return 0;
}
/**
Rezolvarea se bazeaza pe faptul ca D mic
Dupa ce fac ordinea dfs, pun nodurile intr-un sir sortate dupa inaltime si, la egalitate, dupa tin
La un update, iau al max(h[i],D)-lea stramos al lui x, fie el y. Updatez, pe inaltimile h[y] + k pentru k de la 0 la D - (h[x] - h[y]), subarborele
Apoi, merg nod dupa nod cu y spre x, la fiecare pas updatez ultimele doua nivele la care ajunge nodul curent y
Un update este de range prod si query de point value, deci pot face cu un aib care tine chestiile modulo L
Complexitate update: D * log, query: log
Pentru a afla care sunt primul si ultimul nod de la o inaltime care sunt in subarborele meu, inaltime <= D + h[nod], pot doar sa precalculez in N*D
Complexitate: O(N * D + N * logN + Q * D * logN)
Exista o micuta problema, aib-ul nu cred ca isi face treaba deoarece L nu e prim, incercam aint si fingers crossed sa nu dea TLE
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14864 KB |
Output is correct |
4 |
Correct |
4 ms |
15196 KB |
Output is correct |
5 |
Correct |
5 ms |
15196 KB |
Output is correct |
6 |
Correct |
5 ms |
15324 KB |
Output is correct |
7 |
Correct |
6 ms |
15196 KB |
Output is correct |
8 |
Correct |
4 ms |
15196 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14932 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14680 KB |
Output is correct |
15 |
Correct |
3 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
14884 KB |
Output is correct |
17 |
Correct |
3 ms |
14684 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
3 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14888 KB |
Output is correct |
26 |
Correct |
3 ms |
14684 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
567 ms |
102840 KB |
Output is correct |
3 |
Correct |
639 ms |
99548 KB |
Output is correct |
4 |
Correct |
719 ms |
121704 KB |
Output is correct |
5 |
Correct |
592 ms |
101320 KB |
Output is correct |
6 |
Correct |
423 ms |
100176 KB |
Output is correct |
7 |
Correct |
372 ms |
100252 KB |
Output is correct |
8 |
Correct |
288 ms |
98736 KB |
Output is correct |
9 |
Correct |
626 ms |
118656 KB |
Output is correct |
10 |
Correct |
710 ms |
115128 KB |
Output is correct |
11 |
Correct |
563 ms |
102764 KB |
Output is correct |
12 |
Correct |
641 ms |
99652 KB |
Output is correct |
13 |
Correct |
384 ms |
97980 KB |
Output is correct |
14 |
Correct |
457 ms |
97548 KB |
Output is correct |
15 |
Correct |
413 ms |
98000 KB |
Output is correct |
16 |
Correct |
372 ms |
97360 KB |
Output is correct |
17 |
Correct |
440 ms |
98412 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14780 KB |
Output is correct |
20 |
Correct |
3 ms |
14680 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
567 ms |
102840 KB |
Output is correct |
3 |
Correct |
639 ms |
99548 KB |
Output is correct |
4 |
Correct |
719 ms |
121704 KB |
Output is correct |
5 |
Correct |
592 ms |
101320 KB |
Output is correct |
6 |
Correct |
423 ms |
100176 KB |
Output is correct |
7 |
Correct |
372 ms |
100252 KB |
Output is correct |
8 |
Correct |
288 ms |
98736 KB |
Output is correct |
9 |
Correct |
626 ms |
118656 KB |
Output is correct |
10 |
Correct |
710 ms |
115128 KB |
Output is correct |
11 |
Correct |
563 ms |
102764 KB |
Output is correct |
12 |
Correct |
641 ms |
99652 KB |
Output is correct |
13 |
Correct |
384 ms |
97980 KB |
Output is correct |
14 |
Correct |
457 ms |
97548 KB |
Output is correct |
15 |
Correct |
413 ms |
98000 KB |
Output is correct |
16 |
Correct |
372 ms |
97360 KB |
Output is correct |
17 |
Correct |
440 ms |
98412 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14780 KB |
Output is correct |
20 |
Correct |
3 ms |
14680 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14680 KB |
Output is correct |
24 |
Correct |
568 ms |
102752 KB |
Output is correct |
25 |
Correct |
796 ms |
99616 KB |
Output is correct |
26 |
Correct |
768 ms |
118448 KB |
Output is correct |
27 |
Correct |
625 ms |
101152 KB |
Output is correct |
28 |
Correct |
419 ms |
100344 KB |
Output is correct |
29 |
Correct |
430 ms |
100172 KB |
Output is correct |
30 |
Correct |
331 ms |
99144 KB |
Output is correct |
31 |
Correct |
649 ms |
122708 KB |
Output is correct |
32 |
Correct |
773 ms |
118920 KB |
Output is correct |
33 |
Correct |
589 ms |
102788 KB |
Output is correct |
34 |
Correct |
719 ms |
99664 KB |
Output is correct |
35 |
Correct |
3 ms |
14684 KB |
Output is correct |
36 |
Correct |
3 ms |
14684 KB |
Output is correct |
37 |
Correct |
3 ms |
14680 KB |
Output is correct |
38 |
Correct |
3 ms |
14684 KB |
Output is correct |
39 |
Correct |
3 ms |
14684 KB |
Output is correct |
40 |
Correct |
3 ms |
14684 KB |
Output is correct |
41 |
Correct |
3 ms |
14684 KB |
Output is correct |
42 |
Correct |
3 ms |
14684 KB |
Output is correct |
43 |
Correct |
3 ms |
14680 KB |
Output is correct |
44 |
Correct |
4 ms |
14680 KB |
Output is correct |
45 |
Correct |
3 ms |
14684 KB |
Output is correct |
46 |
Correct |
3 ms |
14684 KB |
Output is correct |
47 |
Correct |
3 ms |
14684 KB |
Output is correct |
48 |
Correct |
3 ms |
14684 KB |
Output is correct |
49 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
890 ms |
115804 KB |
Output is correct |
3 |
Correct |
2470 ms |
116704 KB |
Output is correct |
4 |
Correct |
1273 ms |
115092 KB |
Output is correct |
5 |
Correct |
1351 ms |
99644 KB |
Output is correct |
6 |
Correct |
1378 ms |
98992 KB |
Output is correct |
7 |
Correct |
1177 ms |
99284 KB |
Output is correct |
8 |
Correct |
315 ms |
97212 KB |
Output is correct |
9 |
Correct |
885 ms |
117288 KB |
Output is correct |
10 |
Correct |
2618 ms |
111896 KB |
Output is correct |
11 |
Correct |
919 ms |
100200 KB |
Output is correct |
12 |
Correct |
3127 ms |
99592 KB |
Output is correct |
13 |
Execution timed out |
4065 ms |
97224 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
917 ms |
120536 KB |
Output is correct |
3 |
Correct |
2515 ms |
116216 KB |
Output is correct |
4 |
Correct |
1241 ms |
116560 KB |
Output is correct |
5 |
Correct |
1408 ms |
101104 KB |
Output is correct |
6 |
Correct |
1555 ms |
100712 KB |
Output is correct |
7 |
Correct |
1376 ms |
100156 KB |
Output is correct |
8 |
Correct |
321 ms |
98628 KB |
Output is correct |
9 |
Correct |
870 ms |
117684 KB |
Output is correct |
10 |
Correct |
2607 ms |
113036 KB |
Output is correct |
11 |
Correct |
884 ms |
102920 KB |
Output is correct |
12 |
Correct |
3286 ms |
99956 KB |
Output is correct |
13 |
Execution timed out |
4026 ms |
97556 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14864 KB |
Output is correct |
4 |
Correct |
4 ms |
15196 KB |
Output is correct |
5 |
Correct |
5 ms |
15196 KB |
Output is correct |
6 |
Correct |
5 ms |
15324 KB |
Output is correct |
7 |
Correct |
6 ms |
15196 KB |
Output is correct |
8 |
Correct |
4 ms |
15196 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
3 ms |
14932 KB |
Output is correct |
13 |
Correct |
3 ms |
14684 KB |
Output is correct |
14 |
Correct |
3 ms |
14680 KB |
Output is correct |
15 |
Correct |
3 ms |
14684 KB |
Output is correct |
16 |
Correct |
3 ms |
14884 KB |
Output is correct |
17 |
Correct |
3 ms |
14684 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
3 ms |
14684 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
3 ms |
14684 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
3 ms |
14684 KB |
Output is correct |
25 |
Correct |
3 ms |
14888 KB |
Output is correct |
26 |
Correct |
3 ms |
14684 KB |
Output is correct |
27 |
Correct |
4 ms |
14684 KB |
Output is correct |
28 |
Correct |
3 ms |
14684 KB |
Output is correct |
29 |
Correct |
3 ms |
14684 KB |
Output is correct |
30 |
Correct |
567 ms |
102840 KB |
Output is correct |
31 |
Correct |
639 ms |
99548 KB |
Output is correct |
32 |
Correct |
719 ms |
121704 KB |
Output is correct |
33 |
Correct |
592 ms |
101320 KB |
Output is correct |
34 |
Correct |
423 ms |
100176 KB |
Output is correct |
35 |
Correct |
372 ms |
100252 KB |
Output is correct |
36 |
Correct |
288 ms |
98736 KB |
Output is correct |
37 |
Correct |
626 ms |
118656 KB |
Output is correct |
38 |
Correct |
710 ms |
115128 KB |
Output is correct |
39 |
Correct |
563 ms |
102764 KB |
Output is correct |
40 |
Correct |
641 ms |
99652 KB |
Output is correct |
41 |
Correct |
384 ms |
97980 KB |
Output is correct |
42 |
Correct |
457 ms |
97548 KB |
Output is correct |
43 |
Correct |
413 ms |
98000 KB |
Output is correct |
44 |
Correct |
372 ms |
97360 KB |
Output is correct |
45 |
Correct |
440 ms |
98412 KB |
Output is correct |
46 |
Correct |
3 ms |
14684 KB |
Output is correct |
47 |
Correct |
3 ms |
14780 KB |
Output is correct |
48 |
Correct |
3 ms |
14680 KB |
Output is correct |
49 |
Correct |
3 ms |
14684 KB |
Output is correct |
50 |
Correct |
3 ms |
14684 KB |
Output is correct |
51 |
Correct |
3 ms |
14680 KB |
Output is correct |
52 |
Correct |
568 ms |
102752 KB |
Output is correct |
53 |
Correct |
796 ms |
99616 KB |
Output is correct |
54 |
Correct |
768 ms |
118448 KB |
Output is correct |
55 |
Correct |
625 ms |
101152 KB |
Output is correct |
56 |
Correct |
419 ms |
100344 KB |
Output is correct |
57 |
Correct |
430 ms |
100172 KB |
Output is correct |
58 |
Correct |
331 ms |
99144 KB |
Output is correct |
59 |
Correct |
649 ms |
122708 KB |
Output is correct |
60 |
Correct |
773 ms |
118920 KB |
Output is correct |
61 |
Correct |
589 ms |
102788 KB |
Output is correct |
62 |
Correct |
719 ms |
99664 KB |
Output is correct |
63 |
Correct |
3 ms |
14684 KB |
Output is correct |
64 |
Correct |
3 ms |
14684 KB |
Output is correct |
65 |
Correct |
3 ms |
14680 KB |
Output is correct |
66 |
Correct |
3 ms |
14684 KB |
Output is correct |
67 |
Correct |
3 ms |
14684 KB |
Output is correct |
68 |
Correct |
3 ms |
14684 KB |
Output is correct |
69 |
Correct |
3 ms |
14684 KB |
Output is correct |
70 |
Correct |
3 ms |
14684 KB |
Output is correct |
71 |
Correct |
3 ms |
14680 KB |
Output is correct |
72 |
Correct |
4 ms |
14680 KB |
Output is correct |
73 |
Correct |
3 ms |
14684 KB |
Output is correct |
74 |
Correct |
3 ms |
14684 KB |
Output is correct |
75 |
Correct |
3 ms |
14684 KB |
Output is correct |
76 |
Correct |
3 ms |
14684 KB |
Output is correct |
77 |
Correct |
3 ms |
14684 KB |
Output is correct |
78 |
Correct |
3 ms |
14684 KB |
Output is correct |
79 |
Correct |
890 ms |
115804 KB |
Output is correct |
80 |
Correct |
2470 ms |
116704 KB |
Output is correct |
81 |
Correct |
1273 ms |
115092 KB |
Output is correct |
82 |
Correct |
1351 ms |
99644 KB |
Output is correct |
83 |
Correct |
1378 ms |
98992 KB |
Output is correct |
84 |
Correct |
1177 ms |
99284 KB |
Output is correct |
85 |
Correct |
315 ms |
97212 KB |
Output is correct |
86 |
Correct |
885 ms |
117288 KB |
Output is correct |
87 |
Correct |
2618 ms |
111896 KB |
Output is correct |
88 |
Correct |
919 ms |
100200 KB |
Output is correct |
89 |
Correct |
3127 ms |
99592 KB |
Output is correct |
90 |
Execution timed out |
4065 ms |
97224 KB |
Time limit exceeded |
91 |
Halted |
0 ms |
0 KB |
- |