#include "elephants.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
const int nx=150005, tx=390;
int n, c[nx], v[nx], l, cnt;
multiset<int> ms[tx];
vector<int> vl[tx];
vector<pair<int, int>> dp[tx];
void calc(int x)
{
vl[x].resize(0);
for (auto a:ms[x]) vl[x].push_back(a);
dp[x].resize(vl[x].size());
if (vl[x].size()==0) return;
deque<int> dq;
dq.push_back(INT_MAX);
for (int i=vl[x].size()-1; i>=0; i--)
{
while (dq.size()>=2)
{
auto tmp=dq.back();
dq.pop_back();
if (vl[x][dq.back()]<=vl[x][i]+l)
{
dq.push_back(tmp);
break;
}
}
if (dq.back()==INT_MAX)
{
dp[x][i].first=vl[x][i]+l;
dp[x][i].second=1;
}
else
{
dp[x][i].first=dp[x][dq.back()].first;
dp[x][i].second=dp[x][dq.back()].second+1;
}
dq.push_front(i);
//if (cnt==5) cout<<x<<' '<<i<<' '<<vl[x][i]<<' '<<dp[x][i].first<<' '<<dp[x][i].second<<'\n';
}
}
void build()
{
vector<pair<int, int>> d(n);
for (int i=0; i<n; i++) d[i].first=v[i], d[i].second=i;
sort(d.begin(), d.end());
for (int i=0; i<tx; i++) ms[i].clear();
for (int i=0; i<n; i++) c[d[i].second]=i/tx, ms[i/tx].insert(d[i].first);
for (int i=0; i<tx; i++) calc(i);
}
int query()
{
int lst=-1, res=0;
for (int i=0; i<tx; i++)
{
if (vl[i].empty()||lst>=vl[i][vl[i].size()-1]) continue;
int idx=upper_bound(vl[i].begin(), vl[i].end(), lst)-vl[i].begin();
res+=dp[i][idx].second;
lst=dp[i][idx].first;
//if (cnt==5) cout<<i<<' '<<res<<' '<<lst<<'\n';
}
return res;
}
void init(int N, int L, int X[])
{
n=N; l=L;
for (int i=0; i<n; i++) v[i]=X[i];
build();
}
int update(int i, int y)
{
if ((++cnt)%1000==0)
{
v[i]=y;
build();
return query();
}
int loc=c[i];
ms[loc].erase(ms[loc].find(v[i]));
v[i]=y;
calc(loc);
for (int j=0; j<tx; j++)
{
if (j==tx-1)
{
ms[j].insert(y);
c[i]=j;
calc(j);
}
else if (!vl[j].empty()&&vl[j][vl[j].size()-1]>=y)
{
ms[j].insert(y);
c[i]=j;
calc(j);
break;
}
}
return query();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
659 ms |
9964 KB |
Output is correct |
8 |
Correct |
695 ms |
10356 KB |
Output is correct |
9 |
Correct |
762 ms |
12480 KB |
Output is correct |
10 |
Correct |
738 ms |
12204 KB |
Output is correct |
11 |
Correct |
739 ms |
12224 KB |
Output is correct |
12 |
Correct |
1299 ms |
13180 KB |
Output is correct |
13 |
Correct |
751 ms |
12212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
659 ms |
9964 KB |
Output is correct |
8 |
Correct |
695 ms |
10356 KB |
Output is correct |
9 |
Correct |
762 ms |
12480 KB |
Output is correct |
10 |
Correct |
738 ms |
12204 KB |
Output is correct |
11 |
Correct |
739 ms |
12224 KB |
Output is correct |
12 |
Correct |
1299 ms |
13180 KB |
Output is correct |
13 |
Correct |
751 ms |
12212 KB |
Output is correct |
14 |
Correct |
1022 ms |
10480 KB |
Output is correct |
15 |
Correct |
878 ms |
10856 KB |
Output is correct |
16 |
Correct |
1981 ms |
12972 KB |
Output is correct |
17 |
Correct |
2130 ms |
14692 KB |
Output is correct |
18 |
Correct |
2218 ms |
14656 KB |
Output is correct |
19 |
Correct |
1550 ms |
13572 KB |
Output is correct |
20 |
Correct |
2121 ms |
14660 KB |
Output is correct |
21 |
Correct |
2098 ms |
14832 KB |
Output is correct |
22 |
Correct |
1229 ms |
13568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
659 ms |
9964 KB |
Output is correct |
8 |
Correct |
695 ms |
10356 KB |
Output is correct |
9 |
Correct |
762 ms |
12480 KB |
Output is correct |
10 |
Correct |
738 ms |
12204 KB |
Output is correct |
11 |
Correct |
739 ms |
12224 KB |
Output is correct |
12 |
Correct |
1299 ms |
13180 KB |
Output is correct |
13 |
Correct |
751 ms |
12212 KB |
Output is correct |
14 |
Correct |
1022 ms |
10480 KB |
Output is correct |
15 |
Correct |
878 ms |
10856 KB |
Output is correct |
16 |
Correct |
1981 ms |
12972 KB |
Output is correct |
17 |
Correct |
2130 ms |
14692 KB |
Output is correct |
18 |
Correct |
2218 ms |
14656 KB |
Output is correct |
19 |
Correct |
1550 ms |
13572 KB |
Output is correct |
20 |
Correct |
2121 ms |
14660 KB |
Output is correct |
21 |
Correct |
2098 ms |
14832 KB |
Output is correct |
22 |
Correct |
1229 ms |
13568 KB |
Output is correct |
23 |
Correct |
4633 ms |
20956 KB |
Output is correct |
24 |
Correct |
4870 ms |
20612 KB |
Output is correct |
25 |
Correct |
4166 ms |
19680 KB |
Output is correct |
26 |
Correct |
4623 ms |
18992 KB |
Output is correct |
27 |
Correct |
5501 ms |
19136 KB |
Output is correct |
28 |
Correct |
2396 ms |
11988 KB |
Output is correct |
29 |
Correct |
2295 ms |
11992 KB |
Output is correct |
30 |
Correct |
2383 ms |
11976 KB |
Output is correct |
31 |
Correct |
2320 ms |
11868 KB |
Output is correct |
32 |
Correct |
4104 ms |
23440 KB |
Output is correct |
33 |
Correct |
3803 ms |
22768 KB |
Output is correct |
34 |
Correct |
4332 ms |
23648 KB |
Output is correct |
35 |
Correct |
4680 ms |
26812 KB |
Output is correct |
36 |
Correct |
5197 ms |
23432 KB |
Output is correct |
37 |
Correct |
7536 ms |
25744 KB |
Output is correct |
38 |
Correct |
4405 ms |
22608 KB |
Output is correct |
39 |
Correct |
5278 ms |
23696 KB |
Output is correct |
40 |
Correct |
4428 ms |
22708 KB |
Output is correct |
41 |
Correct |
8026 ms |
25780 KB |
Output is correct |
42 |
Execution timed out |
9006 ms |
25996 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |