#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
class node {
public:
node* l;
node* r;
node* p;
bool is;
int sz;
node(bool is): is(is) {
l = r = p = NULL;
sz = is;
}
void pull() {
sz = is;
if (l != NULL) {
l->p = this;
sz += l->sz;
}
if (r != NULL) {
r->p = this;
sz += r->sz;
}
}
};
bool is_bst_root(node* v) {
if (v == NULL) {
return false;
}
return (v->p == NULL || (v->p->l != v && v->p->r != v));
}
void rotate(node* v) {
node* u = v->p;
v->p = u->p;
if (v->p != NULL) {
if (v->p->l == u) {
v->p->l = v;
}
if (v->p->r == u) {
v->p->r = v;
}
}
if (v == u->l) {
u->l = v->r;
v->r = u;
} else {
u->r = v->l;
v->l = u;
}
u->pull();
v->pull();
}
void splay(node* v) {
if (v == NULL) {
return;
}
while (!is_bst_root(v)) {
node* u = v->p;
if (!is_bst_root(u)) {
if ((u->l == v) ^ (u->p->l == u)) {
rotate(v);
} else {
rotate(u);
}
}
rotate(v);
}
}
void access(node* v) {
node* u = NULL;
while (v != NULL) {
splay(v);
v->r = u;
v->pull();
u = v;
v = v->p;
}
}
void link(node* v, node* u) {
splay(v);
v->p = u;
}
void cut(node* v, node* u) {
access(v);
splay(u);
u->r = v->p = NULL;
u->pull();
}
set<pair<int, int>> all, white;
vector<node*> tree;
vector<int> x, to;
int n, m;
int find_next(pair<int, int> p) {
return all.upper_bound(p)->second;
}
void init(int N, int L, int X[]) {
n = N;
m = L;
for (int i = 0; i < n; ++i) {
x.push_back(X[i]);
}
for (int i = 0; i < n * 2 + 2; ++i) {
tree.push_back(new node(i < n));
to.push_back(-1);
}
all.emplace(-1, n * 2);
white.emplace(-1, n * 2);
for (int i = 0; i < n; ++i) {
all.emplace(x[i], i);
all.emplace(x[i] + m, i + n);
white.emplace(x[i] + m, i + n);
to[i] = i + n;
link(tree[i], tree[i + n]);
}
all.emplace(INT_MAX, n * 2 + 1);
for (auto p : white) {
to[p.second] = find_next(p);
link(tree[p.second], tree[to[p.second]]);
}
}
int update(int i, int y) {
all.erase(make_pair(x[i], i));
all.erase(make_pair(x[i] + m, i + n));
white.erase(make_pair(x[i] + m, i + n));
cut(tree[i + n], tree[to[i + n]]);
to[i + n] = -1;
{
auto it = --white.lower_bound(make_pair(x[i], i));
cut(tree[it->second], tree[to[it->second]]);
to[it->second] = find_next(*it);
link(tree[it->second], tree[to[it->second]]);
}
{
auto it = --white.lower_bound(make_pair(x[i] + m, i + n));
cut(tree[it->second], tree[to[it->second]]);
to[it->second] = find_next(*it);
link(tree[it->second], tree[to[it->second]]);
}
x[i] = y;
all.insert(make_pair(x[i], i));
all.insert(make_pair(x[i] + m, i + n));
white.insert(make_pair(x[i] + m, i + n));
to[i + n] = find_next(make_pair(x[i] + m, i + n));
link(tree[i + n], tree[to[i + n]]);
{
auto it = --white.lower_bound(make_pair(x[i], i));
cut(tree[it->second], tree[to[it->second]]);
to[it->second] = find_next(*it);
link(tree[it->second], tree[to[it->second]]);
}
{
auto it = --white.lower_bound(make_pair(x[i] + m, i + n));
cut(tree[it->second], tree[to[it->second]]);
to[it->second] = find_next(*it);
link(tree[it->second], tree[to[it->second]]);
}
access(tree[n * 2]);
splay(tree[n * 2]);
return tree[n * 2]->sz;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
108 ms |
5364 KB |
Output is correct |
8 |
Correct |
119 ms |
6668 KB |
Output is correct |
9 |
Correct |
309 ms |
15872 KB |
Output is correct |
10 |
Correct |
188 ms |
15596 KB |
Output is correct |
11 |
Correct |
218 ms |
15600 KB |
Output is correct |
12 |
Correct |
635 ms |
15852 KB |
Output is correct |
13 |
Correct |
241 ms |
15444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
108 ms |
5364 KB |
Output is correct |
8 |
Correct |
119 ms |
6668 KB |
Output is correct |
9 |
Correct |
309 ms |
15872 KB |
Output is correct |
10 |
Correct |
188 ms |
15596 KB |
Output is correct |
11 |
Correct |
218 ms |
15600 KB |
Output is correct |
12 |
Correct |
635 ms |
15852 KB |
Output is correct |
13 |
Correct |
241 ms |
15444 KB |
Output is correct |
14 |
Correct |
300 ms |
7408 KB |
Output is correct |
15 |
Correct |
219 ms |
8868 KB |
Output is correct |
16 |
Correct |
923 ms |
16408 KB |
Output is correct |
17 |
Correct |
967 ms |
22120 KB |
Output is correct |
18 |
Correct |
1032 ms |
21864 KB |
Output is correct |
19 |
Correct |
274 ms |
21992 KB |
Output is correct |
20 |
Correct |
914 ms |
21904 KB |
Output is correct |
21 |
Correct |
1063 ms |
21876 KB |
Output is correct |
22 |
Correct |
363 ms |
21556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
108 ms |
5364 KB |
Output is correct |
8 |
Correct |
119 ms |
6668 KB |
Output is correct |
9 |
Correct |
309 ms |
15872 KB |
Output is correct |
10 |
Correct |
188 ms |
15596 KB |
Output is correct |
11 |
Correct |
218 ms |
15600 KB |
Output is correct |
12 |
Correct |
635 ms |
15852 KB |
Output is correct |
13 |
Correct |
241 ms |
15444 KB |
Output is correct |
14 |
Correct |
300 ms |
7408 KB |
Output is correct |
15 |
Correct |
219 ms |
8868 KB |
Output is correct |
16 |
Correct |
923 ms |
16408 KB |
Output is correct |
17 |
Correct |
967 ms |
22120 KB |
Output is correct |
18 |
Correct |
1032 ms |
21864 KB |
Output is correct |
19 |
Correct |
274 ms |
21992 KB |
Output is correct |
20 |
Correct |
914 ms |
21904 KB |
Output is correct |
21 |
Correct |
1063 ms |
21876 KB |
Output is correct |
22 |
Correct |
363 ms |
21556 KB |
Output is correct |
23 |
Correct |
1592 ms |
47160 KB |
Output is correct |
24 |
Correct |
1543 ms |
47132 KB |
Output is correct |
25 |
Correct |
1202 ms |
47124 KB |
Output is correct |
26 |
Correct |
632 ms |
47032 KB |
Output is correct |
27 |
Correct |
595 ms |
47012 KB |
Output is correct |
28 |
Correct |
954 ms |
6272 KB |
Output is correct |
29 |
Correct |
911 ms |
6268 KB |
Output is correct |
30 |
Correct |
865 ms |
6280 KB |
Output is correct |
31 |
Correct |
910 ms |
6272 KB |
Output is correct |
32 |
Correct |
725 ms |
46652 KB |
Output is correct |
33 |
Correct |
700 ms |
45848 KB |
Output is correct |
34 |
Correct |
747 ms |
46792 KB |
Output is correct |
35 |
Correct |
693 ms |
47204 KB |
Output is correct |
36 |
Correct |
1574 ms |
46588 KB |
Output is correct |
37 |
Correct |
1964 ms |
46992 KB |
Output is correct |
38 |
Correct |
747 ms |
45744 KB |
Output is correct |
39 |
Correct |
646 ms |
47056 KB |
Output is correct |
40 |
Correct |
759 ms |
45788 KB |
Output is correct |
41 |
Correct |
2321 ms |
46588 KB |
Output is correct |
42 |
Correct |
2681 ms |
46812 KB |
Output is correct |