#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 151000, C = 512;
struct node {
node *l, *r, *p, *pp;
int val, sum;
node(int x = 0) : l(0), r(0), p(0), pp(0), val(x), sum(x) {}
};
using pnode = node*;
void upd(pnode v) {
if(!v) return;
v->sum = v->val;
for(auto i : {v->l, v->r}) if(i) {
i->p = v;
v->sum += i->sum;
}
}
void rotate(pnode v) {
auto p = v->p, g = p->p;
if(g) (g->l == p ? g->l : g->r) = v;
else v->p = 0;
if(p->l == v)
p->l = v->r, v->r = p;
else
p->r = v->l, v->l = p;
upd(p), upd(v), upd(g);
}
pnode splay(pnode v) {
if(!v) return 0;
while(v->p) {
auto p = v->p, g = p->p;
if(g)
rotate((g->l == p) == (p->l == v) ? p : v);
rotate(v);
}
return v;
}
pnode left(pnode v) {
splay(v);
while(v->l) v = v->l;
return splay(v);
}
void cut_r(pnode v) {
splay(v);
if(!v->r) return;
auto t = v->r;
t->p = v->r = 0;
upd(v);
t = left(t);
t->pp = v;
}
pnode merge(pnode a, pnode b) {
if(!a||!b) return a?:b;
b = left(b);
b->l = a;
b->pp = 0;
upd(b);
return a;
}
void expose(pnode v) {
cut_r(v);
v = left(v);
while(v->pp) {
cut_r(v->pp);
v = left(merge(v->pp, v));
}
}
pnode root(pnode a) {
expose(a);
return left(a);
}
void link(pnode a, pnode b) {
expose(b);
a->pp = b;
}
void cut(pnode a, pnode b) {
expose(b);
a->pp = 0;
}
pnode lca(pnode a, pnode b) {
expose(a);
if(left(a)->val == left(b)->val) return b;
expose(b);
if(left(a)->val == left(b)->val) return a;
return left(a)->pp;
}
deque<node> buffer;
pnode newnode(int x = 0) {
buffer.emplace_back(x);
return &buffer.back();
}
int n, l;
pnode nd[maxn];
int pos[maxn], curp[maxn];
set<array<int, 2>> f;
int next(int v) {
auto it = f.upper_bound({pos[v], v});
return it==f.end()?-1:it->at(1);
}
int jump(int v) {
auto it = f.upper_bound({pos[v]+l, 1<<30});
return it==f.end()?-1:it->at(1);
}
void kill(int v) {
if(curp[v] != -1) {
if(curp[v] != -1)
cut(nd[v], nd[curp[v]]);
nd[v]->val = 0;
upd(nd[v]);
curp[v] = -1;
}
}
void make_link(int v) {
kill(v);
int x = next(v);
int y = jump(v);
if(x == -1)
return;
if(jump(x) == y) {
//cout << v << " points(0) to " << x << " | " << pos[v] << " " << pos[x] << endl;
link(nd[v], nd[x]);
curp[v] = x;
} else if(y != -1) {
//cout << v << " points(1) to " << y << " | " << pos[v] << " " << pos[y] << endl;
link(nd[v], nd[y]);
curp[v] = y;
nd[v]->val = 1;
upd(nd[v]);
}
}
void init(int N, int L, int X[]) {
n = N, l = L;
for(int i = 0; i < n; i++) {
f.insert({X[i], i});
pos[i] = X[i];
curp[i] = -1;
nd[i] = newnode(0);
}
for(int i = 0; i < n; i++) make_link(i);
}
void event(int x, int y) {
auto P = f.lower_bound({x, y});
if(P != f.begin()) {
P--;
make_link(P->at(1));
}
P = f.lower_bound({x-l, -1});
if(P != f.begin()) {
P--;
make_link(P->at(1));
}
}
int update(int i, int y) {
//cout << "Query\n";
auto V = f.lower_bound({pos[i], i});
kill(i);
f.erase({pos[i], i});
event(pos[i], i);
f.insert({pos[i] = y, i});
event(y, i);
make_link(i);
//for(int i = 0; i < n; i++) cout << pos[i] << " " << jump(i) << '\n';
return 1+root(nd[f.begin()->at(1)])->sum;
}
Compilation message
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:161:11: warning: variable 'V' set but not used [-Wunused-but-set-variable]
161 | auto V = f.lower_bound({pos[i], i});
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
156 ms |
2552 KB |
Output is correct |
8 |
Correct |
163 ms |
2816 KB |
Output is correct |
9 |
Correct |
308 ms |
6392 KB |
Output is correct |
10 |
Correct |
168 ms |
6396 KB |
Output is correct |
11 |
Correct |
173 ms |
6392 KB |
Output is correct |
12 |
Correct |
472 ms |
6520 KB |
Output is correct |
13 |
Correct |
180 ms |
6392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
156 ms |
2552 KB |
Output is correct |
8 |
Correct |
163 ms |
2816 KB |
Output is correct |
9 |
Correct |
308 ms |
6392 KB |
Output is correct |
10 |
Correct |
168 ms |
6396 KB |
Output is correct |
11 |
Correct |
173 ms |
6392 KB |
Output is correct |
12 |
Correct |
472 ms |
6520 KB |
Output is correct |
13 |
Correct |
180 ms |
6392 KB |
Output is correct |
14 |
Correct |
310 ms |
3072 KB |
Output is correct |
15 |
Correct |
298 ms |
3832 KB |
Output is correct |
16 |
Correct |
676 ms |
6648 KB |
Output is correct |
17 |
Correct |
737 ms |
8952 KB |
Output is correct |
18 |
Correct |
771 ms |
8824 KB |
Output is correct |
19 |
Correct |
226 ms |
8888 KB |
Output is correct |
20 |
Correct |
768 ms |
8828 KB |
Output is correct |
21 |
Correct |
768 ms |
8824 KB |
Output is correct |
22 |
Correct |
255 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
156 ms |
2552 KB |
Output is correct |
8 |
Correct |
163 ms |
2816 KB |
Output is correct |
9 |
Correct |
308 ms |
6392 KB |
Output is correct |
10 |
Correct |
168 ms |
6396 KB |
Output is correct |
11 |
Correct |
173 ms |
6392 KB |
Output is correct |
12 |
Correct |
472 ms |
6520 KB |
Output is correct |
13 |
Correct |
180 ms |
6392 KB |
Output is correct |
14 |
Correct |
310 ms |
3072 KB |
Output is correct |
15 |
Correct |
298 ms |
3832 KB |
Output is correct |
16 |
Correct |
676 ms |
6648 KB |
Output is correct |
17 |
Correct |
737 ms |
8952 KB |
Output is correct |
18 |
Correct |
771 ms |
8824 KB |
Output is correct |
19 |
Correct |
226 ms |
8888 KB |
Output is correct |
20 |
Correct |
768 ms |
8828 KB |
Output is correct |
21 |
Correct |
768 ms |
8824 KB |
Output is correct |
22 |
Correct |
255 ms |
8900 KB |
Output is correct |
23 |
Correct |
1278 ms |
18340 KB |
Output is correct |
24 |
Correct |
1275 ms |
18340 KB |
Output is correct |
25 |
Correct |
1094 ms |
18404 KB |
Output is correct |
26 |
Correct |
575 ms |
18336 KB |
Output is correct |
27 |
Correct |
501 ms |
18552 KB |
Output is correct |
28 |
Correct |
1058 ms |
2680 KB |
Output is correct |
29 |
Correct |
1070 ms |
2636 KB |
Output is correct |
30 |
Correct |
1070 ms |
2684 KB |
Output is correct |
31 |
Correct |
1075 ms |
2840 KB |
Output is correct |
32 |
Correct |
557 ms |
18552 KB |
Output is correct |
33 |
Correct |
506 ms |
18296 KB |
Output is correct |
34 |
Correct |
546 ms |
18424 KB |
Output is correct |
35 |
Correct |
514 ms |
18424 KB |
Output is correct |
36 |
Correct |
1012 ms |
18424 KB |
Output is correct |
37 |
Correct |
1595 ms |
18424 KB |
Output is correct |
38 |
Correct |
585 ms |
18424 KB |
Output is correct |
39 |
Correct |
496 ms |
18552 KB |
Output is correct |
40 |
Correct |
588 ms |
18296 KB |
Output is correct |
41 |
Correct |
1950 ms |
18344 KB |
Output is correct |
42 |
Correct |
1971 ms |
18344 KB |
Output is correct |