求助

P7077 [CSP-S2020] 函数调用

lizichang @ 2021-10-10 20:32:39

#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
using namespace std;
const int N=100005;
const int M=998244353;
int tag=1,n,m;
int plu[N][2],mul[N]; 
int a[N],t[N];
vector<int> v[N];
void addition(int q)
{
    if(tag!=1)
    {
        tag=(tag%M);
        for(int i=1;i<=n;i++)   a[i]=(a[i]*tag)%M;
        tag=1;
    }
    a[plu[q][0]]+=plu[q][1];
}
void recursion(int q)
{
    int y;
    //cout<<v[q].size()<<endl;
    for(int i=0;i<v[q].size();i++)
    {
        y=v[q][i];
        //cout<<y<<endl;
        if(t[y]==1) addition(y);
        else if(t[y]==2)    tag=(tag*mul[y])%M;
        else    recursion(y);
    }
}
int main()
{
    int x,y;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)   scanf("%d",&a[i]);;
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&t[i]);
        if(t[i]==1)
        {
            scanf("%d%d",&x,&y);
            plu[i][0]=x,plu[i][1]=y;
        }
        else if(t[i]==2)
        {
            scanf("%d",&x);
            mul[i]=x;
        }
        else
        {
            scanf("%d",&x);
            for(int j=1;j<=x;j++)
            {
                scanf("%d",&y);
                v[i].push_back(y);
                //cout<<v[i].size()<<endl;
            }
        }
    }
    scanf("%d",&x);
    while(x--)
    {
        scanf("%d",&y);
        if(t[y]==1) addition(y);
        else if(t[y]==2)    tag=(tag*mul[y])%M;
        else    recursion(y);
        //for(int i=1;i<=n;i++) cout<<a[i]<<' ';
    }
    if(tag!=1)
    { 
        tag=(tag%M);
        for(int i=1;i<=n;i++)   a[i]=(a[i]*tag)%M;
        tag=1;
    }
    for(int i=1;i<=n;i++)   cout<<a[i]<<' ';
    return 0;
}

by lizichang @ 2021-10-10 20:34:58

求过50%的数据


|