1 条题解

  • 0
    @ 2024-12-5 19:46:49

    #include<bits/stdc++.h> typedef long long LL; const int INF = 0x3f3f3f3f; const int MAXN = 4e5 + 5, MAXLOG = 25;

    template void read( _T &x ) { x = 0; char s = getchar(); bool f = false; while( s < '0' || '9' < s ) { f = s == '-', s = getchar(); } while( '0' <= s && s <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar(); } if( f ) x = -x; }

    template void write( _T x ) { if( x < 0 ) putchar( '-' ), x = -x; if( 9 < x ) write( x / 10 ); putchar( x % 10 + '0' ); }

    template _T MIN( const _T a, const _T b ) { return a < b ? a : b; }

    template _T MAX( const _T a, const _T b ) { return a > b ? a : b; }

    LL gain[MAXN][MAXLOG]; int go[MAXN][MAXLOG];

    LL jmp[MAXN][MAXLOG]; int nxt[MAXN][MAXLOG], lim[MAXN][MAXLOG];

    LL wei[MAXN][MAXLOG]; int bound[MAXN][MAXLOG], tar[MAXN][MAXLOG]; bool vis[MAXN];

    int S[MAXN], P[MAXN], W[MAXN], L[MAXN];

    int up, dn; int N, Q, lg2, fin;

    void DFS( const int u, const int t ) { if( ~ tar[u][t] || vis[u] ) return ; vis[u] = true; if( S[u] < ( 1 << t ) ) { DFS( W[u], t ); tar[u][t] = tar[W[u]][t]; wei[u][t] = wei[W[u]][t] + S[u]; bound[u][t] = MAX( 0, MIN( up - 1, bound[W[u]][t] ) - S[u] ); } else { DFS( L[u], t ); tar[u][t] = tar[L[u]][t]; wei[u][t] = wei[L[u]][t] + P[u]; bound[u][t] = MAX( 0, MIN( S[u] - 1, MIN( up - 1, bound[L[u]][t] ) - P[u] ) ); } }

    void Init() { int mx = 0; for( int i = 0 ; i < N ; i ++ ) mx = MAX( mx, S[i] ); lg2 = log2( mx ), fin = log2( N ); for( int t = 0 ; t <= lg2 ; t ++ ) { up = ( 1 << ( t + 1 ) ), dn = 1 << t; for( int i = 0 ; i <= N ; i ++ ) tar[i][t] = -1, bound[i][t] = 0, vis[i] = false; for( int i = 0 ; i <= N ; i ++ ) if( i == N || ( dn <= S[i] && S[i] < up ) ) tar[i][t] = i, wei[i][t] = 0, bound[i][t] = INF; for( int i = 0 ; i <= N ; i ++ ) DFS( i, t ); for( int i = 0 ; i < N ; i ++ ) if( dn <= S[i] && S[i] < up ) { if( ~ tar[L[i]][t] ) { nxt[i][0] = tar[L[i]][t]; jmp[i][0] = P[i] + wei[L[i]][t]; lim[i][0] = MAX( 0, MIN( MIN( up - 1 - P[i], S[i] - 1 ), bound[L[i]][t] - P[i] ) ); } else nxt[i][0] = -1, jmp[i][0] = 0, lim[i][0] = 0; } } nxt[N][0] = N, lim[N][0] = 0; for( int j = 1 ; j <= lg2 ; j ++ ) for( int i = 0 ; i <= N ; i ++ ) { int p = nxt[i][j - 1]; if( p == -1 || nxt[p][j - 1] == -1 ) { nxt[i][j] = -1; jmp[i][j] = lim[i][j] = 0; continue; } nxt[i][j] = nxt[p][j - 1]; jmp[i][j] = jmp[i][j - 1] + jmp[p][j - 1]; lim[i][j] = MAX( 0ll, MIN( ( LL ) lim[i][j - 1], lim[p][j - 1] - jmp[i][j - 1] ) ); }

    gain[N][0] = 0, go[N][0] = N;
    for( int i = 0 ; i < N ; i ++ )
        gain[i][0] = S[i], go[i][0] = W[i];
    for( int j = 1 ; j <= fin ; j ++ )
        for( int i = 0 ; i <= N ; i ++ )
            go[i][j] = go[go[i][j - 1]][j - 1],
            gain[i][j] = gain[i][j - 1] + gain[go[i][j - 1]][j - 1];
    

    }

    LL Query( int x, LL z ) { for( int t = 0 ; t <= lg2 ; t ++ ) { if( z >= ( 1 << ( t + 1 ) ) ) continue; if( z > bound[x][t] || tar[x][t] == -1 ) continue; z += wei[x][t], x = tar[x][t]; for( int i = lg2 ; ~ i ; i -- ) if( ~ nxt[x][i] && z <= lim[x][i] ) z += jmp[x][i], x = nxt[x][i]; if( x == N ) break; if( z < S[x] ) z += P[x], x = L[x]; else z += S[x], x = W[x]; } if( x != N ) { for( int i = fin ; ~ i ; i -- ) if( go[x][i] != N ) z += gain[x][i], x = go[x][i]; z += S[x]; } return z; }

    int main() { read( N ), read( Q ); for( int i = 0 ; i < N ; i ++ ) read( S[i] ); for( int i = 0 ; i < N ; i ++ ) read( P[i] ); for( int i = 0 ; i < N ; i ++ ) read( W[i] ); for( int i = 0 ; i < N ; i ++ ) read( L[i] ); Init(); while( Q -- ) { int a, b; read( a ), read( b ); write( Query( a, b ) ), putchar( '\n' ); } return 0; }

    信息

    ID
    21065
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    3
    已通过
    0
    上传者