#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(),a.end()
const int mxN = (int)1.5e5+10;
const int INF = (int)1e9;
const int B = 200;
const int mxB = mxN/B+1;
set<int> pos[mxB+2];
int n, L, tim, *p;
unordered_map<int,int> b;
set<array<int,3>> bl[mxB+2];
void reUpd(int i){
bl[i].clear();
auto itr = end(pos[i]), stop = begin(pos[i]);
while(itr!=stop){ itr--;
auto itr2 = bl[i].lower_bound({*itr+L+1,-1,-1});
if(itr2!=bl[i].end())
bl[i].insert({*itr,1+(*itr2)[1],(*itr2)[2]});
else bl[i].insert({*itr,1,*itr+L+1});
}
}
int findBl(int x){
for(int i = 0; i < mxB; i++){
if(pos[i].empty()) return (i==0?i:i-1);
if(*prev(end(pos[i])) < x) continue;
}
}
void ins(int x, int i=-1){
b[x]++; if(b[x]!=1) return;
if(i==-1) i=findBl(x);
pos[i].insert(x), reUpd(i);
}
void del(int x, int i=-1){
b[x]--; if(b[x]) return;
if(i==-1) i = findBl(x);
pos[i].erase(x), reUpd(i);
}
void build(){
vector<int> v; v.clear(); b.clear();
for(int i = 0; i < mxB; i++)
pos[i].clear(), bl[i].clear();
for(int i = 0; i < n; i++) v.push_back(p[i]);
sort(all(v)); v.erase(unique(all(v)),end(v));
for(int j = 0; auto u : v){
b[u]++; pos[j].insert(u);
if(pos[j].size() >= B) j++;
}
for(int i = 0; i < mxB; i++) reUpd(i);
return;
for(int i = 0; i < mxB; i++){
if(pos[i].empty()) continue;
cout << i << ": ";
for(auto u : pos[i]) cout << u << " ";
cout << "\n";
}
}
void init(int N, int l, int X[]){
n = N; L = l; p=X; build();
}
int update(int i, int y)
{
del(p[i]); p[i]=y; ins(y);
if((++tim)%B==0) build();
int ans = 0, cur = 0;
for(int i = 0; i < mxB; i++){
auto itr = bl[i].lower_bound({cur,-1,-1});
if(itr!=bl[i].end()) ans+=(*itr)[1], cur = (*itr)[2];
}
return ans;
}
Compilation message
elephants.cpp: In function 'void build()':
elephants.cpp:52:20: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
52 | for(int j = 0; auto u : v){
| ^~~~
elephants.cpp: In function 'int findBl(int)':
elephants.cpp:32:1: warning: control reaches end of non-void function [-Wreturn-type]
32 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
24 ms |
3908 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
24 ms |
3908 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Incorrect |
24 ms |
3908 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |