Submission #659312

# Submission time Handle Problem Language Result Execution time Memory
659312 2022-11-17T12:03:32 Z mychecksedad Magic Tree (CEOI19_magictree) C++17
3 / 100
268 ms 89556 KB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD1 (1000000000+7)
#define MOD (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;



int n, m, k, ti[N], sz[N], timer = 1, tin[N], seg_pos[N], par[N], heavy[N], o_t[N];
vector<int> g[N], path[N];
vector<pair<int, int>> times[N];
ll w[N], dp[N], t[N], lazy[N];

void push(int k){
    if(lazy[k] > 0){
        lazy[k<<1] = max(lazy[k<<1], lazy[k]);
        lazy[k<<1|1] = max(lazy[k<<1|1], lazy[k]);
        t[k<<1] = max(t[k<<1], lazy[k]);
        t[k<<1|1] = max(t[k<<1|1], lazy[k]);
        lazy[k] = 0;
    }
}

void build(int l, int r, int k){
    if(l==r){
        t[k] = lazy[k] = 0;
        return;
    }
    int m = (l+r)>>1;
    build(l, m, k<<1);
    build(m+1, r, k<<1|1);
    t[k] = lazy[k] = 0;
}

ll get(int l, int r, int p, int k){
    if(l == r) return t[k];
    push(k);
    int m = (l+r)>>1;
    if(m>=p)
        return max(t[k], get(l, m, p, k<<1));
    return max(t[k], get(m+1, r, p, k<<1|1));
}

void update(int l, int r, int ql, int qr, int k, ll val){
    if(l > qr || r < ql) return;
    if(ql <= l && r <= qr){
        t[k] = max(val, t[k]);
        lazy[k] = max(val, lazy[k]);
        return;
    }
    push(k);
    int m = (l+r)>>1;
    update(l, m, ql, qr, k<<1, val);
    update(m+1, r, ql, qr, k<<1|1, val);
}

void pre(int v, int p){
    sz[v] = 1;
    par[v] = p;
    for(int &u: g[v]){
        pre(u, v);
        sz[v] += sz[u];
        if(sz[u] > sz[g[v][0]]) swap(u, g[v][0]);
    }
}

void pre_dfs(int v, int p){
    tin[v] = timer++;
    path[heavy[v]].pb(v);
    for(int &u: g[v]){
        heavy[u] = (g[v][0] == u ? heavy[v] : u);
        pre_dfs(u, v);
        ti[u] = ti[v];
    }
}

void dfs(int v, int p){
    ll inc = w[v], exc = 0;
    for(int u: g[v]){
        dfs(u, v);
        inc += get(1, n, seg_pos[u], 1);
        exc += max(dp[u], get(1, n, seg_pos[u], 1));
    }

    dp[v] = max(inc, exc);

    int T = o_t[v];
    int V = v;
    
    while(v > 0){
        int pos = upper_bound(all(times[heavy[v]]), make_pair(T, -1)) - times[heavy[v]].begin();
        int S = times[heavy[v]].size() - 1;
        if(pos <= S){
            update(1, n, tin[heavy[v]], tin[heavy[v]] + S - pos, 1, dp[V]);
        }
        v = par[heavy[v]];
    }  
}

void solve(){
    cin >> n >> m >> k;
    for(int i = 2; i <= n; ++i){
        int p; cin >> p;
        g[p].pb(i);
    }
    for(int i = 1; i <= n; ++i) o_t[i] = t[i] = w[i] = dp[i] = 0;
    for(int i = 0; i < m; ++i){
        int v, d, e; cin >> v >> d >> e;
        o_t[v] = ti[v] = d;
        w[v] = e;
    }

    ti[1] = MOD;
    w[1] = 0;

    pre(1, 0);
    heavy[1] = 1;
    pre_dfs(1, 0);

    for(int i = 1; i <= n; ++i){
        times[i].pb({-1, 0});
        for(int v: path[i]){
            times[i].pb({ti[v], v});
        }
        sort(all(times[i]));
        int S = times[i].size() - 1;
        for(int j = 1; j <= S; ++j){
            int v = times[i][j].second;
            seg_pos[v] = tin[i] + S - j;
        }
    }
    build(1, n, 1);

    dfs(1, 0);

    cout << dp[1];
}





int main(){
    cin.tie(0); ios::sync_with_stdio(0);
    int T = 1, aa;
    // cin >> T;aa=T;
    while(T--){
        // cout << "Case #" << aa-T << ": ";
        solve();
        cout << '\n';
    }
    return 0;
 
}

Compilation message

magictree.cpp: In function 'int main()':
magictree.cpp:155:16: warning: unused variable 'aa' [-Wunused-variable]
  155 |     int T = 1, aa;
      |                ^~
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70732 KB Output is correct
2 Correct 36 ms 70796 KB Output is correct
3 Correct 42 ms 70740 KB Output is correct
4 Correct 39 ms 70824 KB Output is correct
5 Incorrect 34 ms 70748 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 206 ms 86180 KB Output is correct
2 Correct 167 ms 89556 KB Output is correct
3 Correct 184 ms 86048 KB Output is correct
4 Correct 116 ms 85864 KB Output is correct
5 Correct 159 ms 86836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 35 ms 70988 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 268 ms 86244 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70732 KB Output is correct
2 Correct 36 ms 70796 KB Output is correct
3 Correct 42 ms 70740 KB Output is correct
4 Correct 39 ms 70824 KB Output is correct
5 Incorrect 34 ms 70748 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 59 ms 74136 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70732 KB Output is correct
2 Correct 36 ms 70796 KB Output is correct
3 Correct 42 ms 70740 KB Output is correct
4 Correct 39 ms 70824 KB Output is correct
5 Incorrect 34 ms 70748 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70732 KB Output is correct
2 Correct 36 ms 70796 KB Output is correct
3 Correct 42 ms 70740 KB Output is correct
4 Correct 39 ms 70824 KB Output is correct
5 Incorrect 34 ms 70748 KB Output isn't correct
6 Halted 0 ms 0 KB -