分析:
讲解
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
ll n, m, root, mod;
ll v[maxn];
struct edge
{
int ep, nex;
edge(int ep = 0, int nex = 0) : ep(ep), nex(nex){}
}e[maxn << 1];
int head[maxn], tot;
void init()
{
mem(head, -1), tot = 0;
}
void addedge(int sp, int ep)
{
e[tot] = edge(ep, head[sp]);
head[sp] = tot++;
}
int depth[maxn], siz[maxn], mson[maxn], f[maxn];
int id[maxn], w[maxn];
int top[maxn];
int tim;
void dfs1(int u, int fa, int dep)
{
depth[u] = dep, siz[u] = 1, f[u] = fa;
int maxnum = -1;
for(int i = head[u]; ~i; i = e[i].nex)
{
int ep = e[i].ep;
if(ep == fa) continue;
dfs1(ep, u, dep + 1);
siz[u] += siz[ep];
if(siz[ep] > maxnum) maxnum = siz[ep], mson[u] = ep;
}
}
void dfs2(int u, int fa, int topfa)
{
id[u] = ++tim;
w[tim] = v[u];
top[u] = topfa;
if(!mson[u]) return;
dfs2(mson[u], u, topfa);
for(int i = head[u]; ~i; i = e[i].nex)
{
int ep = e[i].ep;
if(ep == fa || ep == mson[u]) continue;
dfs2(ep, u, ep);
}
}
struct node
{
ll l, r, sum, lazy;
node(int l = 0, int r = 0, int sum = 0, int lazy = 0) : l(l), r(r), sum(sum), lazy(lazy){}
}t[maxn << 2];
void pushup(int tar)
{
t[tar].sum = (t[tar << 1].sum + t[tar << 1 | 1].sum) % mod;
}
void pushdown(int tar)
{
if(t[tar].lazy)
{
t[tar << 1].lazy += t[tar].lazy, t[tar << 1 | 1].lazy += t[tar].lazy;
t[tar << 1].sum += t[tar].lazy * (t[tar << 1].r - t[tar << 1].l + 1);
t[tar << 1].sum %= mod;
t[tar << 1 | 1].sum += t[tar].lazy * (t[tar << 1 | 1].r - t[tar << 1 | 1].l + 1);
t[tar << 1 | 1].sum %= mod;
t[tar].lazy = 0;
}
}
void build(int tar, int l, int r)
{
t[tar].l = l, t[tar].r = r, t[tar].lazy = 0;
if(l == r)
{
t[tar].sum = w[l] % mod;
return;
}
int mid = l + r >> 1;
build(tar << 1, l, mid), build(tar << 1 | 1, mid + 1, r);
pushup(tar);
}
void update(int tar, int l, int r, int k)
{
if(l <= t[tar].l && t[tar].r <= r)
{
t[tar].sum = (t[tar].sum + (t[tar].r - t[tar].l + 1) * k) % mod;
t[tar].lazy += k;
return;
}
pushdown(tar);
int mid = t[tar].l + t[tar].r >> 1;
if(l <= mid) update(tar << 1, l, r, k);
if(r > mid) update(tar << 1 | 1, l, r, k);
pushup(tar);
}
ll query(int tar, int l, int r)
{
ll res = 0;
if(l <= t[tar].l && t[tar].r <= r)
{
res += t[tar].sum, res %= mod;
return res;
}
pushdown(tar);
int mid = t[tar].l + t[tar].r >> 1;
if(l <= mid) res += query(tar << 1, l, r), res %= mod;
if(r > mid) res += query(tar << 1 | 1, l, r), res %= mod;
return res;
}
void update2(int x, int y, int k)
{
while(top[x] != top[y])
{
if(depth[top[x]] < depth[top[y]]) swap(x, y);
update(1, id[top[x]], id[x], k);
x = f[top[x]];
}
if(depth[x] > depth[y]) swap(x, y);
update(1, id[x], id[y], k);
}
ll query2(int x, int y)
{
ll res = 0;
while(top[x] != top[y])
{
if(depth[top[x]] < depth[top[y]]) swap(x, y);
res += query(1, id[top[x]], id[x]), res %= mod;
x = f[top[x]];
}
if(depth[x] > depth[y]) swap(x, y);
res += query(1, id[x], id[y]), res %= mod;
return res;
}
int main()
{
init();
cin >> n >> m >> root >> mod;
for(int i = 1; i <= n; i++) cin >> v[i];
for(int i = 1; i < n; i++)
{
int x, y; cin >> x >> y;
addedge(x, y), addedge(y, x);
}
dfs1(root, -1, 1);
dfs2(root, -1, root);
build(1, 1, n);
while(m--)
{
int k, x, y, z; cin >> k;
if(k == 1)
{
cin >> x >> y >> z;
update2(x, y, z);
}
else if(k == 2)
{
cin >> x >> y;
cout << query2(x, y) << endl;
}
else if(k == 3)
{
cin >> x >> y;
update(1, id[x], id[x] + siz[x] - 1, y);
}
else
{
cin >> x;
cout << query(1, id[x], id[x] + siz[x] - 1) << endl;
}
}
}