#include "elephants.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
int n,l;
vi s;
int K;
const int oo = 2e9 + 5;
const int NN = 1e6 + 5;
int Step = 0;
multiset < int > ss;
vi v[NN];
vi dp1[NN];
vi dp2[NN];
int SZ;
void build_block(int w) {
int sz = v[w].size();
int index = sz;
dp1[w].resize(sz);
dp2[w].resize(sz);
for (int j = sz - 1;j >= 0;--j) {
while (v[w][j] + l < v[w][index - 1])
--index;
dp1[w][j] = 1;
dp2[w][j] = v[w][j];
if (index < sz)
dp1[w][j] += dp1[w][index],dp2[w][j] = dp2[w][index];
}
}
void del(int val) {
for (int i = 0;i < SZ;++i)
if (!v[i].empty() && v[i][0] <= val && val <= v[i].back()) {
vi nw;
for (auto it : v[i])
if (it != val)
nw.pb(it);
else
val = oo;
assert(nw.size() + 1 == v[i].size());
v[i] = nw;
build_block(i);
break;
}
}
void add(int val) {
int pr = SZ - 1;
for (int i = 0;i < SZ;++i)
if (!v[i].empty() && val <= v[i].back()) {
pr = i;
break;
}
vi nw;
for (auto it : v[pr]) {
if (it > val)
nw.pb(val),val = oo;
nw.pb(it);
}
if (val != oo)
nw.pb(val);
v[pr] = nw;
build_block(pr);
}
void build(void) {
int index = -1;
SZ = (ss.size() + K - 1) / K;
for (int i = 0;i < SZ;++i)
v[i].clear(),dp1[i].clear(),dp2[i].clear();
for (auto it : ss) {
++index;
v[index / K].pb(it);
}
for (int i = 0;i < SZ;++i) {
build_block(i);
}
}
void init(int N, int L, int X[]) {
n = N;
K = sqrt(n * log2(n));
l = L;
for (int i = 0;i < n;++i)
s.pb(X[i]),ss.insert(X[i]);
}
int update(int i, int y) {
if (!(Step % K)) {
build();
}
++Step;
ss.erase(ss.find(s[i]));
del(s[i]);
ss.insert(y);
add(s[i] = y);
int prev = -oo;
int ans = 0;
for (int i = 0;i < SZ;++i) {
if (v[i].empty())
continue;
int low = lower_bound(all(v[i]),prev) - v[i].begin();
if (low != v[i].size()) {
ans += dp1[i][low];
prev = dp2[i][low] + l + 1;
}
}
return ans;
}
Compilation message
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:126:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (low != v[i].size()) {
~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
70776 KB |
Output is correct |
2 |
Correct |
77 ms |
70884 KB |
Output is correct |
3 |
Correct |
65 ms |
70964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
70776 KB |
Output is correct |
2 |
Correct |
77 ms |
70884 KB |
Output is correct |
3 |
Correct |
65 ms |
70964 KB |
Output is correct |
4 |
Correct |
67 ms |
71036 KB |
Output is correct |
5 |
Correct |
70 ms |
71112 KB |
Output is correct |
6 |
Correct |
65 ms |
71112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
70776 KB |
Output is correct |
2 |
Correct |
77 ms |
70884 KB |
Output is correct |
3 |
Correct |
65 ms |
70964 KB |
Output is correct |
4 |
Correct |
67 ms |
71036 KB |
Output is correct |
5 |
Correct |
70 ms |
71112 KB |
Output is correct |
6 |
Correct |
65 ms |
71112 KB |
Output is correct |
7 |
Correct |
862 ms |
72736 KB |
Output is correct |
8 |
Correct |
1047 ms |
73144 KB |
Output is correct |
9 |
Correct |
1171 ms |
75512 KB |
Output is correct |
10 |
Correct |
1052 ms |
75512 KB |
Output is correct |
11 |
Correct |
1035 ms |
75512 KB |
Output is correct |
12 |
Correct |
1672 ms |
75624 KB |
Output is correct |
13 |
Correct |
980 ms |
75624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
70776 KB |
Output is correct |
2 |
Correct |
77 ms |
70884 KB |
Output is correct |
3 |
Correct |
65 ms |
70964 KB |
Output is correct |
4 |
Correct |
67 ms |
71036 KB |
Output is correct |
5 |
Correct |
70 ms |
71112 KB |
Output is correct |
6 |
Correct |
65 ms |
71112 KB |
Output is correct |
7 |
Correct |
862 ms |
72736 KB |
Output is correct |
8 |
Correct |
1047 ms |
73144 KB |
Output is correct |
9 |
Correct |
1171 ms |
75512 KB |
Output is correct |
10 |
Correct |
1052 ms |
75512 KB |
Output is correct |
11 |
Correct |
1035 ms |
75512 KB |
Output is correct |
12 |
Correct |
1672 ms |
75624 KB |
Output is correct |
13 |
Correct |
980 ms |
75624 KB |
Output is correct |
14 |
Correct |
999 ms |
75624 KB |
Output is correct |
15 |
Correct |
1459 ms |
75624 KB |
Output is correct |
16 |
Correct |
2686 ms |
75640 KB |
Output is correct |
17 |
Correct |
2965 ms |
77428 KB |
Output is correct |
18 |
Correct |
3323 ms |
77428 KB |
Output is correct |
19 |
Correct |
1783 ms |
77428 KB |
Output is correct |
20 |
Correct |
2861 ms |
77556 KB |
Output is correct |
21 |
Correct |
2776 ms |
77556 KB |
Output is correct |
22 |
Correct |
1709 ms |
77556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
70776 KB |
Output is correct |
2 |
Correct |
77 ms |
70884 KB |
Output is correct |
3 |
Correct |
65 ms |
70964 KB |
Output is correct |
4 |
Correct |
67 ms |
71036 KB |
Output is correct |
5 |
Correct |
70 ms |
71112 KB |
Output is correct |
6 |
Correct |
65 ms |
71112 KB |
Output is correct |
7 |
Correct |
862 ms |
72736 KB |
Output is correct |
8 |
Correct |
1047 ms |
73144 KB |
Output is correct |
9 |
Correct |
1171 ms |
75512 KB |
Output is correct |
10 |
Correct |
1052 ms |
75512 KB |
Output is correct |
11 |
Correct |
1035 ms |
75512 KB |
Output is correct |
12 |
Correct |
1672 ms |
75624 KB |
Output is correct |
13 |
Correct |
980 ms |
75624 KB |
Output is correct |
14 |
Correct |
999 ms |
75624 KB |
Output is correct |
15 |
Correct |
1459 ms |
75624 KB |
Output is correct |
16 |
Correct |
2686 ms |
75640 KB |
Output is correct |
17 |
Correct |
2965 ms |
77428 KB |
Output is correct |
18 |
Correct |
3323 ms |
77428 KB |
Output is correct |
19 |
Correct |
1783 ms |
77428 KB |
Output is correct |
20 |
Correct |
2861 ms |
77556 KB |
Output is correct |
21 |
Correct |
2776 ms |
77556 KB |
Output is correct |
22 |
Correct |
1709 ms |
77556 KB |
Output is correct |
23 |
Correct |
6610 ms |
83784 KB |
Output is correct |
24 |
Correct |
7235 ms |
84064 KB |
Output is correct |
25 |
Correct |
5121 ms |
84064 KB |
Output is correct |
26 |
Correct |
6356 ms |
84064 KB |
Output is correct |
27 |
Correct |
8230 ms |
84064 KB |
Output is correct |
28 |
Correct |
1687 ms |
84064 KB |
Output is correct |
29 |
Correct |
1694 ms |
84064 KB |
Output is correct |
30 |
Correct |
1689 ms |
84064 KB |
Output is correct |
31 |
Correct |
1566 ms |
84064 KB |
Output is correct |
32 |
Correct |
4532 ms |
84064 KB |
Output is correct |
33 |
Correct |
3956 ms |
84064 KB |
Output is correct |
34 |
Correct |
3997 ms |
84064 KB |
Output is correct |
35 |
Correct |
5329 ms |
84940 KB |
Output is correct |
36 |
Correct |
4624 ms |
84940 KB |
Output is correct |
37 |
Correct |
8089 ms |
84940 KB |
Output is correct |
38 |
Correct |
4231 ms |
84940 KB |
Output is correct |
39 |
Correct |
5094 ms |
84940 KB |
Output is correct |
40 |
Correct |
4081 ms |
84940 KB |
Output is correct |
41 |
Execution timed out |
9054 ms |
84940 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |