Submission #938921

# Submission time Handle Problem Language Result Execution time Memory
938921 2024-03-05T20:54:51 Z vjudge1 Magic Tree (CEOI19_magictree) C++17
14 / 100
195 ms 25428 KB
/*adid_r10*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int N = 1e5 + 9, MOD = 1e9 + 7;
    
//// Total Code time complexity: O(nlogn)



////
    
int k;
// int a[N];
struct ST {
  ll t[4 * N];
  static const int inf = 1e9;
  ST() {
    memset(t, 0, sizeof t);
  }
  void build(int _n, int b, int e) {
    if (b == e) {
      t[_n] = 0;
      return;
    }
    int mid = (b + e) >> 1, l = _n << 1, r = l | 1;
    build(l, b, mid);
    build(r, mid + 1, e);
    t[_n] = max(t[l], t[r]); // change this
  }
  void upd(int _n, int b, int e, int i, ll x) {
    if (b > i || e < i) return;
    if (b == e && b == i) {
      t[_n] = x; // update
      return;
    }
    int mid = (b + e) >> 1, l = _n << 1, r = l | 1;
    upd(l, b, mid, i, x);
    upd(r, mid + 1, e, i, x);
    t[_n] = max(t[l], t[r]); // change this
  }
  void upd (int idx, int x) {
    upd(1, 1, k, idx, x);
  }

  ll query(int _n, int b, int e, int i, int j) {
    if (b > j || e < i) return 0; // return appropriate value
    if (b >= i && e <= j) return t[_n];
    int mid = (b + e) >> 1, l = _n << 1, r = l | 1;
    return max(query(l, b, mid, i, j), query(r, mid + 1, e, i, j)); // change this
  }
  ll query(int l, int r) {
    return query (1, 1, k, l, r);
  }
}seg;
///

vector <int> adj[N];
int sz[N];

int TM = 1;
int st [N], ft[N], node_start[N];
void dfsz_and_eulerTour (int u, int p) {
    sz[u] = 1;
    node_start[TM] = u;
    st[u] = TM++;
    for (auto v: adj[u]) {
        if (v != p) {
            dfsz_and_eulerTour (v, u);
            sz[u] += sz[v];
        }
    }
    ft[u] = TM;
}
int dd[N], ww[N];
void ddffss (int u, int p, ST &seg) {

    for (auto v: adj[u]) {
        if (v != p) {
            ddffss(v, u, seg);
        }
    }
    if(dd[u] == 0) return;
    seg.upd(dd[u], 0LL + seg.query(1, dd[u]) + ww[u]);
}

ll ans[N];
void dfs (int u, int p, bool keep, ST &seg) {
    int mxsz = -1, bigchild = -1;
    for (auto v: adj[u]) {
        if (v != p) {
            if (sz[v] > mxsz) {
                mxsz = sz[v];
                bigchild = v;
            }
        
        }
    }

    for (auto v: adj[u]) {
        if (v != p && v != bigchild) {
            dfs(v, u, 0, seg);
            ans[u] += ans[v];
        }
    }
    if (bigchild != -1) {
        dfs(bigchild, u, 1, seg);
        ans[u] += ans[bigchild];
    }
    
    for (auto v: adj[u]) {
        if (v != p && v != bigchild) {
            // for (int t = st[v]; t < ft[v]; t++) {
            //     // OPS(node_start[t], mxfrq, sum);
            //     // colcnt[col[node_start[t]]]++;

            // }
            ddffss (v, p, seg);
        } 
    }
    if (dd[u] != 0) {
        seg.upd(dd[u], seg.query (1, dd[u]) + ww[u]);
        ans[u] = max(ans[u], 1LL * seg.query (1, dd[u]));
    }
    // OPS(u, mxfrq, sum);
    // colcnt[col[u]]++;    
    //now colcnt[c] is the number of vertices in subtree of vertex u that has color c. You can answer the queries 

    if (keep == 0) {
        for (int t = st[u]; t < ft[u]; t++) {
            int cc = node_start[t];
            // colcnt[col[cc]]--;
            if (dd[cc] == 0) continue;
            seg.upd(dd[cc], 0);
        }
    }
}

int32_t main () {
    ios_base::sync_with_stdio(0);cin.tie(0);
    // cout << "YO\n";
    // return 0;
    int nn, mm, kk; cin >> nn >> mm >> kk;
    k = kk;
    seg.build(1, 1, k);
    // cout << "YO\n";
    for (int i = 2; i <= nn; i++) {
        int p; cin >> p;
        adj[p].push_back(i);
    }   

    for (int i = 1; i <= mm; i++) {
        int v, d, w; cin >> v >> d >> w;
        dd[v] = d;
        ww[v] = w;
    }
    dfsz_and_eulerTour(1, -1);
    dfs (1, -1, 0, seg);
    cout << ans[1] << '\n';
    return 0;
} 



// int32_t main() {
//   ios_base::sync_with_stdio(0);
//   cin.tie(0);
//   // cin >> n;
//   int n = 5;
//   for (int i = 1; i <= n; i++) {
//     a[i] = i;
//   }
//   seg.build(1, 1, n); // building the segment tree
//   seg.upd(2, 10); // assiging 10 to the index 2 (a[2] := 10)
//   cout << seg.query(1, 5) << '\n'; // range max query on the segment [1, 5]

//   return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11184 KB Output is correct
9 Incorrect 2 ms 11100 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 86 ms 13128 KB Output is correct
2 Correct 56 ms 18260 KB Output is correct
3 Correct 195 ms 12636 KB Output is correct
4 Correct 93 ms 12244 KB Output is correct
5 Correct 108 ms 13904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11356 KB Output is correct
2 Correct 2 ms 11356 KB Output is correct
3 Correct 2 ms 11288 KB Output is correct
4 Correct 61 ms 25428 KB Output is correct
5 Correct 60 ms 25428 KB Output is correct
6 Correct 79 ms 25428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 13148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11184 KB Output is correct
9 Incorrect 2 ms 11100 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 11608 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11184 KB Output is correct
9 Incorrect 2 ms 11100 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 2 ms 11100 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 2 ms 11100 KB Output is correct
7 Correct 2 ms 11100 KB Output is correct
8 Correct 2 ms 11184 KB Output is correct
9 Incorrect 2 ms 11100 KB Output isn't correct
10 Halted 0 ms 0 KB -