Submission #477981

# Submission time Handle Problem Language Result Execution time Memory
477981 2021-10-05T00:22:02 Z zerver Magic Tree (CEOI19_magictree) C++14
0 / 100
65 ms 30604 KB
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0); cin.tie(nullptr)
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define sz(v) int(v.size())
#define ss second
#define ff first
#define endl '\n'
using namespace std;
typedef long long Long;
typedef pair<int, int> ii;
const Long INF = 1e13;
const int N = 1e5;
vector<int> adj[N];
set<ii> dif[N];
int dia[N], peso[N], pt[N];

void dfs(int from){
  int big = 0;
  for(int to: adj[from]){
    dfs(to);
    if(big == 0 || sz(dif[pt[to]]) > sz(dif[pt[big]])) big = to;
  }
  if(big == 0){
    if(dia[from] == 0) return;
    dif[pt[from]].insert({dia[from], peso[from]});
    return;
  }
  pt[from] = pt[big];
  for(int to: adj[from]){
    if(to == big) continue;
    for(auto p: dif[pt[to]]) dif[pt[from]].insert(p);
  }
  if(dia[from] != 0){
    ii par = {dia[from] + 1, 0};
    auto it = dif[pt[from]].lower_bound(par);
    ii ins = {dia[from], peso[from]};
    for(; it != dif[pt[from]].end(); it++){
      ii npar = *(it);
      if(npar.ss <= ins.ss){
        ins.ss -= npar.ss;
        dif[pt[from]].erase(it);
      }
      else{
        npar.ss -= ins.ss;
        dif[pt[from]].erase(it);
        dif[pt[from]].insert(npar);
        break;
      }
    }
    dif[pt[from]].insert(ins);
  }
}

void solve(){
  int n, m, k; cin >> n >> m >> k;
  for(int i = 2; i <= n; i++){
    int p; cin >> p;
    adj[p].push_back(i);
  }
  for(int i = 0; i < m; i++){
    int v; cin >> v;
    cin >> dia[v] >> peso[v];
  }
  for(int i = 1; i <= n; i++) pt[i] = i;
  dfs(1);
  Long ans = 0;
  for(auto p: dif[pt[1]]){
    cout << p.ff << " " << p.ss << endl;
    ans += p.ss;
  }
  cout << ans << endl;
}

int main(){
  FAST_IO;
  int tt = 1; 
  // cin >> tt;
  while(tt--) solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 14640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 65 ms 30604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 15052 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 58 ms 22460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 14640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 15952 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 14640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 14640 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -