답안 #659895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
659895 2022-11-19T16:24:52 Z 600Mihnea Magic Tree (CEOI19_magictree) C++17
100 / 100
688 ms 35604 KB
bool home = 0;
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = (int) 1e5 + 7;
const ll INF = (ll) 1e18 + 7;
int n;
int m;
int k;
int p[N];
int type[N];
int w[N];
int sub[N];
int bg[N];
int low[N];
int high[N];
int ord[N];
int top;
vector<int> g[N];
int Counter = 0;
int last_seen[N];
ll tree[4 * N];
ll lazy_a[4 * N];
ll lazy_b[4 * N];
ll lazy_max[4 * N];

void push(int v, int tl, int tr) {
  if (lazy_a[v] != 1 || lazy_b[v] != 0) {
    tree[v] = tree[v] * lazy_a[v] + lazy_b[v];
    if (tl < tr) {
      for (int v2 = 2 * v; v2 <= 2 * v + 1; v2++) {
        lazy_a[v2] *= lazy_a[v];
        lazy_b[v2] *= lazy_a[v];
        lazy_b[v2] += lazy_b[v];
        lazy_max[v2] = lazy_max[v2] * lazy_a[v] + lazy_b[v];
      }
    }
    lazy_a[v] = 1;
    lazy_b[v] = 0;
  }
  if (lazy_max[v]) {
    tree[v] = max(tree[v], lazy_max[v]);
    if (tl < tr) {
      for (int v2 = 2 * v; v2 <= 2 * v + 1; v2++) {
        lazy_max[v2] = max(lazy_max[v2], lazy_max[v]);
      }
    }
    lazy_max[v] = 0;
  }
}

void pushcoef(int v, int tl, int tr, int l, int r, ll a, ll b) {
  push(v, tl, tr);
  if (tr < l || r < tl) {
    return;
  }
  if (l <= tl && tr <= r) {
    lazy_a[v] = a;
    lazy_b[v] = b;
    push(v, tl, tr);
    return;
  }
  int tm = (tl + tr) / 2;
  pushcoef(2 * v, tl, tm, l, r, a, b);
  pushcoef(2 * v + 1, tm + 1, tr, l, r, a, b);
  tree[v] = max(tree[2 * v], tree[2 * v + 1]);
}

void maxup(int v, int tl, int tr, int l, int r, ll x) {
  push(v, tl, tr);
  if (tr < l || r < tl) {
    return;
  }
  if (l <= tl && tr <= r) {
    lazy_max[v] = x;
    push(v, tl, tr);
    return;
  }
  int tm = (tl + tr) / 2;
  maxup(2 * v, tl, tm, l, r, x);
  maxup(2 * v + 1, tm + 1, tr, l, r, x);
  tree[v] = max(tree[2 * v], tree[2 * v + 1]);
}

ll get_max(int v, int tl, int tr, int l, int r) {
  push(v, tl, tr);
  if (tr < l || r < tl) {
    return -INF;
  }
  if (l <= tl && tr <= r) {
    return tree[v];
  }
  int tm = (tl + tr) / 2;
  return max(get_max(2 * v, tl, tm, l, r), get_max(2 * v + 1, tm + 1, tr, l, r));
}

void pushcoef(int l, int r, ll a, ll b) {
  pushcoef(1, 1, k, l, r, a, b);
}

void maxup(int l, int r, ll x) {
  maxup(1, 1, k, l, r, x);
}

ll get_max(int l, int r) {
  if (l > r) {
    return 0;
  }
  if (l == 0) {
    assert(r == 0);
    return 0;
  }
  assert(1 <= l && l <= r && r <= k);
  return get_max(1, 1, k, l, r);
}

void bu(int a) {
  low[a] = ++top;
  ord[top] = a;
  sub[a] = 1;
  for (auto &b : g[a]) {
    bu(b);
    sub[a] += sub[b];
    if (sub[b] > sub[bg[a]]) {
      bg[a] = b;
    }
  }
  high[a] = top;
}


vector<pair<int, ll>> get_and_clr_dp(int v) {
  Counter++;
  vector<pair<int, ll>> sol;
  for (int j = low[v]; j <= high[v]; j++) {
    if (int i = type[ord[j]]) {
      if (last_seen[i] == Counter) {
        continue;
      }
      last_seen[i] = Counter;
      sol.push_back({i, get_max(i, i) - get_max(i - 1, i - 1)});
    }
  }
  pushcoef(1, k, 0, 0);
  return sol;
}

ll dpaux[N];

void solve(int a) {
  if (g[a].empty()) {
    if (type[a]) {
      pushcoef(type[a], k, 1, w[a]);
    }
  } else {
    assert(bg[a] != -1);
    vector<vector<pair<int, ll>>> all;
    for (auto &b : g[a]) {
      if (b != bg[a]) {
        solve(b);
        all.push_back(get_and_clr_dp(b));
        continue;
      }
    }
    {
      int b = bg[a];
      solve(b);
      for (auto &v : all) {
        for (auto &g : v) {
          pushcoef(g.first, k, 1, g.second);
        }
      }
      if (type[a]) {
        pushcoef(type[a], type[a], 1, w[a]);
        maxup(type[a], k, get_max(type[a], type[a]));
      }
    }
  }
}

int main() {

  if (!home) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  } else {
    freopen("input.txt", "r", stdin);
  }


  for (int i = 0; i < 4 * N; i++) {
    lazy_a[i] = 1;
    lazy_b[i] = 0;
    lazy_max[i] = 0;
  }

  cin >> n >> m >> k;
  if (0) {
    cout << "  : ";
    for (int i = 1; i <= k; i++) {
      cout << i << " ";
    }
    cout << "\n";
  }
  for (int i = 2; i <= n; i++) {
    cin >> p[i];
    g[p[i]].push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    int v;
    cin >> v;
    cin >> type[v];
    cin >> w[v];
  }
  {
    map<int, int> mp;
    for (int i = 1; i <= n; i++) {
      if (type[i]) {
        mp[type[i]] = 0;
      }
    }
    int cr = 0;
    for (auto &it : mp) {
      it.second = ++cr;
    }
    for (int i = 1; i <= n; i++) {
      if (type[i]) {
        type[i] = mp[type[i]];
      }
    }
    k = min(k, m);
  }
  bu(1);
  solve(1);
  cout << get_max(k, k) << "\n";
  return 0;
}
/**

dp[vertex][last_value] = ?



**/

Compilation message

magictree.cpp: In function 'int main()':
magictree.cpp:189:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  189 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12032 KB Output is correct
3 Correct 5 ms 11988 KB Output is correct
4 Correct 6 ms 11996 KB Output is correct
5 Correct 5 ms 11988 KB Output is correct
6 Correct 6 ms 12092 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 191 ms 18348 KB Output is correct
2 Correct 82 ms 22800 KB Output is correct
3 Correct 559 ms 20288 KB Output is correct
4 Correct 239 ms 24728 KB Output is correct
5 Correct 268 ms 26452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12244 KB Output is correct
2 Correct 6 ms 12244 KB Output is correct
3 Correct 9 ms 12208 KB Output is correct
4 Correct 142 ms 33576 KB Output is correct
5 Correct 147 ms 35604 KB Output is correct
6 Correct 195 ms 35464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 16716 KB Output is correct
2 Correct 62 ms 16800 KB Output is correct
3 Correct 69 ms 22660 KB Output is correct
4 Correct 48 ms 20288 KB Output is correct
5 Correct 55 ms 29220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12032 KB Output is correct
3 Correct 5 ms 11988 KB Output is correct
4 Correct 6 ms 11996 KB Output is correct
5 Correct 5 ms 11988 KB Output is correct
6 Correct 6 ms 12092 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
10 Correct 136 ms 16816 KB Output is correct
11 Correct 117 ms 16716 KB Output is correct
12 Correct 88 ms 22608 KB Output is correct
13 Correct 85 ms 20364 KB Output is correct
14 Correct 72 ms 29176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 13036 KB Output is correct
2 Correct 45 ms 16852 KB Output is correct
3 Correct 37 ms 16804 KB Output is correct
4 Correct 45 ms 16892 KB Output is correct
5 Correct 19 ms 18632 KB Output is correct
6 Correct 36 ms 19752 KB Output is correct
7 Correct 40 ms 22348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12032 KB Output is correct
3 Correct 5 ms 11988 KB Output is correct
4 Correct 6 ms 11996 KB Output is correct
5 Correct 5 ms 11988 KB Output is correct
6 Correct 6 ms 12092 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
10 Correct 6 ms 12244 KB Output is correct
11 Correct 6 ms 12244 KB Output is correct
12 Correct 9 ms 12208 KB Output is correct
13 Correct 142 ms 33576 KB Output is correct
14 Correct 147 ms 35604 KB Output is correct
15 Correct 195 ms 35464 KB Output is correct
16 Correct 136 ms 16816 KB Output is correct
17 Correct 117 ms 16716 KB Output is correct
18 Correct 88 ms 22608 KB Output is correct
19 Correct 85 ms 20364 KB Output is correct
20 Correct 72 ms 29176 KB Output is correct
21 Correct 74 ms 13580 KB Output is correct
22 Correct 211 ms 18384 KB Output is correct
23 Correct 339 ms 20696 KB Output is correct
24 Correct 688 ms 22920 KB Output is correct
25 Correct 254 ms 24104 KB Output is correct
26 Correct 319 ms 25444 KB Output is correct
27 Correct 257 ms 28924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12116 KB Output is correct
2 Correct 6 ms 12032 KB Output is correct
3 Correct 5 ms 11988 KB Output is correct
4 Correct 6 ms 11996 KB Output is correct
5 Correct 5 ms 11988 KB Output is correct
6 Correct 6 ms 12092 KB Output is correct
7 Correct 5 ms 11988 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 5 ms 12116 KB Output is correct
10 Correct 191 ms 18348 KB Output is correct
11 Correct 82 ms 22800 KB Output is correct
12 Correct 559 ms 20288 KB Output is correct
13 Correct 239 ms 24728 KB Output is correct
14 Correct 268 ms 26452 KB Output is correct
15 Correct 6 ms 12244 KB Output is correct
16 Correct 6 ms 12244 KB Output is correct
17 Correct 9 ms 12208 KB Output is correct
18 Correct 142 ms 33576 KB Output is correct
19 Correct 147 ms 35604 KB Output is correct
20 Correct 195 ms 35464 KB Output is correct
21 Correct 72 ms 16716 KB Output is correct
22 Correct 62 ms 16800 KB Output is correct
23 Correct 69 ms 22660 KB Output is correct
24 Correct 48 ms 20288 KB Output is correct
25 Correct 55 ms 29220 KB Output is correct
26 Correct 136 ms 16816 KB Output is correct
27 Correct 117 ms 16716 KB Output is correct
28 Correct 88 ms 22608 KB Output is correct
29 Correct 85 ms 20364 KB Output is correct
30 Correct 72 ms 29176 KB Output is correct
31 Correct 16 ms 13036 KB Output is correct
32 Correct 45 ms 16852 KB Output is correct
33 Correct 37 ms 16804 KB Output is correct
34 Correct 45 ms 16892 KB Output is correct
35 Correct 19 ms 18632 KB Output is correct
36 Correct 36 ms 19752 KB Output is correct
37 Correct 40 ms 22348 KB Output is correct
38 Correct 74 ms 13580 KB Output is correct
39 Correct 211 ms 18384 KB Output is correct
40 Correct 339 ms 20696 KB Output is correct
41 Correct 688 ms 22920 KB Output is correct
42 Correct 254 ms 24104 KB Output is correct
43 Correct 319 ms 25444 KB Output is correct
44 Correct 257 ms 28924 KB Output is correct
45 Correct 74 ms 13856 KB Output is correct
46 Correct 215 ms 18656 KB Output is correct
47 Correct 326 ms 21052 KB Output is correct
48 Correct 629 ms 23720 KB Output is correct
49 Correct 250 ms 24804 KB Output is correct
50 Correct 322 ms 26240 KB Output is correct
51 Correct 254 ms 29632 KB Output is correct