#include "elephants.h"
#include <algorithm>
#include <vector>
using namespace std;
const int mag = 380;
struct Block
{
Block()
: a()
, cnt()
, upto()
{}
vector<int> a;
vector<int> cnt;
vector<int> upto;
};
int n;
int len;
int x[150000];
int blof[150000];
vector<Block> blocks;
void SetBlock(Block& block)
{
if (block.a.empty())
{
block.upto.clear();
block.cnt.clear();
return;
}
vector<int>& a = block.a;
vector<int>& cnt = block.cnt;
cnt.resize(a.size());
vector<int>& upto = block.upto;
upto.resize(a.size());
cnt.back() = 1;
int uptoind = (int)a.size() - 1;
upto.back() = a.back() + len;
for (int j = (int)a.size() - 2; j >= 0; --j)
{
while (a[uptoind] - a[j] > len) --uptoind;
cnt[j] = 1 + (uptoind + 1 == (int)a.size() ? 0 : cnt[uptoind + 1]);
upto[j] = (uptoind + 1 == (int)a.size() ? a[j] + len : upto[uptoind + 1]);
}
}
void Init()
{
blocks.clear();
vector<pair<int, int> > y(n);
for (int i = 0; i < n; ++i)
y[i] = {x[i], i};
sort(y.begin(), y.end());
for (int i = 0; i < n; ++i)
blof[y[i].second] = i / mag;
for (int i = 0; i < n; i += mag)
{
blocks.push_back(Block());
blocks.back().a.clear();
for (int j = i; j < min(i + mag, n); ++j)
blocks.back().a.push_back(y[j].first);
SetBlock(blocks.back());
}
}
int update(int id, int nv)
{
static int upds = 0;
vector<int>* a = &blocks[blof[id]].a;
a->erase(find(a->begin(), a->end(), x[id]));
SetBlock(blocks[blof[id]]);
vector<int> temp;
for (int i = 0; i < (int)blocks.size(); ++i)
if (!blocks[i].a.empty())
temp.push_back(i);
auto it = upper_bound(temp.begin(), temp.end(), nv, [](int y, int bl) {
return y < blocks[bl].a.back();
});
blof[id] = (it == temp.end() ? (int)blocks.size() - 1 : *it);
a = &blocks[blof[id]].a;
a->insert(upper_bound(a->begin(), a->end(), nv), nv);
SetBlock(blocks[blof[id]]);
x[id] = nv;
if (++upds >= mag)
{
upds = 0;
Init();
}
int res = 0;
int upto = -1;
for (int bl = 0; bl < (int)blocks.size(); ++bl)
{
int i = upper_bound(blocks[bl].a.begin(), blocks[bl].a.end(), upto) - blocks[bl].a.begin();
if (i < (int)blocks[bl].a.size())
{
res += blocks[bl].cnt[i];
upto = blocks[bl].upto[i];
}
}
return res;
}
void init(int n_, int len_, int x_[])
{
n = n_;
len = len_;
for (int i = 0; i < n; ++i)
x[i] = x_[i];
Init();
}
/*
5 10 5
1
1
1
1
1
0 10
1 20
2 11
3 31
4 33
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 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 |
263 ms |
2296 KB |
Output is correct |
8 |
Correct |
285 ms |
2656 KB |
Output is correct |
9 |
Correct |
515 ms |
4320 KB |
Output is correct |
10 |
Correct |
640 ms |
4068 KB |
Output is correct |
11 |
Correct |
705 ms |
3952 KB |
Output is correct |
12 |
Correct |
1070 ms |
4192 KB |
Output is correct |
13 |
Correct |
684 ms |
3712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 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 |
263 ms |
2296 KB |
Output is correct |
8 |
Correct |
285 ms |
2656 KB |
Output is correct |
9 |
Correct |
515 ms |
4320 KB |
Output is correct |
10 |
Correct |
640 ms |
4068 KB |
Output is correct |
11 |
Correct |
705 ms |
3952 KB |
Output is correct |
12 |
Correct |
1070 ms |
4192 KB |
Output is correct |
13 |
Correct |
684 ms |
3712 KB |
Output is correct |
14 |
Correct |
524 ms |
3308 KB |
Output is correct |
15 |
Correct |
519 ms |
3584 KB |
Output is correct |
16 |
Correct |
1603 ms |
4704 KB |
Output is correct |
17 |
Correct |
1905 ms |
5728 KB |
Output is correct |
18 |
Correct |
2035 ms |
5624 KB |
Output is correct |
19 |
Correct |
1164 ms |
5716 KB |
Output is correct |
20 |
Correct |
1881 ms |
5588 KB |
Output is correct |
21 |
Correct |
1904 ms |
5496 KB |
Output is correct |
22 |
Correct |
1238 ms |
5188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 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 |
263 ms |
2296 KB |
Output is correct |
8 |
Correct |
285 ms |
2656 KB |
Output is correct |
9 |
Correct |
515 ms |
4320 KB |
Output is correct |
10 |
Correct |
640 ms |
4068 KB |
Output is correct |
11 |
Correct |
705 ms |
3952 KB |
Output is correct |
12 |
Correct |
1070 ms |
4192 KB |
Output is correct |
13 |
Correct |
684 ms |
3712 KB |
Output is correct |
14 |
Correct |
524 ms |
3308 KB |
Output is correct |
15 |
Correct |
519 ms |
3584 KB |
Output is correct |
16 |
Correct |
1603 ms |
4704 KB |
Output is correct |
17 |
Correct |
1905 ms |
5728 KB |
Output is correct |
18 |
Correct |
2035 ms |
5624 KB |
Output is correct |
19 |
Correct |
1164 ms |
5716 KB |
Output is correct |
20 |
Correct |
1881 ms |
5588 KB |
Output is correct |
21 |
Correct |
1904 ms |
5496 KB |
Output is correct |
22 |
Correct |
1238 ms |
5188 KB |
Output is correct |
23 |
Correct |
4256 ms |
12220 KB |
Output is correct |
24 |
Correct |
4571 ms |
12152 KB |
Output is correct |
25 |
Correct |
3417 ms |
12152 KB |
Output is correct |
26 |
Correct |
5208 ms |
12152 KB |
Output is correct |
27 |
Correct |
5652 ms |
12120 KB |
Output is correct |
28 |
Correct |
1071 ms |
5372 KB |
Output is correct |
29 |
Correct |
1035 ms |
5372 KB |
Output is correct |
30 |
Correct |
1075 ms |
5496 KB |
Output is correct |
31 |
Correct |
1045 ms |
5240 KB |
Output is correct |
32 |
Correct |
5962 ms |
11588 KB |
Output is correct |
33 |
Correct |
5741 ms |
10944 KB |
Output is correct |
34 |
Correct |
5179 ms |
11828 KB |
Output is correct |
35 |
Correct |
5737 ms |
12048 KB |
Output is correct |
36 |
Correct |
3900 ms |
11604 KB |
Output is correct |
37 |
Correct |
7605 ms |
11984 KB |
Output is correct |
38 |
Correct |
5406 ms |
10796 KB |
Output is correct |
39 |
Correct |
4957 ms |
11824 KB |
Output is correct |
40 |
Correct |
5784 ms |
10824 KB |
Output is correct |
41 |
Correct |
7783 ms |
11476 KB |
Output is correct |
42 |
Correct |
7935 ms |
11860 KB |
Output is correct |