This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
int n, k;
const int N = 150000;
const int K = 250;
int dp[N+5], dokle[N+5], pos[N+5];
vector <pair <int, int>> vec[N+5];
void Calc(int tr){
if(vec[tr].empty()) return;
int j = vec[tr].size() - 1;
for(int i=vec[tr].size()-1; i>=0; i--){
while(vec[tr][i].first + k - 1 < vec[tr][j].first) j--;
if(j == vec[tr].size() - 1){
dp[vec[tr][i].second] = 1;
dokle[vec[tr][i].second] = vec[tr][i].first + k - 1;
}
else{
dp[vec[tr][i].second] = dp[vec[tr][j+1].second] + 1;
dokle[vec[tr][i].second] = dokle[vec[tr][j+1].second];
}
}
}
void Build(const vector <pair <int, int>> &v){
for(int i=1; i<=n; i++) vec[i].clear();
int tr = 1;
for(auto c : v){
if(vec[tr].size() > K){
Calc(tr);
tr++;
}
vec[tr].push_back(c);
}
Calc(tr);
}
void Rebuild(){
vector <pair <int, int>> v;
for(int i=1; i<=n; i++) for(auto c : vec[i]) v.push_back(c);
Build(v);
}
int query(){
int tr = -1, res = 0;
for(int i=1; i<=n; i++){
auto x = upper_bound(vec[i].begin(), vec[i].end(), make_pair(tr, n+5));
if(x == vec[i].end()) continue;
res += dp[x->second];
tr = dokle[x->second];
}
return res;
}
void init(int _n, int L, int X[]){
n = _n, k = L + 1;
vector <pair <int, int>> x;
for(int i=0; i<n; i++){
pos[i] = X[i];
x.push_back({X[i], i});
}
Build(x);
}
void brisi(int ind, int tr){
for(int i=0; i<vec[tr].size(); i++){
if(vec[tr][i] == make_pair(pos[ind], ind)){
vec[tr].erase(vec[tr].begin() + i);
return;
}
}
}
void dodaj(int ind, int tr){
for(int i=0; i<vec[tr].size(); i++){
if(vec[tr][i] > make_pair(pos[ind], ind)){
vec[tr].insert(vec[tr].begin() + i, {pos[ind], ind});
return;
}
}
vec[tr].push_back({pos[ind], ind});
return;
}
int update(int ind, int y){
int gde = 1;
for(int i=1; i<=2+n/K; i++){
if(vec[i].empty()) continue;
gde = i;
if(make_pair(pos[ind], ind) <= vec[i].back()) break;
}
brisi(ind, gde);
Calc(gde);
pos[ind] = y;
gde = 1;
for(int i=1; i<=2+n/K; i++){
if(vec[i].empty()) continue;
gde = i;
if(make_pair(pos[ind], ind) <= vec[i].back()) break;
}
dodaj(ind, gde);
Calc(gde);
if(vec[gde].size() > 2*K) Rebuild();
return query();
}
Compilation message (stderr)
elephants.cpp: In function 'void Calc(int)':
elephants.cpp:19:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if(j == vec[tr].size() - 1){
| ~~^~~~~~~~~~~~~~~~~~~~~
elephants.cpp: In function 'void brisi(int, int)':
elephants.cpp:71:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i=0; i<vec[tr].size(); i++){
| ~^~~~~~~~~~~~~~~
elephants.cpp: In function 'void dodaj(int, int)':
elephants.cpp:80:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i=0; i<vec[tr].size(); i++){
| ~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |