#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 = 2;
const int mxB = mxN/B+1;
set<int> pos[mxB+2];
int n, L, tim, *p;
int le[mxB+2], ri[mxB+2];
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(le[i]<=x and x<=ri[i]) return i;
return mxB-1;
}
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();
for(int i = 0; i < mxB; i++)
pos[i].clear(), bl[i].clear(), le[i]=-1, ri[i]=INF;
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){
ri[j]=u; b[j]++; pos[j].insert(u);
if(le[j]==-1) le[j]=u;
if(pos[j].size() >= B) j++;
}
for(int i = 0; i < mxB; i++) reUpd(i);
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 or 1) 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){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
7956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
7956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
7956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
7956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
113 ms |
7956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |