时间内通过但是爆内存了,求调教

P7077 [CSP-S2020] 函数调用

H2O_MERO @ 2023-10-12 18:04:05

#include<iostream>
#include<cstdio>
#include<vector>
#define ll unsigned long long
#define llp long long

using namespace std;

const int N = 10e6 + 20 , P = 998244353;
struct point{
    short op;
    int val , pos;
};
short func0[N];
vector<int> s , func[N];
//#define int unsigned long long
ll mul[N] , mult[N] , ans[N];
int n , m , q;

ll read()
{
    ll f = 1 , x = 0;
    char c = getchar();
    while( c > '9' || c < '0' ){
        if( c == '-' )
            f = -1;
        c = getchar();
    }
    while( c >= '0' && c <= '9' ){
        x  = x * 10 + ( c - '0' );
        c = getchar();
    }
    return x * f;
}

void addtos( int num )
{
    int op = func0[num];
    switch( op ){
        case 1:{
            s.push_back( num );
            break;
        }
        case 2:{
            s.push_back( num );
            break;
        }
        case 3:{
            int len = func[num].size() ;
            for( int i = 0 ; i < len ; i++ ){
                if( func0[func[num][i]] == 3 ){
                    addtos( func[num][i] );
                }
                else{
                    s.push_back( func[num][i] );
                }
            }
            break;
        }
    }
    return;
}

void calmul()
{
    ll len = s.size();
    for( llp j = len - 2 ; j >= 0 ; j-- ){
        mult[j] = mult[j] * mult[j+1] %P;
    }
}

void calans()
{
    ll len = s.size();
    for( llp i = len - 1 ; i >= 0 ; i-- ){
        ll funcn = s[i];
        short op = func0[funcn];
        if( op == 1 ){
            ll pos = func[funcn][0];
            ll val = func[funcn][1];

            ans[pos] = (ans[pos] + val * mult[i] %P)%P;
        }
    }
}

#undef int

int main()
{
    #define int unsigned long long
    n = read();
    for( int i = 1 ; i <= n ; i++ ){
        func0[i] = 1 ;
        func[i].push_back( i );
        func[i].push_back( read() );
        s.push_back( i );
        mul[i] = 1;
    }
    m = read();
    for( int i = 1 + n ; i <= m + n ; i++ ){
        int op = read();
        switch( op ){
            case 1:{
                func0[i] = 1;
                func[i].push_back(read());
                func[i].push_back(read());
                mul[i] = 1;
                break;
            }
            case 2:{
                func0[i] = 2;
                int t = read();
                func[i].push_back(t);
                mul[i] = t;
                break;
            }
            case 3:{
                int k = read();
                func0[i] = 3;
                for( int j = 1 ; j <= k ; j++ )
                    func[i].push_back(read()+n);
                break;
            }
        }
    }
    q = read();
    for( int i = 1 ; i <= q ; i++ ){
        addtos( read() +  n );
    }
    int len = s.size();
    for( int i = 0 ; i < len ; i++ )
        mult[i] = mul[s[i]];
    calmul();
    calans();
    for( int i = 1 ; i <= n ; i++ )
        cout << ans[i] <<" ";
    return 0;
}

|