#include <iostream>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)
struct Node {
int num, l, r;
};
class SugoyDataStructure {
public:
vector<Node>vec; int size_ = 0; long long sum = 0;
void init() {
vec.push_back(Node{ 0,-1,-1 });
size_ = 1;
}
void add(long long pos, long long x) {
long long cx = 0; pos -= sum;
for (int i = 49; i >= 0; i--) {
vec[cx].num += x;
if (pos >= (1LL << i)) {
if (vec[cx].r >= 0) cx = vec[cx].r;
else { vec.push_back(Node{ 0,-1,-1 }); vec[cx].r = size_; cx = size_; size_++; }
pos -= (1LL << i);
}
else {
if (vec[cx].l >= 0) cx = vec[cx].l;
else { vec.push_back(Node{ 0,-1,-1 }); vec[cx].l = size_; cx = size_; size_++; }
}
}
vec[cx].num += x;
}
int getsize() {
return vec[0].num;
}
long long getvalue(int p) {
long long cx = 0, D = 0;
for (int i = 49; i >= 0; i--) {
if (vec[cx].l >= 0 && p < vec[vec[cx].l].num) cx = vec[cx].l;
else { if (vec[cx].l >= 0) { p -= vec[vec[cx].l].num; } cx = vec[cx].r; D += (1LL << i); }
}
return D + sum;
}
void AddToSum(long long x) {
sum += x;
}
};
long long N, M, A[300009], B[300009], dist[300009], rem; vector<pair<long long, long long>>G[300009];
long long P[300009]; SugoyDataStructure F[300009];
int main() {
cin >> N >> M;
for (int i = 2; i <= N + M; i++) { scanf("%lld%lld", &A[i], &B[i]); rem += B[i]; G[A[i]].push_back(make_pair(i, B[i])); }
for (int i = 1; i <= N + M; i++) { for (int j = 0; j < G[i].size(); j++) dist[G[i][j].first] += dist[i] + G[i][j].second; }
for (int i = N + M; i >= 1; i--) {
if (G[i].size() == 0) {
F[i].init(); P[i] = i;
F[i].add(dist[i], 2);
}
else {
vector<pair<long long, long long>>A; long long res = 0;
for (int j = 0; j < G[i].size(); j++) {
int to = G[i][j].first, L = F[P[to]].getsize();
A.push_back(make_pair(L, P[to])); res += L;
}
sort(A.begin(), A.end(), greater<pair<long long, long long>>());
int S = A[0].second;
if (i == 1) { S = 1; F[S].init(); }
P[i] = S;
for (int j = 0; j < G[i].size(); j++) {
int to = G[i][j].first, v = P[to];
if (v == S) continue;
for (int k = 0; k < F[v].getsize(); k++) F[S].add(F[v].getvalue(k), 1);
}
for (int j = 0; j < G[i].size() - 1; j++) { long long v1 = F[S].getvalue(F[S].getsize() - 1); F[S].add(v1, -1); }
long long D = F[S].getsize();
long long v1 = F[S].getvalue(D - 2), v2 = F[S].getvalue(D - 1);
F[S].AddToSum(-B[i]);
F[S].add(v1 - B[i], -1); F[S].add(v1, 1);
F[S].add(v2 - B[i], -1); F[S].add(v2, 1);
}
//cout << i << ": "; for (int j = 0; j < F[P[i]].getsize(); j++) cout << F[P[i]].getvalue(j) << " "; cout << endl;
}
long long LastAnswer = rem, cx = 0;
for (int i = 0; i < F[1].getsize() - 1; i++) {
long long val = F[1].getvalue(i); //cout << val << endl;
LastAnswer -= (F[1].getsize() - i - 1)*(val - cx); cx = val;
}
cout << LastAnswer << endl;
return 0;
}
Compilation message
fireworks.cpp:7:0: warning: ignoring #pragma warning [-Wunknown-pragmas]
#pragma warning (disable: 4996)
fireworks.cpp: In function 'int main()':
fireworks.cpp:59:55: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i <= N + M; i++) { for (int j = 0; j < G[i].size(); j++) dist[G[i][j].first] += dist[i] + G[i][j].second; }
~~^~~~~~~~~~~~~
fireworks.cpp:68:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[i].size(); j++) {
~~^~~~~~~~~~~~~
fireworks.cpp:77:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[i].size(); j++) {
~~^~~~~~~~~~~~~
fireworks.cpp:83:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < G[i].size() - 1; j++) { long long v1 = F[S].getvalue(F[S].getsize() - 1); F[S].add(v1, -1); }
~~^~~~~~~~~~~~~~~~~
fireworks.cpp:58:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 2; i <= N + M; i++) { scanf("%lld%lld", &A[i], &B[i]); rem += B[i]; G[A[i]].push_back(make_pair(i, B[i])); }
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19192 KB |
Output is correct |
2 |
Correct |
17 ms |
19192 KB |
Output is correct |
3 |
Correct |
16 ms |
19508 KB |
Output is correct |
4 |
Correct |
16 ms |
19508 KB |
Output is correct |
5 |
Correct |
16 ms |
19508 KB |
Output is correct |
6 |
Correct |
17 ms |
19508 KB |
Output is correct |
7 |
Correct |
16 ms |
19508 KB |
Output is correct |
8 |
Correct |
17 ms |
19508 KB |
Output is correct |
9 |
Correct |
16 ms |
19508 KB |
Output is correct |
10 |
Correct |
16 ms |
19508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19508 KB |
Output is correct |
2 |
Correct |
17 ms |
19508 KB |
Output is correct |
3 |
Correct |
17 ms |
19508 KB |
Output is correct |
4 |
Correct |
16 ms |
19508 KB |
Output is correct |
5 |
Correct |
17 ms |
19508 KB |
Output is correct |
6 |
Correct |
17 ms |
19508 KB |
Output is correct |
7 |
Correct |
17 ms |
19508 KB |
Output is correct |
8 |
Correct |
17 ms |
19508 KB |
Output is correct |
9 |
Correct |
18 ms |
19508 KB |
Output is correct |
10 |
Correct |
18 ms |
19508 KB |
Output is correct |
11 |
Correct |
17 ms |
19580 KB |
Output is correct |
12 |
Correct |
17 ms |
19580 KB |
Output is correct |
13 |
Correct |
18 ms |
19580 KB |
Output is correct |
14 |
Correct |
17 ms |
19580 KB |
Output is correct |
15 |
Correct |
18 ms |
19580 KB |
Output is correct |
16 |
Correct |
17 ms |
19580 KB |
Output is correct |
17 |
Correct |
18 ms |
19708 KB |
Output is correct |
18 |
Correct |
17 ms |
19708 KB |
Output is correct |
19 |
Correct |
18 ms |
19708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19192 KB |
Output is correct |
2 |
Correct |
17 ms |
19192 KB |
Output is correct |
3 |
Correct |
16 ms |
19508 KB |
Output is correct |
4 |
Correct |
16 ms |
19508 KB |
Output is correct |
5 |
Correct |
16 ms |
19508 KB |
Output is correct |
6 |
Correct |
17 ms |
19508 KB |
Output is correct |
7 |
Correct |
16 ms |
19508 KB |
Output is correct |
8 |
Correct |
17 ms |
19508 KB |
Output is correct |
9 |
Correct |
16 ms |
19508 KB |
Output is correct |
10 |
Correct |
16 ms |
19508 KB |
Output is correct |
11 |
Correct |
16 ms |
19508 KB |
Output is correct |
12 |
Correct |
17 ms |
19508 KB |
Output is correct |
13 |
Correct |
17 ms |
19508 KB |
Output is correct |
14 |
Correct |
16 ms |
19508 KB |
Output is correct |
15 |
Correct |
17 ms |
19508 KB |
Output is correct |
16 |
Correct |
17 ms |
19508 KB |
Output is correct |
17 |
Correct |
17 ms |
19508 KB |
Output is correct |
18 |
Correct |
17 ms |
19508 KB |
Output is correct |
19 |
Correct |
18 ms |
19508 KB |
Output is correct |
20 |
Correct |
18 ms |
19508 KB |
Output is correct |
21 |
Correct |
17 ms |
19580 KB |
Output is correct |
22 |
Correct |
17 ms |
19580 KB |
Output is correct |
23 |
Correct |
18 ms |
19580 KB |
Output is correct |
24 |
Correct |
17 ms |
19580 KB |
Output is correct |
25 |
Correct |
18 ms |
19580 KB |
Output is correct |
26 |
Correct |
17 ms |
19580 KB |
Output is correct |
27 |
Correct |
18 ms |
19708 KB |
Output is correct |
28 |
Correct |
17 ms |
19708 KB |
Output is correct |
29 |
Correct |
18 ms |
19708 KB |
Output is correct |
30 |
Correct |
21 ms |
20092 KB |
Output is correct |
31 |
Correct |
22 ms |
21244 KB |
Output is correct |
32 |
Correct |
27 ms |
21884 KB |
Output is correct |
33 |
Correct |
28 ms |
22924 KB |
Output is correct |
34 |
Correct |
32 ms |
23928 KB |
Output is correct |
35 |
Correct |
36 ms |
24888 KB |
Output is correct |
36 |
Correct |
36 ms |
25692 KB |
Output is correct |
37 |
Correct |
41 ms |
26908 KB |
Output is correct |
38 |
Correct |
42 ms |
27316 KB |
Output is correct |
39 |
Correct |
41 ms |
27316 KB |
Output is correct |
40 |
Correct |
32 ms |
27316 KB |
Output is correct |
41 |
Correct |
31 ms |
27316 KB |
Output is correct |
42 |
Correct |
32 ms |
27316 KB |
Output is correct |
43 |
Correct |
38 ms |
27316 KB |
Output is correct |
44 |
Correct |
42 ms |
27316 KB |
Output is correct |
45 |
Correct |
40 ms |
27316 KB |
Output is correct |
46 |
Correct |
42 ms |
27316 KB |
Output is correct |
47 |
Correct |
43 ms |
27316 KB |
Output is correct |
48 |
Correct |
38 ms |
27316 KB |
Output is correct |
49 |
Correct |
40 ms |
27316 KB |
Output is correct |
50 |
Correct |
41 ms |
27316 KB |
Output is correct |
51 |
Correct |
36 ms |
27316 KB |
Output is correct |
52 |
Correct |
40 ms |
27316 KB |
Output is correct |
53 |
Correct |
42 ms |
27316 KB |
Output is correct |
54 |
Correct |
39 ms |
27316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19192 KB |
Output is correct |
2 |
Correct |
17 ms |
19192 KB |
Output is correct |
3 |
Correct |
16 ms |
19508 KB |
Output is correct |
4 |
Correct |
16 ms |
19508 KB |
Output is correct |
5 |
Correct |
16 ms |
19508 KB |
Output is correct |
6 |
Correct |
17 ms |
19508 KB |
Output is correct |
7 |
Correct |
16 ms |
19508 KB |
Output is correct |
8 |
Correct |
17 ms |
19508 KB |
Output is correct |
9 |
Correct |
16 ms |
19508 KB |
Output is correct |
10 |
Correct |
16 ms |
19508 KB |
Output is correct |
11 |
Correct |
16 ms |
19508 KB |
Output is correct |
12 |
Correct |
17 ms |
19508 KB |
Output is correct |
13 |
Correct |
17 ms |
19508 KB |
Output is correct |
14 |
Correct |
16 ms |
19508 KB |
Output is correct |
15 |
Correct |
17 ms |
19508 KB |
Output is correct |
16 |
Correct |
17 ms |
19508 KB |
Output is correct |
17 |
Correct |
17 ms |
19508 KB |
Output is correct |
18 |
Correct |
17 ms |
19508 KB |
Output is correct |
19 |
Correct |
18 ms |
19508 KB |
Output is correct |
20 |
Correct |
18 ms |
19508 KB |
Output is correct |
21 |
Correct |
17 ms |
19580 KB |
Output is correct |
22 |
Correct |
17 ms |
19580 KB |
Output is correct |
23 |
Correct |
18 ms |
19580 KB |
Output is correct |
24 |
Correct |
17 ms |
19580 KB |
Output is correct |
25 |
Correct |
18 ms |
19580 KB |
Output is correct |
26 |
Correct |
17 ms |
19580 KB |
Output is correct |
27 |
Correct |
18 ms |
19708 KB |
Output is correct |
28 |
Correct |
17 ms |
19708 KB |
Output is correct |
29 |
Correct |
18 ms |
19708 KB |
Output is correct |
30 |
Correct |
21 ms |
20092 KB |
Output is correct |
31 |
Correct |
22 ms |
21244 KB |
Output is correct |
32 |
Correct |
27 ms |
21884 KB |
Output is correct |
33 |
Correct |
28 ms |
22924 KB |
Output is correct |
34 |
Correct |
32 ms |
23928 KB |
Output is correct |
35 |
Correct |
36 ms |
24888 KB |
Output is correct |
36 |
Correct |
36 ms |
25692 KB |
Output is correct |
37 |
Correct |
41 ms |
26908 KB |
Output is correct |
38 |
Correct |
42 ms |
27316 KB |
Output is correct |
39 |
Correct |
41 ms |
27316 KB |
Output is correct |
40 |
Correct |
32 ms |
27316 KB |
Output is correct |
41 |
Correct |
31 ms |
27316 KB |
Output is correct |
42 |
Correct |
32 ms |
27316 KB |
Output is correct |
43 |
Correct |
38 ms |
27316 KB |
Output is correct |
44 |
Correct |
42 ms |
27316 KB |
Output is correct |
45 |
Correct |
40 ms |
27316 KB |
Output is correct |
46 |
Correct |
42 ms |
27316 KB |
Output is correct |
47 |
Correct |
43 ms |
27316 KB |
Output is correct |
48 |
Correct |
38 ms |
27316 KB |
Output is correct |
49 |
Correct |
40 ms |
27316 KB |
Output is correct |
50 |
Correct |
41 ms |
27316 KB |
Output is correct |
51 |
Correct |
36 ms |
27316 KB |
Output is correct |
52 |
Correct |
40 ms |
27316 KB |
Output is correct |
53 |
Correct |
42 ms |
27316 KB |
Output is correct |
54 |
Correct |
39 ms |
27316 KB |
Output is correct |
55 |
Correct |
97 ms |
39660 KB |
Output is correct |
56 |
Correct |
312 ms |
97036 KB |
Output is correct |
57 |
Correct |
547 ms |
154568 KB |
Output is correct |
58 |
Correct |
694 ms |
190712 KB |
Output is correct |
59 |
Correct |
974 ms |
251968 KB |
Output is correct |
60 |
Correct |
1208 ms |
305804 KB |
Output is correct |
61 |
Correct |
1339 ms |
341872 KB |
Output is correct |
62 |
Correct |
1462 ms |
374028 KB |
Output is correct |
63 |
Correct |
1752 ms |
447952 KB |
Output is correct |
64 |
Correct |
1854 ms |
461576 KB |
Output is correct |
65 |
Correct |
940 ms |
461576 KB |
Output is correct |
66 |
Correct |
928 ms |
461576 KB |
Output is correct |
67 |
Correct |
950 ms |
461576 KB |
Output is correct |
68 |
Correct |
1493 ms |
461576 KB |
Output is correct |
69 |
Correct |
1594 ms |
461576 KB |
Output is correct |
70 |
Correct |
1622 ms |
461576 KB |
Output is correct |
71 |
Correct |
1726 ms |
461740 KB |
Output is correct |
72 |
Correct |
1748 ms |
467352 KB |
Output is correct |
73 |
Correct |
1588 ms |
467352 KB |
Output is correct |
74 |
Correct |
1655 ms |
467352 KB |
Output is correct |
75 |
Correct |
1486 ms |
467352 KB |
Output is correct |
76 |
Correct |
1497 ms |
467352 KB |
Output is correct |
77 |
Correct |
1672 ms |
467352 KB |
Output is correct |
78 |
Correct |
1638 ms |
467352 KB |
Output is correct |
79 |
Correct |
1408 ms |
467352 KB |
Output is correct |