Submission #1109492

# Submission time Handle Problem Language Result Execution time Memory
1109492 2024-11-06T23:49:56 Z nicksms Tree (IOI24_tree) C++17
100 / 100
213 ms 80108 KB
#include "tree.h"

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using db=long double;
template<class T> using V=vector<T>;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<int,int>;
#define f first
#define s second
#define sz(x) (int)((x).size())
#define each(a,b) for (auto &a : b)

const int MAXN = 1<<18;

int j[18][MAXN], jm[18][MAXN];
ll w[MAXN];
ll ans[MAXN], csum[MAXN], a0;
vi k[MAXN], legs[MAXN];

void dfs(int i, int p) {
  a0 += w[i]*(csum[i] = abs(1-sz(k[i])));
  if (sz(k[i]) == 0) return;
  j[0][i] = p;
  jm[0][i] = min(w[i], w[p]);
  for (int b = 1; b < 18; b++) {
    j[b][i] = j[b-1][j[b-1][i]];
    jm[b][i] = min(jm[b-1][i], jm[b-1][j[b-1][i]]);
  }
  for (int j : k[i]) dfs(j, i);

  if (w[i]) {
    int cur = i;
    for (int b = 17; b >= 0; b--) {
      if (jm[b][cur] >= w[i]) cur = j[b][cur];
    }
    legs[cur].push_back(i);
    csum[j[0][cur]] += csum[i];
  }

  sort(legs[i].begin(), legs[i].end(), [&](int a, int b){
    return w[a] > w[b];
  });
  int last = 0;
  for (int j : legs[i]) {
    ans[last+1] -= w[j]-w[p];
    ans[last+1+csum[j]] += w[j]-w[p];
    last = last+csum[j];
  }
}

void init(std::vector<int> P, std::vector<int> W) {
  for (int i = 0; i < P.size(); i++) {
    k[P[i]+1].push_back(i+1);
    w[i+1]=W[i];
  }
  dfs(1, 0);
  for (int i = 1; i < MAXN; i++) ans[i] += ans[i-1];
  ans[0] += a0;
  for (int i = 1; i < MAXN; i++) ans[i] += ans[i-1];
}

long long query(int L, int R) {
  if (L != 1) {
    int Q = R/L, S = R%L;
    return (L-S)*query(1, Q) + S*query(1, Q+1);
  }
  return ans[min(R-1, MAXN-1)];
}

// int main() {
//   init(vi{-1,0,0}, vi{1,1,1});
//   cout << query(1,1);
//   cout << query(1,2);
// }

Compilation message

tree.cpp: In function 'void init(std::vector<int>, std::vector<int>)':
tree.cpp:55:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |   for (int i = 0; i < P.size(); i++) {
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 53840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 79252 KB Output is correct
2 Correct 138 ms 75600 KB Output is correct
3 Correct 99 ms 70332 KB Output is correct
4 Correct 145 ms 68936 KB Output is correct
5 Correct 138 ms 68748 KB Output is correct
6 Correct 134 ms 64712 KB Output is correct
7 Correct 145 ms 68744 KB Output is correct
8 Correct 153 ms 68756 KB Output is correct
9 Correct 133 ms 67216 KB Output is correct
10 Correct 36 ms 59588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 54156 KB Output is correct
2 Correct 10 ms 54096 KB Output is correct
3 Correct 9 ms 54096 KB Output is correct
4 Correct 8 ms 53840 KB Output is correct
5 Correct 9 ms 53840 KB Output is correct
6 Correct 9 ms 53840 KB Output is correct
7 Correct 8 ms 54052 KB Output is correct
8 Correct 7 ms 53840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 54156 KB Output is correct
2 Correct 10 ms 54096 KB Output is correct
3 Correct 9 ms 54096 KB Output is correct
4 Correct 8 ms 53840 KB Output is correct
5 Correct 9 ms 53840 KB Output is correct
6 Correct 9 ms 53840 KB Output is correct
7 Correct 8 ms 54052 KB Output is correct
8 Correct 7 ms 53840 KB Output is correct
9 Correct 48 ms 60744 KB Output is correct
10 Correct 46 ms 60952 KB Output is correct
11 Correct 43 ms 60488 KB Output is correct
12 Correct 51 ms 60928 KB Output is correct
13 Correct 42 ms 59960 KB Output is correct
14 Correct 39 ms 59464 KB Output is correct
15 Correct 45 ms 57672 KB Output is correct
16 Correct 49 ms 57680 KB Output is correct
17 Correct 48 ms 57416 KB Output is correct
18 Correct 47 ms 57704 KB Output is correct
19 Correct 51 ms 57548 KB Output is correct
20 Correct 43 ms 57160 KB Output is correct
21 Correct 40 ms 57100 KB Output is correct
22 Correct 15 ms 55756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 77856 KB Output is correct
2 Correct 146 ms 66984 KB Output is correct
3 Correct 150 ms 66984 KB Output is correct
4 Correct 150 ms 66992 KB Output is correct
5 Correct 155 ms 66976 KB Output is correct
6 Correct 197 ms 69568 KB Output is correct
7 Correct 143 ms 65960 KB Output is correct
8 Correct 59 ms 61028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 77856 KB Output is correct
2 Correct 146 ms 66984 KB Output is correct
3 Correct 150 ms 66984 KB Output is correct
4 Correct 150 ms 66992 KB Output is correct
5 Correct 155 ms 66976 KB Output is correct
6 Correct 197 ms 69568 KB Output is correct
7 Correct 143 ms 65960 KB Output is correct
8 Correct 59 ms 61028 KB Output is correct
9 Correct 117 ms 70772 KB Output is correct
10 Correct 158 ms 67624 KB Output is correct
11 Correct 150 ms 67368 KB Output is correct
12 Correct 148 ms 67312 KB Output is correct
13 Correct 198 ms 70148 KB Output is correct
14 Correct 135 ms 66344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 76524 KB Output is correct
2 Correct 123 ms 73000 KB Output is correct
3 Correct 161 ms 68980 KB Output is correct
4 Correct 174 ms 68976 KB Output is correct
5 Correct 165 ms 69160 KB Output is correct
6 Correct 171 ms 68904 KB Output is correct
7 Correct 166 ms 68844 KB Output is correct
8 Correct 213 ms 72232 KB Output is correct
9 Correct 150 ms 67892 KB Output is correct
10 Correct 157 ms 67716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 53840 KB Output is correct
2 Correct 138 ms 79252 KB Output is correct
3 Correct 138 ms 75600 KB Output is correct
4 Correct 99 ms 70332 KB Output is correct
5 Correct 145 ms 68936 KB Output is correct
6 Correct 138 ms 68748 KB Output is correct
7 Correct 134 ms 64712 KB Output is correct
8 Correct 145 ms 68744 KB Output is correct
9 Correct 153 ms 68756 KB Output is correct
10 Correct 133 ms 67216 KB Output is correct
11 Correct 36 ms 59588 KB Output is correct
12 Correct 10 ms 54156 KB Output is correct
13 Correct 10 ms 54096 KB Output is correct
14 Correct 9 ms 54096 KB Output is correct
15 Correct 8 ms 53840 KB Output is correct
16 Correct 9 ms 53840 KB Output is correct
17 Correct 9 ms 53840 KB Output is correct
18 Correct 8 ms 54052 KB Output is correct
19 Correct 7 ms 53840 KB Output is correct
20 Correct 48 ms 60744 KB Output is correct
21 Correct 46 ms 60952 KB Output is correct
22 Correct 43 ms 60488 KB Output is correct
23 Correct 51 ms 60928 KB Output is correct
24 Correct 42 ms 59960 KB Output is correct
25 Correct 39 ms 59464 KB Output is correct
26 Correct 45 ms 57672 KB Output is correct
27 Correct 49 ms 57680 KB Output is correct
28 Correct 48 ms 57416 KB Output is correct
29 Correct 47 ms 57704 KB Output is correct
30 Correct 51 ms 57548 KB Output is correct
31 Correct 43 ms 57160 KB Output is correct
32 Correct 40 ms 57100 KB Output is correct
33 Correct 15 ms 55756 KB Output is correct
34 Correct 151 ms 77856 KB Output is correct
35 Correct 146 ms 66984 KB Output is correct
36 Correct 150 ms 66984 KB Output is correct
37 Correct 150 ms 66992 KB Output is correct
38 Correct 155 ms 66976 KB Output is correct
39 Correct 197 ms 69568 KB Output is correct
40 Correct 143 ms 65960 KB Output is correct
41 Correct 59 ms 61028 KB Output is correct
42 Correct 117 ms 70772 KB Output is correct
43 Correct 158 ms 67624 KB Output is correct
44 Correct 150 ms 67368 KB Output is correct
45 Correct 148 ms 67312 KB Output is correct
46 Correct 198 ms 70148 KB Output is correct
47 Correct 135 ms 66344 KB Output is correct
48 Correct 137 ms 76524 KB Output is correct
49 Correct 123 ms 73000 KB Output is correct
50 Correct 161 ms 68980 KB Output is correct
51 Correct 174 ms 68976 KB Output is correct
52 Correct 165 ms 69160 KB Output is correct
53 Correct 171 ms 68904 KB Output is correct
54 Correct 166 ms 68844 KB Output is correct
55 Correct 213 ms 72232 KB Output is correct
56 Correct 150 ms 67892 KB Output is correct
57 Correct 157 ms 67716 KB Output is correct
58 Correct 160 ms 78464 KB Output is correct
59 Correct 166 ms 80108 KB Output is correct
60 Correct 101 ms 68904 KB Output is correct
61 Correct 132 ms 74028 KB Output is correct
62 Correct 171 ms 70252 KB Output is correct
63 Correct 176 ms 68404 KB Output is correct
64 Correct 181 ms 70252 KB Output is correct
65 Correct 193 ms 70000 KB Output is correct
66 Correct 173 ms 70196 KB Output is correct
67 Correct 172 ms 69928 KB Output is correct
68 Correct 213 ms 73256 KB Output is correct
69 Correct 157 ms 68996 KB Output is correct
70 Correct 144 ms 67368 KB Output is correct
71 Correct 60 ms 62624 KB Output is correct