95求助,第10点tle了
查看原帖
95求助,第10点tle了
873302
Nienie楼主2024/3/5 21:31
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 1e5 + 10, M = 2e6 + 10, INF = 0x3f3f3f3f, mod = 998244353;

int n, m, q, op[N];
int h[N], e[M], ne[M], idx;
LL a[N], b[N], p[N], v[N];
LL mul = 1;
LL f[N];
int cnt[N], din[N];
int qu[N], top, tt;
int s[N];

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void get_f(int u) {
    LL t = 1;
    if (op[u] == 2) t = v[u];
    for (int i = h[u];i != -1;i = ne[i]) {
        int j = e[i];
        if (!f[j]) get_f(j);
        t = t * f[j] % mod;
    }
    f[u] = t;
}

void topsort() {
    for (int i = 1;i <= m;i++)
        if (op[i] == 3 && din[i] == 0)
            qu[tt++] = i;

    while (tt != top) {
        int t = qu[top++];
        for (int i = h[t];i != -1;i = ne[i]) {
            int j = e[i];
            if (op[j] == 1) cnt[j] = (cnt[j] + cnt[t]) % mod;
            else if (op[j] == 2) cnt[t] = cnt[t] * v[j] % mod;
            else {
                cnt[j] = (cnt[j] + cnt[t]) % mod;
                cnt[t] = cnt[t] * f[j] % mod;
                din[j]--;
                if (din[j] == 0) qu[tt++] = j;
            }
        }
        cnt[t] = 0;
    }
}

void solve() {
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    cin >> m;
    memset(h, -1, sizeof h);

    for (int i = 1;i <= m;i++) {
        cin >> op[i];
        if (op[i] == 1) cin >> p[i] >> v[i];
        else if (op[i] == 2) cin >> v[i];
        else {
            int j;
            cin >> j;
            while (j--) {
                int t;
                cin >> t;
                add(i, t);
                din[t]++;
            }
        }
    }

    for (int i = 1;i <= m;i++) {
        if (!f[i]) {
            if (op[i] == 1) f[i] = 1;
            else if (op[i] == 2) f[i] = v[i];
            else get_f(i);
        }
    }

    cin >> q;
    for (int i = 0;i < q;i++) cin >> s[i];

    for (int t = q - 1;t >= 0;t--) {
        int i = s[t];
        if (op[i] == 1) cnt[i] = (cnt[i] + mul) % mod;
        else if (op[i] == 2) mul = mul * v[i] % mod;
        else if (op[i] == 3) {
            cnt[i] = (cnt[i] + mul) % mod;
            mul = mul * f[i] % mod;
        }
    }

    topsort();

    for (int i = 1;i <= m;i++) {
        if (op[i] != 1) continue;
        b[p[i]] = (b[p[i]] + v[i] * cnt[i] % mod) % mod;
    }

    for (int i = 1;i <= n;i++) cout << (a[i] * mul % mod + b[i]) % mod << " ";
    cout << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    solve();
    return 0;
}
2024/3/5 21:31
加载中...