#include <iostream>
#define MAXN 150005
#define K 400
#include "elephants.h"
using namespace std;
int N, L, Q, B, Pos[MAXN], Bid[MAXN], Sort[MAXN]; //0 indexed
class Block {
public:
int Sz, Id[2 * K + 5]; //ID sorted by Pos
int Cost[2 * K + 5], Last[2 * K + 5];
void Insert(int id) {
Id[Sz] = id, Sz++;
int pt = Sz-1;
while (pt > 0) {
if (Pos[Id[pt]] < Pos[Id[pt-1]]) {swap(Id[pt], Id[pt-1]);}
else {break;}
pt--;
}
}
void Del(int id) {
for (int i=0; i<Sz-1; i++) {
if (Id[i] == id) {swap(Id[i], Id[i+1]);}
}
Sz-=1;
}
void slv() {
int pt = Sz-1;
for (int i=Sz-1; i>=0; i--) {
while (pt > 0 && Pos[Id[pt-1]] > Pos[Id[i]] + L) {pt--;}
if (Pos[Id[Sz-1]] <= Pos[Id[i]] + L) {
Cost[i] = 1, Last[i]=Id[i];
} else {
Cost[i] = Cost[pt] + 1, Last[i]=Last[pt];
}
}
}
int ub(int x) {
if (Sz == 0) {return(-1);}
int lo = -1, hi = Sz;
while (lo + 1 != hi) {
int mid = (lo + hi) >> 1;
if (Pos[Id[mid]] > x) {hi = mid;}
else {lo = mid;}
}
if (hi == Sz) {return(-1);}
return(hi);
}
} Blk[MAXN/K + 5];
void build() {
if (Q%K != 0) {return;}
int ind = 0, bcur = -1;
if (Q == 0) {
for (int i=0; i<N; i++) {
Sort[i] = i;
} //already sorted
} else {
for (int i=0; i<B; i++) {
for (int j=0; j < Blk[i].Sz; j++) {
Sort[ind] = Blk[i].Id[j];
ind++;
}
Blk[i].Sz = 0;
}
}
for (int i=0; i<N; i++) {
if (i % K == 0) {
if (bcur >= 0) {Blk[bcur].slv();}
bcur++;
}
Blk[bcur].Insert(Sort[i]);
Bid[Sort[i]] = bcur;
}
Blk[bcur].slv();
B= bcur+1;
}
void init(int n, int l, int X[])
{
N = n, L = l;
for (int i=0; i<N; i++) {
Pos[i] = X[i];
}
build();
}
int update(int id, int y)
{
Q+=1;
Blk[Bid[id]].Del(id);
Blk[Bid[id]].slv();
Pos[id] = y;
for (int i=0; i<B; i++) {
if ((Blk[i+1].Sz > 0 && Pos[Blk[i+1].Id[0]]>=y) || i == B-1) {
Blk[i].Insert(id);
Bid[id] = i;
Blk[i].slv();
break;
}
}
build();
int cost = 0, prev = -1<<30;
for (int i=0; i<B; i++) {
int New = Blk[i].ub(prev + L);
if (New == -1) {continue;}
else {
prev = Pos[Blk[i].Last[New]];
cost += Blk[i].Cost[New];
}
}
return(cost);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
492 ms |
2424 KB |
Output is correct |
8 |
Correct |
521 ms |
2732 KB |
Output is correct |
9 |
Correct |
459 ms |
4576 KB |
Output is correct |
10 |
Correct |
452 ms |
4344 KB |
Output is correct |
11 |
Correct |
424 ms |
4216 KB |
Output is correct |
12 |
Correct |
744 ms |
4472 KB |
Output is correct |
13 |
Correct |
463 ms |
4220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
492 ms |
2424 KB |
Output is correct |
8 |
Correct |
521 ms |
2732 KB |
Output is correct |
9 |
Correct |
459 ms |
4576 KB |
Output is correct |
10 |
Correct |
452 ms |
4344 KB |
Output is correct |
11 |
Correct |
424 ms |
4216 KB |
Output is correct |
12 |
Correct |
744 ms |
4472 KB |
Output is correct |
13 |
Correct |
463 ms |
4220 KB |
Output is correct |
14 |
Correct |
408 ms |
3448 KB |
Output is correct |
15 |
Correct |
805 ms |
3576 KB |
Output is correct |
16 |
Correct |
1297 ms |
5008 KB |
Output is correct |
17 |
Correct |
1327 ms |
6008 KB |
Output is correct |
18 |
Correct |
1435 ms |
6008 KB |
Output is correct |
19 |
Correct |
793 ms |
5948 KB |
Output is correct |
20 |
Correct |
1294 ms |
6008 KB |
Output is correct |
21 |
Correct |
1333 ms |
5880 KB |
Output is correct |
22 |
Correct |
826 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
492 ms |
2424 KB |
Output is correct |
8 |
Correct |
521 ms |
2732 KB |
Output is correct |
9 |
Correct |
459 ms |
4576 KB |
Output is correct |
10 |
Correct |
452 ms |
4344 KB |
Output is correct |
11 |
Correct |
424 ms |
4216 KB |
Output is correct |
12 |
Correct |
744 ms |
4472 KB |
Output is correct |
13 |
Correct |
463 ms |
4220 KB |
Output is correct |
14 |
Correct |
408 ms |
3448 KB |
Output is correct |
15 |
Correct |
805 ms |
3576 KB |
Output is correct |
16 |
Correct |
1297 ms |
5008 KB |
Output is correct |
17 |
Correct |
1327 ms |
6008 KB |
Output is correct |
18 |
Correct |
1435 ms |
6008 KB |
Output is correct |
19 |
Correct |
793 ms |
5948 KB |
Output is correct |
20 |
Correct |
1294 ms |
6008 KB |
Output is correct |
21 |
Correct |
1333 ms |
5880 KB |
Output is correct |
22 |
Correct |
826 ms |
5496 KB |
Output is correct |
23 |
Correct |
3509 ms |
9976 KB |
Output is correct |
24 |
Correct |
3857 ms |
13152 KB |
Output is correct |
25 |
Correct |
2908 ms |
13068 KB |
Output is correct |
26 |
Correct |
3718 ms |
13176 KB |
Output is correct |
27 |
Correct |
3998 ms |
12912 KB |
Output is correct |
28 |
Correct |
1748 ms |
5240 KB |
Output is correct |
29 |
Correct |
1651 ms |
5368 KB |
Output is correct |
30 |
Correct |
1721 ms |
5240 KB |
Output is correct |
31 |
Correct |
1647 ms |
5240 KB |
Output is correct |
32 |
Correct |
3219 ms |
12536 KB |
Output is correct |
33 |
Correct |
2754 ms |
11896 KB |
Output is correct |
34 |
Correct |
3123 ms |
12792 KB |
Output is correct |
35 |
Correct |
2804 ms |
13004 KB |
Output is correct |
36 |
Correct |
2750 ms |
12408 KB |
Output is correct |
37 |
Correct |
3868 ms |
12792 KB |
Output is correct |
38 |
Correct |
3096 ms |
11640 KB |
Output is correct |
39 |
Correct |
3052 ms |
12744 KB |
Output is correct |
40 |
Correct |
3859 ms |
11744 KB |
Output is correct |
41 |
Correct |
4823 ms |
12536 KB |
Output is correct |
42 |
Correct |
4908 ms |
12664 KB |
Output is correct |