이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
const int k_N = 15e4 + 3;
const int k_B = 500;
int n, l;
int *x;
vector<vector<int>> idx, val;
vector<vector<array<int, 2>>> p;
map<int, int> mp;
void init(int N, int L, int X[]){
n = N;
l = L;
x = X;
for(int i = 0; i < n; ++i){
if(i && x[i] == x[i - 1]) continue;
if(idx.empty() || idx.back().size() >= k_B){
idx.push_back({});
val.push_back({});
}
idx.back().push_back(i);
val.back().push_back(x[i]);
mp[i] = (int)idx.size() - 1;
}
}
int get_ans(){
int bigger = 0, ans = 0;
for(int i = 0; i < idx.size(); ++i){
auto it = lower_bound(val[i].begin(), val[i].end(), bigger);
if(it == val[i].end()) continue;
int j = it - val[i].begin();
ans += p[i][j][0];
bigger = p[i][j][1];
}
return ans;
}
void calc_p(int i){
int ptr = idx[i].size();
p[i].resize(idx[i].size());
for(int j = (int)idx[i].size() - 1; j >= 0; --j){
while(idx[i][ptr - 1] - idx[i][j] > l)
ptr--;
if(ptr == idx[i].size())
p[i][j] = {1, val[i][j] + l};
else
p[i][j] = {p[i][ptr][0] + 1, p[i][ptr][1]};
}
}
void rebuild(){
vector<int> val_tmp;
vector<int> idx_tmp;
for(int i = 0; i < idx.size(); ++i){
for(int x: idx[i])
idx_tmp.push_back(x);
for(int x: val[i])
val_tmp.push_back(x);
}
idx.clear();
val.clear();
for(int i = 0; i < val_tmp.size(); ++i){
if(idx.empty() || idx.back().size() >= k_B){
idx.push_back({});
val.push_back({});
}
idx.back().push_back(idx_tmp[i]);
val.back().push_back(val_tmp[i]);
}
for(int i = 0; i < idx.size(); ++i)
calc_p(i);
}
void remove(int t, int i){
int j = mp[i];
for(int k = 0; k < idx[j].size(); ++j){
if(idx[j][k] == i){
idx[j].erase(idx[j].begin() + k);
val[j].erase(val[j].begin() + k);
break;
}
}
calc_p(j);
}
void add(int t, int i){
int j;
for(j = 0; j < idx.size(); ++j){
if(idx[j].empty()) continue;
if(t <= val[j].back())
break;
}
if(j == (int)idx.size()){
j--;
idx.back().push_back(i);
val.back().push_back(t);
calc_p(j);
return;
}
for(int k = 0; k < idx[j].size(); ++j){
if(t <= val[j][k]){
idx[j].insert(idx[j].begin() + k, i);
val[j].insert(val[j].begin() + k, t);
break;
}
}
calc_p(j);
}
int cnt_upd = 0;
int update(int i, int y){
if(cnt_upd++ % k_B == k_B - 1)
rebuild();
remove(x[i], i);
x[i] = y;
add(x[i], i);
return get_ans();
}
/*
4 4 3
3 4 6 7
0 5 1
3 9 2
3 8 1
*/
컴파일 시 표준 에러 (stderr) 메시지
elephants.cpp: In function 'int get_ans()':
elephants.cpp:35:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < idx.size(); ++i){
~~^~~~~~~~~~~~
elephants.cpp: In function 'void calc_p(int)':
elephants.cpp:52:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ptr == idx[i].size())
~~~~^~~~~~~~~~~~~~~~
elephants.cpp: In function 'void rebuild()':
elephants.cpp:63:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < idx.size(); ++i){
~~^~~~~~~~~~~~
elephants.cpp:73:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < val_tmp.size(); ++i){
~~^~~~~~~~~~~~~~~~
elephants.cpp:82:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < idx.size(); ++i)
~~^~~~~~~~~~~~
elephants.cpp: In function 'void remove(int, int)':
elephants.cpp:88:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = 0; k < idx[j].size(); ++j){
~~^~~~~~~~~~~~~~~
elephants.cpp: In function 'void add(int, int)':
elephants.cpp:100:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < idx.size(); ++j){
~~^~~~~~~~~~~~
elephants.cpp:112:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = 0; k < idx[j].size(); ++j){
~~^~~~~~~~~~~~~~~
# | 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... |