-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcepc206.c
195 lines (174 loc) · 4.64 KB
/
dcepc206.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#define MAX_STEPS 100000
struct node {
struct node *left;
struct node *right;
struct node *parent;
int64_t value;
int64_t replicas;
int64_t sum;
int height;
};
struct tree {
struct node *root;
struct node nodes[MAX_STEPS];
int64_t pointer;
};
int clean_tree(struct tree *t) {
t->pointer = 0;
}
struct node* alloc_node(struct tree *t) {
return &(t->nodes[t->pointer++]);
}
struct node* mknode(struct tree *t, struct node *current, int64_t v) {
struct node *n = alloc_node(t); \
n->left = NULL;
n->right = NULL;
n->parent = current;
n->value = v;
n->sum = 0;
n->replicas = 1;
n->height = 1;
return n;
}
void rotate_left(struct tree* t, struct node* n) {
struct node **root;
if (n->parent) {
if (n->parent->right == n) {
root = &n->parent->right;
} else {
root = &n->parent->left;
}
} else {
root = &t->root;
}
assert(n->right);
*root = n->right;
(*root)->parent = n->parent;
n->right = (*root)->left;
if (n->right) {
n->right->parent = n;
}
(*root)->left = n;
n->parent = *root;
(*root)->sum += (n->value*n->replicas) + n->sum;
n->height = max_height(n->left, n->right) + 1;
(*root)->height = max_height((*root)->left, (*root)->right) + 1;
}
void rotate_right(struct tree* t, struct node* n) {
struct node **root;
if (n->parent) {
if (n->parent->right == n) {
root = &n->parent->right;
} else {
root = &n->parent->left;
}
} else {
root = &t->root;
}
assert(n->left);
n->left->parent = n->parent;
*root = n->left;
n->left = n->left->right;
if (n->left) {
n->left->parent = n;
}
assert(n->sum >= ((*root)->value * (*root)->replicas) - (*root)->sum);
n->sum = n->sum - ((*root)->value * (*root)->replicas) - (*root)->sum;
(*root)->right = n;
n->parent = *root;
n->height = max_height(n->left, n->right) + 1;
(*root)->height = max_height((*root)->left, (*root)->right) + 1;
}
int get_height(struct node *n) {
if (!n) {
return 0;
} else {
return n->height;
}
}
int max_height(struct node *a, struct node *b) {
int ha = get_height(a);
int hb = get_height(b);
return ha > hb ? ha : hb;
}
void balance(struct tree* t, struct node* n, int64_t value) {
int diff;
while (n) {
diff = get_height(n->left) - get_height(n->right);
if (diff < -1 && value > n->right->value) {
rotate_left(t, n);
} else if (diff > 1 && value < n->left->value) {
rotate_right(t, n);
} else if (diff < -1 && value < n->right->value) {
rotate_right(t, n->right);
rotate_left(t, n);
} else if (diff > 1 && value > n->left->value) {
rotate_left(t, n->left);
rotate_right(t, n);
}
if (!n->left && !n->right) {
n->height = 1;
} else {
n->height = max_height(n->left, n->right) + 1;
}
n = n->parent;
}
}
int64_t insert(struct tree* t, int64_t value) {
int p = 0;
struct node *current = t->root;
int64_t s = 0;
while (1) {
if (current->value == value) {
current->replicas++;
return current->sum + s;
} else if (current->value < value) {
s += current->value*current->replicas + current->sum;
if (!current->right) {
current->right = mknode(t, current, value);
balance(t, current, value);
return s;
} else {
current = current->right;
}
} else {
current->sum += value;
if (!current->left) {
current->left = mknode(t, current, value);
balance(t, current, value);
return s;
} else {
current = current->left;
}
}
}
}
int64_t solve(struct tree *t, int64_t stairs[], int size) {
int i;
int64_t total = 0;
clean_tree(t);
t->root = mknode(t, NULL, stairs[0]);
for (i = 1; i < size; i++) {
total += insert(t, stairs[i]);
}
return total;
}
int main(void) {
int i, t;
int tests;
int steps;
int64_t stairs[MAX_STEPS];
struct tree tree;
scanf("%d", &tests);
for (t = 0; t < tests; t++) {
scanf("%d", &steps);
for (i = 0; i < steps; i++) {
scanf("%" PRIu64, &stairs[i]);
}
printf("%" PRIu64 "\n", solve(&tree, stairs, steps));
}
}