#include <bits/stdc+
2024-04-15 20:39:29
发布于:广东
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 3e5 + 10;
const LL p = 998244353;
int n, m, q,T[N], P[N], idx[N];
LL a[N], V[N];
vector<int> G[N];
int deg[N];
LL K = 1, k[N], t[N];
bool vis[N]; // dfs 和 bfs 记录
void dfs(int j) {
if (vis[j]) return;
vis[j] = true;
int size = G[j].size();
for (int ptr = size-1; ptr >= 0; --ptr) {
int g = G[j][ptr];
++deg[g], dfs(g);
k[j] = (k[j]*k[g]) % p;
}
}
queue<int> Q;
void topSort() {
for (int j = 1; j <= m; ++j)
if (deg[j] == 0) Q.push(j);
while (!Q.empty()) {
int j = Q.front(); Q.pop();
if (vis[j]) continue;
vis[j] = true;
LL w = 1;
int size = G[j].size();
/* push_down /
for (int ptr = size-1; ptr >= 0; --ptr) {
int g = G[j][ptr];
t[g] = (t[g] + (wt[j])%p) % p;
w = (w*k[g]) % p;
deg[g] -= 1;
if (deg[g] == 0) Q.push(g);
}
}
}
int main() {
/* 读取数据 */
cin >> n;
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
cin >> m;
for (int j = 1; j <= m; ++j) {
scanf("%d", &T[j]);
k[j] = 1;
if (T[j] == 1) {
scanf("%d%lld", &P[j], &V[j]);
} else if (T[j] == 2) {
scanf("%lld", &k[j]);
} else if (T[j] == 3) {
int C; scanf("%d", &C);
for (int l = 1; l <= C; ++l) {
int g; scanf("%d", &g);
G[j].push_back(g);
}
}
}
cin >> q;
for (int j = 1; j <= q; ++j) scanf("%d", &idx[j]);
/* dfs 计算 k[j] */
memset(vis, false, sizeof(vis));
for (int j = 1; j <= m; ++j) dfs(j);
/* 计算乘法 K,打加法标记 */
LL w = 1;
for (int j = q; j >= 1; --j) {
K = (K*k[idx[j]]) % p;
t[idx[j]] = (t[idx[j]]+w) % p;
w = (w*k[idx[j]]) % p;
}
/* 拓扑排序 push_down 加法标记 */
memset(vis, false, sizeof(vis)), topSort();
/* 计算答案 */
for (int i = 1; i <= n; ++i)
a[i] = (a[i]*K) % p;
for (int j = 1; j <= m; ++j) {
if (T[j] != 1) continue;
a[P[j]] = (a[P[j]] + ((t[j]*V[j])%p)) % p;
}
for (int i = 1; i <= n; ++i) printf("%lld ", a[i]);
putchar('\n');
return 0;
}
这里空空如也
有帮助,赞一个