Submission #597278

# Submission time Handle Problem Language Result Execution time Memory
597278 2022-07-15T20:32:17 Z Hacv16 Arboras (RMI20_arboras) C++17
0 / 100
4030 ms 213196 KB
#include<bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,avx")
#pragma GCC optimize("unroll-loops")

typedef long long ll;
typedef pair<int, int> pii;

const int MAX = 2e6 + 15;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ": " << "[ " << x << " ]\n"

ll n, q, p[MAX], wg[MAX], dp[MAX][2], use[MAX][2], ans; 
vector<int> adj[MAX], weight[MAX];

void dfs(int u){
    for(int i = 0; i < sz(adj[u]); i++){
        int v = adj[u][i], w = weight[u][i];
        dfs(v);

        if(dp[v][0] + w > dp[u][0]){
            dp[u][1] = dp[u][0];
            dp[u][0] = dp[v][0] + w;

            use[u][1] = use[u][0];
            use[u][0] = v;

        }else if(dp[v][0] + w > dp[u][1]){
            dp[u][1] = dp[v][0] + w;
            use[u][1] = v;
        }
    }

    ans += dp[u][0] + dp[u][1]; ans %= MOD;
}

void update(int u, int x){ //Update the tree and answer
    wg[u] += x;

    while(u != 0){
        if(use[p[u]][0] == u){
            ans += dp[u][0] + wg[u] - dp[p[u]][0]; 
            dp[p[u]][0] = dp[u][0] + wg[u];

        }else if(dp[u][0] + wg[u] > dp[p[u]][0]){
            ans += dp[p[u]][0] - dp[p[u]][1] + dp[u][0] + wg[u] - dp[p[u]][0];
            dp[p[u]][1] = dp[p[u]][0]; 
            use[p[u]][0] = use[p[u]][1];

            dp[p[u]][0] = dp[u][0] + wg[u];
            use[p[u]][0] = u;

        }else if(dp[u][0] + wg[u] > dp[p[u]][1]){
            ans += dp[u][0] + wg[u] - dp[p[u]][1];
            dp[p[u]][1] = dp[u][0] + wg[u];
            use[p[u]][1] = u;
        }

        ans %= MOD;
        u = p[u];
    }
}

int main(){
    scanf("%lld", &n);

    for(int i = 1; i < n; i++){
        scanf("%lld", &p[i]);
        adj[p[i]].pb(i);
    }

    for(int i = 1; i < n; i++){
        scanf("%lld", &wg[i]);
        weight[p[i]].pb(wg[i]);
    }

    dfs(0);

    printf("%lld\n", ans);

    scanf("%lld", &q);

    while(q--){
        int u, add; 
        scanf("%lld%lld", &u, &add);
        update(u, add);

        printf("%lld\n", ans);
    }

    return 0;
}

Compilation message

arboras.cpp: In function 'int main()':
arboras.cpp:96:19: warning: format '%lld' expects argument of type 'long long int*', but argument 2 has type 'int*' [-Wformat=]
   96 |         scanf("%lld%lld", &u, &add);
      |                ~~~^       ~~
      |                   |       |
      |                   |       int*
      |                   long long int*
      |                %d
arboras.cpp:96:23: warning: format '%lld' expects argument of type 'long long int*', but argument 3 has type 'int*' [-Wformat=]
   96 |         scanf("%lld%lld", &u, &add);
      |                    ~~~^       ~~~~
      |                       |       |
      |                       |       int*
      |                       long long int*
      |                    %d
arboras.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |     scanf("%lld", &n);
      |     ~~~~~^~~~~~~~~~~~
arboras.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         scanf("%lld", &p[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
arboras.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         scanf("%lld", &wg[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~
arboras.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     scanf("%lld", &q);
      |     ~~~~~^~~~~~~~~~~~
arboras.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         scanf("%lld%lld", &u, &add);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 141 ms 191052 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 232 ms 208144 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4030 ms 213196 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 141 ms 191052 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -