답안 #461181

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
461181 2021-08-09T13:31:55 Z solaimanope Fireworks (APIO16_fireworks) C++17
100 / 100
273 ms 76492 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
template< typename T >
struct SlopeTrick {
  T INF = numeric_limits< T >::max() / 3;
  T min_f, add_l, add_r;
  priority_queue< T, vector< T >, less<> > L;
  priority_queue< T, vector< T >, greater<> > R;
  void push_R(const T &a) { R.push(a - add_r); }
  T top_R() const { return R.top() + add_r; }
  T pop_R() { T val=top_R(); R.pop(); return val;}
  void push_L(const T &a) { L.push(a - add_l); }
  T top_L() const { return L.top() + add_l; }
  T pop_L() { T val=top_L(); L.pop(); return val;}
public:
  SlopeTrick() : min_f(0), add_l(0), add_r(0) {
    L.push(-INF); R.push(INF);
  }
  // f(x) += a
  void add_all(const T &a) {
    min_f += a;
  }
  // add \_ ; f(x) += max(a - x, 0)
  void add_a_minus_x(const T &a) {
    if (a > top_R()) {
      min_f+=a-top_R(); push_L(pop_R());push_R(a);
    } else {
      push_L(a);
    }
  }
  // add _/ ; f(x) += max(x - a, 0)
  void add_x_minus_a(const T &a) {
    if (top_L() > a) {
      min_f+=top_L()-a;push_R(pop_L());push_L(a);
    } else {
      push_R(a);
    }
  }
  // add \/ ; f(x) += max(x - a, 0)
  void add_abs(const T &a) {
    add_a_minus_x(a); add_x_minus_a(a);
  }
  // \/ -> \_ ; f_{new} (x) = min f(y) (y <= x)
  void clear_right(){ while(R.size()>=2)R.pop(); }
  // \/ -> _/ ; f_{new} (x) = min f(y) (y >= x)
  void clear_left(){ while(L.size()>=2) L.pop(); }
  // \/. -> .\/ ; f_{new} (x) = f(x - a)
  void shift(const T &a) { add_l+=a; add_r+=a; }
  T get(const T &x) {
    T ret = min_f;
    if (!L.empty() && x < top_L()) {
      while(!L.empty())ret += max(T(0),pop_L()-x);
    }
    if (!R.empty() && top_R() < x) {
      while(!R.empty())ret += max(T(0),x-pop_R());
    }
    return ret;
  }
};
void SmallToLarge(SlopeTrick<LL> &from,
                  SlopeTrick<LL>&to) {
  if (from.L.size()+from.R.size()>
      to.L.size()+to.R.size()) swap(from, to);
  while (from.L.size() >= 2) {
    to.add_a_minus_x(from.pop_L());
  }
  while (from.R.size() >= 2) {
    to.add_x_minus_a(from.pop_R());
  }
  to.min_f += from.min_f;
}
const int MAXN = 3e5+7; int P[MAXN], C[MAXN];
vector<int>child[MAXN];SlopeTrick<LL> trick[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m; cin >> n >> m;
  for (int i = 2; i <= n+m; i++) {
    cin >> P[i] >> C[i];child[P[i]].push_back(i);
  }
  for (int i = n+1; i <= n+m; i++) {
    trick[i].add_abs(C[i]);
  }
  for (int i = n; i > 0; i--) {
    for (int c : child[i]) {
      SmallToLarge(trick[c], trick[i]);
    }
    { ///clearing all slopes greater than 1
      LL save = trick[i].top_R();
      trick[i].clear_right();
      trick[i].push_R(save);
    }
    if (i > 1) {
      trick[i].push_L(trick[i].pop_L()+C[i]);
      trick[i].push_R(trick[i].pop_R()+C[i]);
    }
  }
  cout << trick[1].min_f << "\n";
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 54400 KB Output is correct
2 Correct 52 ms 54320 KB Output is correct
3 Correct 50 ms 54244 KB Output is correct
4 Correct 50 ms 54280 KB Output is correct
5 Correct 54 ms 54204 KB Output is correct
6 Correct 50 ms 54252 KB Output is correct
7 Correct 51 ms 54272 KB Output is correct
8 Correct 61 ms 54260 KB Output is correct
9 Correct 55 ms 54304 KB Output is correct
10 Correct 54 ms 54300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 54320 KB Output is correct
2 Correct 50 ms 54304 KB Output is correct
3 Correct 52 ms 54320 KB Output is correct
4 Correct 52 ms 54228 KB Output is correct
5 Correct 50 ms 54336 KB Output is correct
6 Correct 51 ms 54328 KB Output is correct
7 Correct 52 ms 54340 KB Output is correct
8 Correct 52 ms 54212 KB Output is correct
9 Correct 51 ms 54320 KB Output is correct
10 Correct 50 ms 54212 KB Output is correct
11 Correct 52 ms 54332 KB Output is correct
12 Correct 50 ms 54212 KB Output is correct
13 Correct 50 ms 54308 KB Output is correct
14 Correct 50 ms 54328 KB Output is correct
15 Correct 52 ms 54340 KB Output is correct
16 Correct 51 ms 54212 KB Output is correct
17 Correct 52 ms 54288 KB Output is correct
18 Correct 52 ms 54260 KB Output is correct
19 Correct 53 ms 54264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 54400 KB Output is correct
2 Correct 52 ms 54320 KB Output is correct
3 Correct 50 ms 54244 KB Output is correct
4 Correct 50 ms 54280 KB Output is correct
5 Correct 54 ms 54204 KB Output is correct
6 Correct 50 ms 54252 KB Output is correct
7 Correct 51 ms 54272 KB Output is correct
8 Correct 61 ms 54260 KB Output is correct
9 Correct 55 ms 54304 KB Output is correct
10 Correct 54 ms 54300 KB Output is correct
11 Correct 57 ms 54320 KB Output is correct
12 Correct 50 ms 54304 KB Output is correct
13 Correct 52 ms 54320 KB Output is correct
14 Correct 52 ms 54228 KB Output is correct
15 Correct 50 ms 54336 KB Output is correct
16 Correct 51 ms 54328 KB Output is correct
17 Correct 52 ms 54340 KB Output is correct
18 Correct 52 ms 54212 KB Output is correct
19 Correct 51 ms 54320 KB Output is correct
20 Correct 50 ms 54212 KB Output is correct
21 Correct 52 ms 54332 KB Output is correct
22 Correct 50 ms 54212 KB Output is correct
23 Correct 50 ms 54308 KB Output is correct
24 Correct 50 ms 54328 KB Output is correct
25 Correct 52 ms 54340 KB Output is correct
26 Correct 51 ms 54212 KB Output is correct
27 Correct 52 ms 54288 KB Output is correct
28 Correct 52 ms 54260 KB Output is correct
29 Correct 53 ms 54264 KB Output is correct
30 Correct 50 ms 54352 KB Output is correct
31 Correct 54 ms 54468 KB Output is correct
32 Correct 61 ms 54404 KB Output is correct
33 Correct 59 ms 54444 KB Output is correct
34 Correct 63 ms 54480 KB Output is correct
35 Correct 78 ms 54492 KB Output is correct
36 Correct 55 ms 54568 KB Output is correct
37 Correct 60 ms 54644 KB Output is correct
38 Correct 62 ms 54632 KB Output is correct
39 Correct 60 ms 54588 KB Output is correct
40 Correct 53 ms 54584 KB Output is correct
41 Correct 52 ms 54692 KB Output is correct
42 Correct 54 ms 54584 KB Output is correct
43 Correct 60 ms 54480 KB Output is correct
44 Correct 61 ms 54572 KB Output is correct
45 Correct 58 ms 54556 KB Output is correct
46 Correct 60 ms 54676 KB Output is correct
47 Correct 61 ms 54568 KB Output is correct
48 Correct 60 ms 54536 KB Output is correct
49 Correct 62 ms 54632 KB Output is correct
50 Correct 61 ms 54500 KB Output is correct
51 Correct 63 ms 54704 KB Output is correct
52 Correct 61 ms 54596 KB Output is correct
53 Correct 61 ms 54552 KB Output is correct
54 Correct 66 ms 54612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 54400 KB Output is correct
2 Correct 52 ms 54320 KB Output is correct
3 Correct 50 ms 54244 KB Output is correct
4 Correct 50 ms 54280 KB Output is correct
5 Correct 54 ms 54204 KB Output is correct
6 Correct 50 ms 54252 KB Output is correct
7 Correct 51 ms 54272 KB Output is correct
8 Correct 61 ms 54260 KB Output is correct
9 Correct 55 ms 54304 KB Output is correct
10 Correct 54 ms 54300 KB Output is correct
11 Correct 57 ms 54320 KB Output is correct
12 Correct 50 ms 54304 KB Output is correct
13 Correct 52 ms 54320 KB Output is correct
14 Correct 52 ms 54228 KB Output is correct
15 Correct 50 ms 54336 KB Output is correct
16 Correct 51 ms 54328 KB Output is correct
17 Correct 52 ms 54340 KB Output is correct
18 Correct 52 ms 54212 KB Output is correct
19 Correct 51 ms 54320 KB Output is correct
20 Correct 50 ms 54212 KB Output is correct
21 Correct 52 ms 54332 KB Output is correct
22 Correct 50 ms 54212 KB Output is correct
23 Correct 50 ms 54308 KB Output is correct
24 Correct 50 ms 54328 KB Output is correct
25 Correct 52 ms 54340 KB Output is correct
26 Correct 51 ms 54212 KB Output is correct
27 Correct 52 ms 54288 KB Output is correct
28 Correct 52 ms 54260 KB Output is correct
29 Correct 53 ms 54264 KB Output is correct
30 Correct 50 ms 54352 KB Output is correct
31 Correct 54 ms 54468 KB Output is correct
32 Correct 61 ms 54404 KB Output is correct
33 Correct 59 ms 54444 KB Output is correct
34 Correct 63 ms 54480 KB Output is correct
35 Correct 78 ms 54492 KB Output is correct
36 Correct 55 ms 54568 KB Output is correct
37 Correct 60 ms 54644 KB Output is correct
38 Correct 62 ms 54632 KB Output is correct
39 Correct 60 ms 54588 KB Output is correct
40 Correct 53 ms 54584 KB Output is correct
41 Correct 52 ms 54692 KB Output is correct
42 Correct 54 ms 54584 KB Output is correct
43 Correct 60 ms 54480 KB Output is correct
44 Correct 61 ms 54572 KB Output is correct
45 Correct 58 ms 54556 KB Output is correct
46 Correct 60 ms 54676 KB Output is correct
47 Correct 61 ms 54568 KB Output is correct
48 Correct 60 ms 54536 KB Output is correct
49 Correct 62 ms 54632 KB Output is correct
50 Correct 61 ms 54500 KB Output is correct
51 Correct 63 ms 54704 KB Output is correct
52 Correct 61 ms 54596 KB Output is correct
53 Correct 61 ms 54552 KB Output is correct
54 Correct 66 ms 54612 KB Output is correct
55 Correct 67 ms 55124 KB Output is correct
56 Correct 90 ms 57832 KB Output is correct
57 Correct 116 ms 59840 KB Output is correct
58 Correct 133 ms 61520 KB Output is correct
59 Correct 168 ms 63768 KB Output is correct
60 Correct 186 ms 65984 KB Output is correct
61 Correct 209 ms 68124 KB Output is correct
62 Correct 233 ms 69372 KB Output is correct
63 Correct 261 ms 72228 KB Output is correct
64 Correct 273 ms 73020 KB Output is correct
65 Correct 170 ms 70928 KB Output is correct
66 Correct 168 ms 70876 KB Output is correct
67 Correct 171 ms 70864 KB Output is correct
68 Correct 203 ms 68364 KB Output is correct
69 Correct 229 ms 68312 KB Output is correct
70 Correct 249 ms 68268 KB Output is correct
71 Correct 267 ms 76492 KB Output is correct
72 Correct 265 ms 76320 KB Output is correct
73 Correct 246 ms 72780 KB Output is correct
74 Correct 255 ms 72984 KB Output is correct
75 Correct 251 ms 72160 KB Output is correct
76 Correct 245 ms 71928 KB Output is correct
77 Correct 263 ms 71196 KB Output is correct
78 Correct 273 ms 70308 KB Output is correct
79 Correct 249 ms 69564 KB Output is correct