记录编号 577219 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [CSP 2020S]函数调用 最终得分 100
用户昵称 Gravatarlihaoze 是否通过 通过
代码语言 C++ 运行时间 1.155 s
提交时间 2022-10-26 21:31:25 内存使用 10.62 MiB
显示代码纯文本
#include "bits/stdc++.h"

using i64 = long long;
 
constexpr int P = 998244353;
 
int norm(int x) {
    if (x < 0) 
        x += P;
    if (x >= P)
        x -= P;
    return x;
}
 
template <class T>
T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a)
        if (b % 2)
            res *= a;
    return res;
}
 
struct Z {
    i64 x;
    Z (int x = 0) : x(norm(x)) { }
    Z (i64 x) : x(norm(x % P)) { }
    int val() const {
        return x;
    }
    Z inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    Z & operator *= (const Z &rhs) {
        x = i64(x) * rhs.x % P;
        return *this;
    }
    Z & operator += (const Z &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    Z & operator -= (const Z &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    Z & operator /= (const Z &rhs) {
        return *this *= rhs.inv();
    }
    friend Z operator * (const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator + (const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator - (const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator / (const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream & operator >> (std::istream &is, Z &a) {
        i64 v;
        is >> v;
        a = Z(v);
        return is;
    }
    friend std::ostream & operator << (std::ostream &os, const Z &a) {
        return os << a.val();
    }
};

struct func {
    Z add, mul, pos, type, time;
};

const int N = 1e5 + 10;
int n, m, t;
Z a[N];
bool st[N];
func f[N];
std::vector<int> to[N];

void dfs(int u) {
    st[u] = true;
    for (int v : to[u]) {
        if (!st[v]) dfs(v);
        f[u].mul *= f[v].mul;
    }
}

int ind[N];
void toposort() {
    std::queue<int> q;
    for (int i = 1; i <= m + 1; ++ i) {
        if (ind[i] == 0) q.push(i);
    }
    while (q.size()) {
        int u = q.front(); q.pop();
        Z now = 1;
        std::reverse(to[u].begin(), to[u].end());
        for (int v : to[u]) {
            if (-- ind[v] == 0) q.push(v);
            f[v].time += f[u].time * now;
            now *= f[v].mul;
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    freopen("2020call.in", "r", stdin); 
    freopen("2020call.out", "w", stdout);
    std::cin >> n;
    for (int i = 1; i <= n; ++ i) {
        std::cin >> a[i];
    }
    std::cin >> m;
    for (int i = 1; i <= m; ++ i) {
        int op, x, y;
        std::cin >> op;
        if (op == 1) {
            std::cin >> x >> y;
            f[i] = { y, 1, x, 1, 0 };
        } else if (op == 2) {
            std::cin >> x;
            f[i] = { 0, x, 0, 2, 0 };
        } else if (op == 3) {
            std::cin >> x;
            f[i] = { 0, 1, 0, 3, 0 };
            for (int j = 1; j <= x; ++ j) {
                std::cin >> y;
                to[i].emplace_back(y);
                ++ ind[y];
            }
        }
    }
    std::cin >> t;
    f[m + 1].mul = 1;
    f[m + 1].time = 1;
    for (int i = 1, x; i <= t; ++ i) {
        std::cin >> x;
        to[m + 1].emplace_back(x);
        ++ ind[x];
    }
    dfs(m + 1);
    toposort();
    for (int i = 1; i <= n; ++ i) {
        a[i] *= f[m + 1].mul;
    }
    for (int i = 1; i <= m; ++ i) {
        if (f[i].add.x) {
            a[f[i].pos.x] += f[i].add * f[i].time;
        }
    }
    for (int i = 1; i <= n; ++ i) {
        std::cout << a[i] << ' ';
    }
    std::cout << '\n';
    return 0;
}