#include <bits/stdc++.h>
#define int long long
using namespace std;
using Point = pair<int, int>;
using idata = vector<int>;
struct Node {
int pos, delta, rng;
int ssize = 1;
int sdelt = 0;
Node* left = nullptr; Node* right = nullptr;
void compute () {
ssize = 1;
sdelt = delta;
if (left != nullptr) {
ssize += left->ssize;
sdelt += left->sdelt;
}
if (right != nullptr) {
ssize += right->ssize;
sdelt += right->sdelt;
}
}
};
struct Treap {
Node* root = nullptr;
static void __show (Node* node, int depth) {
if (node == nullptr) {
for (int i = 0; i < depth; i ++) cout << " ";
cout << ">\n";
return ;
}
for (int i = 0; i < depth; i ++) cout << " ";
cout << "> " << node->pos << " " << node->delta << " " << node->ssize << " " << node->sdelt << endl;
__show(node->left, depth + 1);
__show(node->right, depth + 1);
}
void show () {
__show(root, 0);
}
static Node* __merge (Node* A, Node* B) {
if (A == nullptr) return B;
if (B == nullptr) return A;
if (A->rng >= B->rng) {
A->right = __merge(A->right, B);
A->compute();
return A;
} else {
B->left = __merge(A, B->left);
B->compute();
return B;
}
}
static pair<Node*, Node*> __split (Node* A, int k) {
if (A == nullptr) return { nullptr, nullptr };
if (A->pos > k) {
const auto [LL, LR] = __split(A->left, k);
A->left = LR;
A->compute();
return { LL, A };
} else {
const auto [RL, RR] = __split(A->right, k);
A->right = RL;
A->compute();
return { A, RR };
}
}
void insert( int pos, int delta ) {
Node* node = new Node();
node->pos = pos;
node->delta = delta;
node->rng = rand();
node->sdelt = delta;
const auto ldata = __split(root, pos - 1);
Node* left = ldata.first;
const auto rdata = __split(ldata.second, pos);
Node* right = rdata.second;
Node* mid = rdata.first;
if (mid == nullptr) mid = node;
else {
delete node;
mid->delta += delta;
mid->sdelt += delta;
}
root = __merge(left, __merge(mid, right));
}
int query (int x) {
Node* local = root;
int res = 0;
while (local != nullptr) {
if (local->pos > x) local = local->left;
else {
res += local->delta;
if (local->left != nullptr) res += local->left->sdelt;
local = local->right;
}
}
return res;
}
void __chmin (Node* node, int until) {
if (node == nullptr) return ;
if (node->left != nullptr) {
if (node->left->sdelt > until) {
__chmin(node->left, until);
node->right = nullptr;
node->delta = 0;
node->compute();
return ;
} else until -= node->left->sdelt;
}
if (until < node->delta) {
node->delta = until;
node->right = nullptr;
node->compute();
return ;
}
until -= node->delta;
__chmin(node->right, until);
node->compute();
}
void chmin (int until) {
__chmin(root, until);
}
Treap cut (int position) {
const auto data = __split(root, position);
root = data.first;
Treap other; other.root = data.second;
return other;
}
static void __traverse (Node* node, vector<Node*> &answer) {
if (node == nullptr) return ;
__traverse(node->left, answer);
answer.push_back(node);
__traverse(node->right, answer);
}
vector<Node*> traverse () {
vector<Node*> ans;
__traverse(root, ans);
return ans;
}
int size () { return root != nullptr ? root->ssize : 0; }
void swap (Treap &other) {
Node* c = root;
root = other.root;
other.root = c;
}
};
void compile (Treap &treap, int H_i, int C_i) {
int chmin_left = treap.query( H_i );
Treap right = treap.cut(H_i);
right.insert( H_i + 1, C_i );
treap.insert( - 1e9, C_i );
treap.chmin( chmin_left );
treap.root = treap.__merge(treap.root, right.root);
}
void merge (Treap &target, Treap &other) {
if (target.size() < other.size())
target.swap(other);
for (Node* node : other.traverse())
target.insert(node->pos, node->delta);
}
vector<Treap> treapArray;
idata A, H, C;
signed main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
treapArray.resize(N);
A.resize(N); H.resize(N); C.resize(N);
for (int i = 0; i < N; i ++) {
cin >> A[i] >> H[i] >> C[i];
A[i] --;
if ((i != 0 && A[i] > i - 1) || (i == 0 && A[i] != 0)) return 0;
}
for (int i = N - 1; i >= 0; i --) {
compile(treapArray[i], H[i], C[i]);
if (i == 0) continue ;
merge(treapArray[A[i]], treapArray[i]);
}
cout << treapArray[0].query(1) << endl;
}
Compilation message
worst_reporter2.cpp: In static member function 'static std::pair<Node*, Node*> Treap::__split(Node*, long long int)':
worst_reporter2.cpp:68:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | const auto [LL, LR] = __split(A->left, k);
| ^
worst_reporter2.cpp:74:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
74 | const auto [RL, RR] = __split(A->right, k);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
1864 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1096 KB |
Output is correct |
8 |
Correct |
7 ms |
2028 KB |
Output is correct |
9 |
Correct |
5 ms |
1364 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
852 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
2260 KB |
Output is correct |
17 |
Correct |
6 ms |
1484 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
6 ms |
1180 KB |
Output is correct |
20 |
Correct |
3 ms |
852 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
5 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1108 KB |
Output is correct |
24 |
Correct |
5 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
852 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
4 ms |
1100 KB |
Output is correct |
29 |
Correct |
4 ms |
972 KB |
Output is correct |
30 |
Correct |
5 ms |
980 KB |
Output is correct |
31 |
Correct |
5 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
1864 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1096 KB |
Output is correct |
8 |
Correct |
7 ms |
2028 KB |
Output is correct |
9 |
Correct |
5 ms |
1364 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
852 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
2260 KB |
Output is correct |
17 |
Correct |
6 ms |
1484 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
6 ms |
1180 KB |
Output is correct |
20 |
Correct |
3 ms |
852 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
5 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1108 KB |
Output is correct |
24 |
Correct |
5 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
852 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
4 ms |
1100 KB |
Output is correct |
29 |
Correct |
4 ms |
972 KB |
Output is correct |
30 |
Correct |
5 ms |
980 KB |
Output is correct |
31 |
Correct |
5 ms |
980 KB |
Output is correct |
32 |
Correct |
7 ms |
1992 KB |
Output is correct |
33 |
Correct |
421 ms |
91764 KB |
Output is correct |
34 |
Correct |
278 ms |
57016 KB |
Output is correct |
35 |
Correct |
411 ms |
89692 KB |
Output is correct |
36 |
Correct |
276 ms |
57164 KB |
Output is correct |
37 |
Correct |
122 ms |
28944 KB |
Output is correct |
38 |
Correct |
92 ms |
22484 KB |
Output is correct |
39 |
Correct |
181 ms |
22536 KB |
Output is correct |
40 |
Correct |
163 ms |
17196 KB |
Output is correct |
41 |
Correct |
72 ms |
11688 KB |
Output is correct |
42 |
Correct |
162 ms |
24268 KB |
Output is correct |
43 |
Correct |
147 ms |
18344 KB |
Output is correct |
44 |
Correct |
438 ms |
110652 KB |
Output is correct |
45 |
Correct |
273 ms |
68644 KB |
Output is correct |
46 |
Correct |
81 ms |
24156 KB |
Output is correct |
47 |
Correct |
273 ms |
36588 KB |
Output is correct |
48 |
Correct |
161 ms |
24244 KB |
Output is correct |
49 |
Correct |
84 ms |
24140 KB |
Output is correct |
50 |
Correct |
309 ms |
48536 KB |
Output is correct |
51 |
Correct |
141 ms |
35872 KB |
Output is correct |
52 |
Correct |
319 ms |
36656 KB |
Output is correct |
53 |
Correct |
148 ms |
24308 KB |
Output is correct |
54 |
Correct |
103 ms |
24364 KB |
Output is correct |
55 |
Correct |
200 ms |
38588 KB |
Output is correct |
56 |
Correct |
132 ms |
29784 KB |
Output is correct |
57 |
Correct |
131 ms |
27056 KB |
Output is correct |
58 |
Correct |
262 ms |
30496 KB |
Output is correct |
59 |
Correct |
245 ms |
30556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
1864 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1096 KB |
Output is correct |
8 |
Correct |
7 ms |
2028 KB |
Output is correct |
9 |
Correct |
5 ms |
1364 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
852 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
2260 KB |
Output is correct |
17 |
Correct |
6 ms |
1484 KB |
Output is correct |
18 |
Correct |
3 ms |
972 KB |
Output is correct |
19 |
Correct |
6 ms |
1180 KB |
Output is correct |
20 |
Correct |
3 ms |
852 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
5 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1108 KB |
Output is correct |
24 |
Correct |
5 ms |
1108 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
852 KB |
Output is correct |
27 |
Correct |
4 ms |
1364 KB |
Output is correct |
28 |
Correct |
4 ms |
1100 KB |
Output is correct |
29 |
Correct |
4 ms |
972 KB |
Output is correct |
30 |
Correct |
5 ms |
980 KB |
Output is correct |
31 |
Correct |
5 ms |
980 KB |
Output is correct |
32 |
Correct |
7 ms |
1992 KB |
Output is correct |
33 |
Correct |
421 ms |
91764 KB |
Output is correct |
34 |
Correct |
278 ms |
57016 KB |
Output is correct |
35 |
Correct |
411 ms |
89692 KB |
Output is correct |
36 |
Correct |
276 ms |
57164 KB |
Output is correct |
37 |
Correct |
122 ms |
28944 KB |
Output is correct |
38 |
Correct |
92 ms |
22484 KB |
Output is correct |
39 |
Correct |
181 ms |
22536 KB |
Output is correct |
40 |
Correct |
163 ms |
17196 KB |
Output is correct |
41 |
Correct |
72 ms |
11688 KB |
Output is correct |
42 |
Correct |
162 ms |
24268 KB |
Output is correct |
43 |
Correct |
147 ms |
18344 KB |
Output is correct |
44 |
Correct |
438 ms |
110652 KB |
Output is correct |
45 |
Correct |
273 ms |
68644 KB |
Output is correct |
46 |
Correct |
81 ms |
24156 KB |
Output is correct |
47 |
Correct |
273 ms |
36588 KB |
Output is correct |
48 |
Correct |
161 ms |
24244 KB |
Output is correct |
49 |
Correct |
84 ms |
24140 KB |
Output is correct |
50 |
Correct |
309 ms |
48536 KB |
Output is correct |
51 |
Correct |
141 ms |
35872 KB |
Output is correct |
52 |
Correct |
319 ms |
36656 KB |
Output is correct |
53 |
Correct |
148 ms |
24308 KB |
Output is correct |
54 |
Correct |
103 ms |
24364 KB |
Output is correct |
55 |
Correct |
200 ms |
38588 KB |
Output is correct |
56 |
Correct |
132 ms |
29784 KB |
Output is correct |
57 |
Correct |
131 ms |
27056 KB |
Output is correct |
58 |
Correct |
262 ms |
30496 KB |
Output is correct |
59 |
Correct |
245 ms |
30556 KB |
Output is correct |
60 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |