Submission #993914

# Submission time Handle Problem Language Result Execution time Memory
993914 2024-06-06T20:14:10 Z cadmiumsky Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
632 ms 100248 KB
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#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 p[nmax][18], d[nmax];
   int budget[nmax];
   
   void fill(int a, int f) {
      
      //cerr << a << ' ' << f << '\n';
      
      d[a] = d[f] + 1;
      p[a][0] = f;
      for(int i = 1; i < 18; i++) p[a][i] = p[p[a][i - 1]][i - 1];
      
      pin[a] = inp++;
      for(auto x : g[a])
         if(x != f) 
            fill(x, a);
      pout[a] = inp;
      return;
   }
 
   void startProcedure(vector<int> nodes, vector<int> cycles) {
      height[0] = -inf;
      sol = 0;
      inp = 1;
      fill(0, 0);
      
      map<int, ll> cyclefree;
      
      for(auto x : cycles) solved[x] = 100, d[x] = -1;
      copy(all(cycles), back_inserter(nodes));
      
      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])];
      for(auto x : nodes) {
         if(solved[x] == 100)
            cyclefree[height[x]] += cost[x];
      }
      
      occupied.init(inp);
      
      //cerr << "wow\n";
      
      ll sum = 0, maxpossible = 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); };
      
      for(auto x : nodes) {
         int specialed = 0;
         if(solved[x] == 100) {
            specialed = cyclefree[height[x]];
         }
         else {
            int mine = anc(x), upward = x, mybudget = cost[x];
            
            while(mybudget > 0 && mine > 0) {
               for(int i = 17; i >= 0; i--)
                  if(anc(p[upward][i]) == mine) upward = p[upward][i];
               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];
         }
         
         maxpossible = max(maxpossible, sol + specialed);
      }
      
      for(auto x : nodes) solved[x] = 1;
      
      sol = sum - maxpossible;
      
      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, cycles;
      g[n + 1].clear(); g[0].clear();
      g[0].emplace_back(n + 1); g[n + 1].emplace_back(0);
      
      int x = i;
      while(solved[x] == 0) {
         solved[x] = -2;
         x = father[x];
      }
      while(solved[x] == -2) {
         solved[x] = -1;
         x = father[x];
      }
      
      height[n + 1] = -inf;
      cost[n + 1] = 0;
      while(solved[x] == -1) {
         cycles.emplace_back(x);
         
         for(auto a : naive[x]) {
            if(solved[a] != -1 && solved[a] != 1) {
               fill(a, x, [&](int node) {
                  nodes.emplace_back(node);
                  if(solved[father[node]] == -1) 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];
      }
      
      DS::startProcedure(nodes, cycles);
      autoAdd += DS::sol;
   }
   
   cout << autoAdd << '\n';
}
 
/**
      Istenem! Nu poate fi real.
-- Surse verificate
*/
 
# Verdict Execution time Memory Grader output
1 Correct 4 ms 23640 KB Output is correct
2 Correct 5 ms 23644 KB Output is correct
3 Correct 4 ms 23644 KB Output is correct
4 Correct 4 ms 23644 KB Output is correct
5 Correct 8 ms 24432 KB Output is correct
6 Correct 8 ms 24152 KB Output is correct
7 Correct 7 ms 24156 KB Output is correct
8 Correct 8 ms 24500 KB Output is correct
9 Correct 8 ms 24228 KB Output is correct
10 Correct 8 ms 24156 KB Output is correct
11 Correct 10 ms 24276 KB Output is correct
12 Correct 11 ms 24924 KB Output is correct
13 Correct 11 ms 24948 KB Output is correct
14 Correct 10 ms 24668 KB Output is correct
15 Correct 11 ms 24668 KB Output is correct
16 Correct 8 ms 24408 KB Output is correct
17 Correct 7 ms 24156 KB Output is correct
18 Correct 7 ms 24156 KB Output is correct
19 Correct 9 ms 24664 KB Output is correct
20 Correct 9 ms 24668 KB Output is correct
21 Correct 8 ms 24760 KB Output is correct
22 Correct 7 ms 24412 KB Output is correct
23 Correct 6 ms 24248 KB Output is correct
24 Correct 9 ms 24668 KB Output is correct
25 Correct 8 ms 24512 KB Output is correct
26 Correct 8 ms 24924 KB Output is correct
27 Correct 8 ms 24668 KB Output is correct
28 Correct 8 ms 24668 KB Output is correct
29 Correct 8 ms 24920 KB Output is correct
30 Correct 8 ms 24748 KB Output is correct
31 Correct 9 ms 24668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 23640 KB Output is correct
2 Correct 5 ms 23644 KB Output is correct
3 Correct 4 ms 23644 KB Output is correct
4 Correct 4 ms 23644 KB Output is correct
5 Correct 8 ms 24432 KB Output is correct
6 Correct 8 ms 24152 KB Output is correct
7 Correct 7 ms 24156 KB Output is correct
8 Correct 8 ms 24500 KB Output is correct
9 Correct 8 ms 24228 KB Output is correct
10 Correct 8 ms 24156 KB Output is correct
11 Correct 10 ms 24276 KB Output is correct
12 Correct 11 ms 24924 KB Output is correct
13 Correct 11 ms 24948 KB Output is correct
14 Correct 10 ms 24668 KB Output is correct
15 Correct 11 ms 24668 KB Output is correct
16 Correct 8 ms 24408 KB Output is correct
17 Correct 7 ms 24156 KB Output is correct
18 Correct 7 ms 24156 KB Output is correct
19 Correct 9 ms 24664 KB Output is correct
20 Correct 9 ms 24668 KB Output is correct
21 Correct 8 ms 24760 KB Output is correct
22 Correct 7 ms 24412 KB Output is correct
23 Correct 6 ms 24248 KB Output is correct
24 Correct 9 ms 24668 KB Output is correct
25 Correct 8 ms 24512 KB Output is correct
26 Correct 8 ms 24924 KB Output is correct
27 Correct 8 ms 24668 KB Output is correct
28 Correct 8 ms 24668 KB Output is correct
29 Correct 8 ms 24920 KB Output is correct
30 Correct 8 ms 24748 KB Output is correct
31 Correct 9 ms 24668 KB Output is correct
32 Correct 8 ms 24408 KB Output is correct
33 Correct 351 ms 84060 KB Output is correct
34 Correct 265 ms 71324 KB Output is correct
35 Correct 426 ms 82712 KB Output is correct
36 Correct 277 ms 72608 KB Output is correct
37 Correct 201 ms 71588 KB Output is correct
38 Correct 140 ms 70588 KB Output is correct
39 Correct 613 ms 98972 KB Output is correct
40 Correct 609 ms 100124 KB Output is correct
41 Correct 365 ms 100248 KB Output is correct
42 Correct 632 ms 89504 KB Output is correct
43 Correct 551 ms 89500 KB Output is correct
44 Correct 325 ms 84980 KB Output is correct
45 Correct 206 ms 72356 KB Output is correct
46 Correct 107 ms 71468 KB Output is correct
47 Correct 430 ms 93344 KB Output is correct
48 Correct 429 ms 93088 KB Output is correct
49 Correct 249 ms 92552 KB Output is correct
50 Correct 238 ms 83360 KB Output is correct
51 Correct 130 ms 70812 KB Output is correct
52 Correct 402 ms 92324 KB Output is correct
53 Correct 388 ms 86432 KB Output is correct
54 Correct 226 ms 99932 KB Output is correct
55 Correct 351 ms 92060 KB Output is correct
56 Correct 337 ms 95672 KB Output is correct
57 Correct 285 ms 98204 KB Output is correct
58 Correct 331 ms 90780 KB Output is correct
59 Correct 303 ms 90528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 23640 KB Output is correct
2 Correct 5 ms 23644 KB Output is correct
3 Correct 4 ms 23644 KB Output is correct
4 Correct 4 ms 23644 KB Output is correct
5 Correct 8 ms 24432 KB Output is correct
6 Correct 8 ms 24152 KB Output is correct
7 Correct 7 ms 24156 KB Output is correct
8 Correct 8 ms 24500 KB Output is correct
9 Correct 8 ms 24228 KB Output is correct
10 Correct 8 ms 24156 KB Output is correct
11 Correct 10 ms 24276 KB Output is correct
12 Correct 11 ms 24924 KB Output is correct
13 Correct 11 ms 24948 KB Output is correct
14 Correct 10 ms 24668 KB Output is correct
15 Correct 11 ms 24668 KB Output is correct
16 Correct 8 ms 24408 KB Output is correct
17 Correct 7 ms 24156 KB Output is correct
18 Correct 7 ms 24156 KB Output is correct
19 Correct 9 ms 24664 KB Output is correct
20 Correct 9 ms 24668 KB Output is correct
21 Correct 8 ms 24760 KB Output is correct
22 Correct 7 ms 24412 KB Output is correct
23 Correct 6 ms 24248 KB Output is correct
24 Correct 9 ms 24668 KB Output is correct
25 Correct 8 ms 24512 KB Output is correct
26 Correct 8 ms 24924 KB Output is correct
27 Correct 8 ms 24668 KB Output is correct
28 Correct 8 ms 24668 KB Output is correct
29 Correct 8 ms 24920 KB Output is correct
30 Correct 8 ms 24748 KB Output is correct
31 Correct 9 ms 24668 KB Output is correct
32 Correct 8 ms 24408 KB Output is correct
33 Correct 351 ms 84060 KB Output is correct
34 Correct 265 ms 71324 KB Output is correct
35 Correct 426 ms 82712 KB Output is correct
36 Correct 277 ms 72608 KB Output is correct
37 Correct 201 ms 71588 KB Output is correct
38 Correct 140 ms 70588 KB Output is correct
39 Correct 613 ms 98972 KB Output is correct
40 Correct 609 ms 100124 KB Output is correct
41 Correct 365 ms 100248 KB Output is correct
42 Correct 632 ms 89504 KB Output is correct
43 Correct 551 ms 89500 KB Output is correct
44 Correct 325 ms 84980 KB Output is correct
45 Correct 206 ms 72356 KB Output is correct
46 Correct 107 ms 71468 KB Output is correct
47 Correct 430 ms 93344 KB Output is correct
48 Correct 429 ms 93088 KB Output is correct
49 Correct 249 ms 92552 KB Output is correct
50 Correct 238 ms 83360 KB Output is correct
51 Correct 130 ms 70812 KB Output is correct
52 Correct 402 ms 92324 KB Output is correct
53 Correct 388 ms 86432 KB Output is correct
54 Correct 226 ms 99932 KB Output is correct
55 Correct 351 ms 92060 KB Output is correct
56 Correct 337 ms 95672 KB Output is correct
57 Correct 285 ms 98204 KB Output is correct
58 Correct 331 ms 90780 KB Output is correct
59 Correct 303 ms 90528 KB Output is correct
60 Correct 4 ms 23644 KB Output is correct
61 Correct 4 ms 23640 KB Output is correct
62 Correct 4 ms 23644 KB Output is correct
63 Correct 4 ms 23644 KB Output is correct
64 Correct 427 ms 81956 KB Output is correct
65 Correct 372 ms 74304 KB Output is correct
66 Correct 386 ms 78732 KB Output is correct
67 Correct 437 ms 81100 KB Output is correct
68 Correct 250 ms 69688 KB Output is correct
69 Correct 185 ms 66056 KB Output is correct
70 Correct 305 ms 50584 KB Output is correct
71 Correct 141 ms 33248 KB Output is correct
72 Correct 325 ms 59760 KB Output is correct
73 Correct 130 ms 36036 KB Output is correct
74 Correct 324 ms 85672 KB Output is correct
75 Correct 153 ms 67228 KB Output is correct
76 Correct 124 ms 66856 KB Output is correct
77 Correct 297 ms 84916 KB Output is correct
78 Correct 133 ms 66228 KB Output is correct
79 Correct 348 ms 84544 KB Output is correct
80 Correct 242 ms 66744 KB Output is correct
81 Correct 150 ms 65724 KB Output is correct
82 Correct 221 ms 59884 KB Output is correct
83 Correct 80 ms 29976 KB Output is correct
84 Correct 310 ms 89528 KB Output is correct
85 Correct 304 ms 89520 KB Output is correct
86 Correct 291 ms 83376 KB Output is correct
87 Correct 333 ms 89552 KB Output is correct
88 Correct 309 ms 89464 KB Output is correct