답안 #993907

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
993907 2024-06-06T19:18:21 Z cadmiumsky Worst Reporter 4 (JOI21_worst_reporter4) C++17
79 / 100
460 ms 80776 KB
// o sa ia zero -- am avut dreptate
// ia si de data asta zero
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
 
using ll = long long;
using ld = long double;
 
#define int ll
#define sz(x) ((int)(x).size())
 
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
 
const int nmax = 2e5 + 5;
const int inf = 1e9 + 5 + 7 + 9;
 
vector<int> g[nmax];
 
int father[nmax], cost[nmax], height[nmax];
vector<int> naive[nmax];
 
int solved[nmax];
 
template<class CB> void fill(int a, int f, CB&& cb, vector<int> graph[nmax]) {
   cb(a);
   for(auto x : graph[a])
      if(x != f) 
         fill(x, a, cb, graph);
   return;
}
 
#define lsb(x) (x & -x)
struct AIB {
   vector<int> v;
   void init(int n_) { v.assign(n_ + 5, 0); }
   int query(int a) {
      int sum = 0;
      while(a > 0) sum += v[a], a -= lsb(a);
      return sum;
   }
   void update(int p, int x) {
      while(p < sz(v)) v[p] += x, p += lsb(p);
   }
};
 
namespace DS {
   ll sol;
  
   AIB occupied;
   int pin[nmax], pout[nmax], inp;
   int jump[nmax], p[nmax], d[nmax];
   int budget[nmax];
   
   void fill(int a, int f) {
      
      //cerr << a << ' ' << f << '\n';
      
      d[a] = d[f] + 1;
      p[a] = jump[a] = f;
      if(d[f] + d[jump[jump[f]]] == 2 * d[jump[f]]) jump[a] = jump[jump[f]];
            
      pin[a] = inp++;
      for(auto x : g[a])
         if(x != f) 
            fill(x, a);
      pout[a] = inp++;
      return;
   }
 
   void startProcedure(vector<int> nodes) {
      height[0] = -inf;
      sol = 0;
      inp = 1;
      fill(0, 0);
      
      map<pii, int> renrm;
      for(auto x : nodes) renrm[make_pair(height[x], d[x])];
      int cnt = 0;
      for(auto &[a, b] : renrm) b = cnt++;
      for(auto x : nodes) height[x] = renrm[make_pair(height[x], d[x])];
      
      occupied.init(inp);
      
      //cerr << "wow\n";
      
      ll sum = 0;
      for(auto x : nodes) sum += cost[x];
      
      sort(all(nodes), [&](int a, int b) { return height[a] > height[b]; });
      for(auto x : nodes) budget[x] = 0;
      
      auto anc = [&](int node) { return occupied.query(pin[node]); };
      auto modnode = [&](int node, int p) { occupied.update(pin[node], p), occupied.update(pout[node], -p); };
      
      nodes.pop_back();
      for(auto x : nodes) {
         //cerr << x << '\t' << anc(x) << '\t' << height[x] << '\n';
         int mine = anc(x), upward = x, mybudget = cost[x];
         
         while(mybudget > 0 && mine > 0) {
            int tmp = upward;
            //if(mine >= 0) cerr << upward << '\t';
            while(upward != 0 && anc(upward) == mine) {
               if(anc(jump[upward]) == mine) upward = jump[upward];
               tmp = upward;
               upward = p[upward];
            }
            upward = tmp;
            //if(mine >= 0) cerr << upward << '\n';
            if(upward != 0) {
               int u = min(mybudget, budget[upward]);
               mybudget -= u, budget[upward] -= u;
               if(budget[upward] == 0) modnode(upward, -1), mine = anc(x);
            }
         }
         
         sol += mybudget;
         
         modnode(x, 1);
         budget[x] = cost[x];
      }
      
      sol = sum - sol;
      
      return;
   }
}
 
signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n;
   cin >> n;
   
   for(int i = 1; i <= n; i++) {
      cin >> father[i] >> height[i] >> cost[i];
      naive[i].emplace_back(father[i]);
      naive[father[i]].emplace_back(i);
   }
   
   ll autoAdd = 0;
   
   for(int i = 1; i <= n; i++) {
      if(solved[i]) continue;
      
      vector<int> nodes;
      g[n + 1].clear(); g[0].clear();
      g[0].emplace_back(n + 1); g[n + 1].emplace_back(0);
      nodes.emplace_back(0); nodes.emplace_back(n + 1);
      
      int x = i;
      while(solved[x] == 0) {
         solved[x] = -1;
         x = father[x];
      }
      int mnh = height[x];
      while(solved[x] == -1) {
         solved[x] = -2;
         mnh = min(mnh, height[x]);
         x = father[x];
      }
      
      
      height[n + 1] = mnh;
      cost[n + 1] = 0;
      while(solved[x] == -2) {
         if(height[x] != height[n + 1]) autoAdd += cost[x];
         else cost[n + 1] += cost[x];
         
         for(auto a : naive[x]) {
            if(solved[a] != -2 && solved[a] != 1) {
               fill(a, x, [&](int node) {
                  nodes.emplace_back(node);
                  if(solved[father[node]] == -2) g[node].emplace_back(n + 1), g[n + 1].emplace_back(node);
                  else g[node].emplace_back(father[node]), g[father[node]].emplace_back(node);
                  solved[node] = 1;
               }, naive);
            }
         }
         solved[x] = 1;
         x = father[x];
      }
      
      //for(auto x : nodes) cerr << x << ' ';
      //cerr << '\n';
      //cerr << autoAdd << '\n';
      DS::startProcedure(nodes);
      autoAdd += DS::sol;
   }
   
   cout << autoAdd << '\n';
}
 
/**
      Istenem! Nu poate fi real.
-- Surse verificate
*/
 
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 3 ms 23228 KB Output is correct
3 Correct 3 ms 23236 KB Output is correct
4 Correct 3 ms 23128 KB Output is correct
5 Correct 7 ms 24348 KB Output is correct
6 Correct 6 ms 23900 KB Output is correct
7 Correct 6 ms 23900 KB Output is correct
8 Correct 7 ms 24308 KB Output is correct
9 Correct 6 ms 23900 KB Output is correct
10 Correct 6 ms 23900 KB Output is correct
11 Correct 6 ms 23832 KB Output is correct
12 Correct 8 ms 24668 KB Output is correct
13 Correct 8 ms 24720 KB Output is correct
14 Correct 9 ms 24308 KB Output is correct
15 Correct 8 ms 24412 KB Output is correct
16 Correct 7 ms 24252 KB Output is correct
17 Correct 6 ms 23896 KB Output is correct
18 Correct 6 ms 23900 KB Output is correct
19 Correct 9 ms 24412 KB Output is correct
20 Correct 8 ms 24452 KB Output is correct
21 Correct 7 ms 24412 KB Output is correct
22 Correct 6 ms 24156 KB Output is correct
23 Correct 5 ms 23900 KB Output is correct
24 Correct 8 ms 24412 KB Output is correct
25 Correct 9 ms 24156 KB Output is correct
26 Correct 6 ms 24568 KB Output is correct
27 Correct 7 ms 24412 KB Output is correct
28 Correct 7 ms 24412 KB Output is correct
29 Correct 7 ms 24412 KB Output is correct
30 Correct 8 ms 24480 KB Output is correct
31 Correct 10 ms 24408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 3 ms 23228 KB Output is correct
3 Correct 3 ms 23236 KB Output is correct
4 Correct 3 ms 23128 KB Output is correct
5 Correct 7 ms 24348 KB Output is correct
6 Correct 6 ms 23900 KB Output is correct
7 Correct 6 ms 23900 KB Output is correct
8 Correct 7 ms 24308 KB Output is correct
9 Correct 6 ms 23900 KB Output is correct
10 Correct 6 ms 23900 KB Output is correct
11 Correct 6 ms 23832 KB Output is correct
12 Correct 8 ms 24668 KB Output is correct
13 Correct 8 ms 24720 KB Output is correct
14 Correct 9 ms 24308 KB Output is correct
15 Correct 8 ms 24412 KB Output is correct
16 Correct 7 ms 24252 KB Output is correct
17 Correct 6 ms 23896 KB Output is correct
18 Correct 6 ms 23900 KB Output is correct
19 Correct 9 ms 24412 KB Output is correct
20 Correct 8 ms 24452 KB Output is correct
21 Correct 7 ms 24412 KB Output is correct
22 Correct 6 ms 24156 KB Output is correct
23 Correct 5 ms 23900 KB Output is correct
24 Correct 8 ms 24412 KB Output is correct
25 Correct 9 ms 24156 KB Output is correct
26 Correct 6 ms 24568 KB Output is correct
27 Correct 7 ms 24412 KB Output is correct
28 Correct 7 ms 24412 KB Output is correct
29 Correct 7 ms 24412 KB Output is correct
30 Correct 8 ms 24480 KB Output is correct
31 Correct 10 ms 24408 KB Output is correct
32 Correct 7 ms 24156 KB Output is correct
33 Correct 313 ms 64172 KB Output is correct
34 Correct 221 ms 52812 KB Output is correct
35 Correct 306 ms 64196 KB Output is correct
36 Correct 218 ms 52688 KB Output is correct
37 Correct 175 ms 51648 KB Output is correct
38 Correct 136 ms 51392 KB Output is correct
39 Correct 460 ms 80576 KB Output is correct
40 Correct 437 ms 80524 KB Output is correct
41 Correct 243 ms 80680 KB Output is correct
42 Correct 449 ms 71360 KB Output is correct
43 Correct 403 ms 71144 KB Output is correct
44 Correct 280 ms 64960 KB Output is correct
45 Correct 159 ms 52928 KB Output is correct
46 Correct 103 ms 52412 KB Output is correct
47 Correct 436 ms 74172 KB Output is correct
48 Correct 413 ms 74172 KB Output is correct
49 Correct 226 ms 72884 KB Output is correct
50 Correct 218 ms 64176 KB Output is correct
51 Correct 107 ms 51644 KB Output is correct
52 Correct 458 ms 72812 KB Output is correct
53 Correct 381 ms 66640 KB Output is correct
54 Correct 176 ms 80776 KB Output is correct
55 Correct 240 ms 72936 KB Output is correct
56 Correct 221 ms 76392 KB Output is correct
57 Correct 200 ms 77756 KB Output is correct
58 Correct 428 ms 70644 KB Output is correct
59 Correct 432 ms 70496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 23132 KB Output is correct
2 Correct 3 ms 23228 KB Output is correct
3 Correct 3 ms 23236 KB Output is correct
4 Correct 3 ms 23128 KB Output is correct
5 Correct 7 ms 24348 KB Output is correct
6 Correct 6 ms 23900 KB Output is correct
7 Correct 6 ms 23900 KB Output is correct
8 Correct 7 ms 24308 KB Output is correct
9 Correct 6 ms 23900 KB Output is correct
10 Correct 6 ms 23900 KB Output is correct
11 Correct 6 ms 23832 KB Output is correct
12 Correct 8 ms 24668 KB Output is correct
13 Correct 8 ms 24720 KB Output is correct
14 Correct 9 ms 24308 KB Output is correct
15 Correct 8 ms 24412 KB Output is correct
16 Correct 7 ms 24252 KB Output is correct
17 Correct 6 ms 23896 KB Output is correct
18 Correct 6 ms 23900 KB Output is correct
19 Correct 9 ms 24412 KB Output is correct
20 Correct 8 ms 24452 KB Output is correct
21 Correct 7 ms 24412 KB Output is correct
22 Correct 6 ms 24156 KB Output is correct
23 Correct 5 ms 23900 KB Output is correct
24 Correct 8 ms 24412 KB Output is correct
25 Correct 9 ms 24156 KB Output is correct
26 Correct 6 ms 24568 KB Output is correct
27 Correct 7 ms 24412 KB Output is correct
28 Correct 7 ms 24412 KB Output is correct
29 Correct 7 ms 24412 KB Output is correct
30 Correct 8 ms 24480 KB Output is correct
31 Correct 10 ms 24408 KB Output is correct
32 Correct 7 ms 24156 KB Output is correct
33 Correct 313 ms 64172 KB Output is correct
34 Correct 221 ms 52812 KB Output is correct
35 Correct 306 ms 64196 KB Output is correct
36 Correct 218 ms 52688 KB Output is correct
37 Correct 175 ms 51648 KB Output is correct
38 Correct 136 ms 51392 KB Output is correct
39 Correct 460 ms 80576 KB Output is correct
40 Correct 437 ms 80524 KB Output is correct
41 Correct 243 ms 80680 KB Output is correct
42 Correct 449 ms 71360 KB Output is correct
43 Correct 403 ms 71144 KB Output is correct
44 Correct 280 ms 64960 KB Output is correct
45 Correct 159 ms 52928 KB Output is correct
46 Correct 103 ms 52412 KB Output is correct
47 Correct 436 ms 74172 KB Output is correct
48 Correct 413 ms 74172 KB Output is correct
49 Correct 226 ms 72884 KB Output is correct
50 Correct 218 ms 64176 KB Output is correct
51 Correct 107 ms 51644 KB Output is correct
52 Correct 458 ms 72812 KB Output is correct
53 Correct 381 ms 66640 KB Output is correct
54 Correct 176 ms 80776 KB Output is correct
55 Correct 240 ms 72936 KB Output is correct
56 Correct 221 ms 76392 KB Output is correct
57 Correct 200 ms 77756 KB Output is correct
58 Correct 428 ms 70644 KB Output is correct
59 Correct 432 ms 70496 KB Output is correct
60 Incorrect 3 ms 23132 KB Output isn't correct
61 Halted 0 ms 0 KB -