이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#include "elephants.h"
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pii pair<int, int>
using namespace std;
#define SQ1 800
#define SQ2 400
int n, l;
int loc[150005];
int bid[150005];
vector<vector<pair<int , pii> > > buck;
//{LOC {count, last} }
inline int find(){
int res = 0;
int lst = -1;
for(auto& v : buck){
if(v.empty() || v.back().ff <= lst) continue;
int id = upper_bound(v.begin(), v.end(), lst, [](const int& lhs, const pair<int, pii>& rhs){
return lhs < rhs.ff;
}) - v.begin();
res += v[id].ss.ff;
lst = v[id].ss.ss;
}
return res;
}
inline void recalc(vector<pair<int, pii> >& vec){
int sz = vec.size();
int r = sz;
for(int l = sz - 1; l >= 0; l--){
while(vec[r - 1].ff > vec[l].ff + ::l) r--;
vec[l].ss.ff = (r == sz ? 0 : vec[r].ss.ff) + 1;
vec[l].ss.ss = (r == sz ? vec[l].ff + ::l : vec[r].ss.ss);
}
}
vector<pii> sorted;
inline void renew(int o){
for(int i = 0; i < n; i++){
sorted[i].ff = loc[i];
sorted[i].ss = i;
}
if(!o){
sort(sorted.begin(), sorted.end());
}
buck.clear();
buck.resize((n - 1) / SQ1 + 1);
for(int i = 0; i < n; i++){
buck[i / SQ1].push_back({sorted[i].ff, {0, 0}});
bid[sorted[i].ss] = i / SQ1;
}
for(auto& x : buck){
recalc(x);
}
}
void init(int N, int L, int X[]){
n = N; l = L;
for(int i = 0; i < n; i++){
loc[i] = X[i];
}
sorted.resize(n);
renew(1);
}
int cnt = 0;
inline void erase(vector<pair<int, pii> >& buck, int& x){
for(int i = 1; i < buck.size(); i++){
if(buck[i - 1].ff == x) swap(buck[i - 1], buck[i]);
}
buck.pop_back();
recalc(buck);
}
inline void insert(vector<pair<int, pii> >& buck, int& x){
buck.push_back({x, {0, 0}});
int i = buck.size() - 1;
while(i != 0 && buck[i - 1].ff > buck[i].ff){
swap(buck[i - 1], buck[i]);
i--;
}
recalc(buck);
}
int update(int j, int y){
if(loc[j] == y) return find();
if(++cnt % SQ2 == 0){
loc[j] = y;
renew(0);
} else {
erase(buck[bid[j]], loc[j]);
int t = upper_bound(buck.begin(), buck.end(), y, [](const int& lhs, const vector<pair<int, pii> >& rhs){
return rhs.back().ff >= lhs;
}) - buck.begin();
if(t == buck.size()) t--;
insert(buck[t], y);
loc[j] = y;
bid[j] = t;
}
return find();
}
컴파일 시 표준 에러 (stderr) 메시지
elephants.cpp: In function 'void erase(std::vector<std::pair<int, std::pair<int, int> > >&, int&)':
elephants.cpp:69:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < buck.size(); i++){
~~^~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:94:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(t == buck.size()) t--;
~~^~~~~~~~~~~~~~
# | 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... |