#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
#define MAX 150010
#define B 400
int N;
int L;
int arr[MAX];
int cnt;
map<int, int> ncnt;
int gcnt;
set<int> allv;
set<pair<int, pi>> bukkit[MAX];
//refresh bukkit
void refb(int b) {
int K = bukkit[b].size();
set<pair<int, pi>> s1;
for (auto it = prev(bukkit[b].end());; it--) {
int x = it->first;
int e = x + L;
auto ub = s1.lower_bound(make_pair(e + 1, pi(0, 0)));
pair<int, pi> ins;
ins.first = x;
if (ub == s1.end()) ins.second = pi(1, e);
else ins.second = pi(ub->second.first + 1, ub->second.second);
s1.insert(ins);
if (it == bukkit[b].begin()) break;
}
bukkit[b].swap(s1);
}
void ref() {
gcnt = 0;
for (auto v : allv) {
if (bukkit[gcnt].size() == B) gcnt++;
bukkit[gcnt].insert(make_pair(v, pi(0, 0)));
}
gcnt++;
for (int i = 0; i < gcnt; i++) refb(i);
}
int calc() {
int en = -1, i;
int cnt = 0;
for (i = 0; i < gcnt; i++) {
if (bukkit[i].empty()) continue;
auto ub = bukkit[i].lower_bound(make_pair(en + 1, pi(0, 0)));
cnt += ub->second.first;
en = ub->second.second;
}
return cnt;
}
void init(int N, int L, int X[]) {
::N = N;
::L = L;
int i;
for (i = 0; i < N; i++) arr[i] = X[i], allv.insert(arr[i]), ncnt[arr[i]]++;
ref();
}
bool cmp(set<pair<int, pi>>& s1, set<pair<int, pi>>& s2) {
return s1.begin()->first < s2.begin()->first;
}
int update(int i, int y) {
cnt++;
if (cnt == B) ref();
int x = arr[i];
arr[i] = y;
ncnt[x]--;
if (ncnt[x] == 0) {
int i, ind = 0;
for (i = 0; i < gcnt; i++) if (bukkit[i].size() && bukkit[i].begin()->first <= x) ind = i;
auto it = bukkit[ind].lower_bound(make_pair(x, pi(0, 0)));
bukkit[ind].erase(it);
allv.erase(x);
refb(ind);
}
if (ncnt[y] == 0) {
allv.insert(y);
int i, ind = 0;
for (i = 0; i < gcnt; i++) if (bukkit[i].size() && bukkit[i].begin()->first <= y) ind = i;
bukkit[ind].insert(make_pair(y, pi(0, 0)));
allv.insert(y);
refb(ind);
}
ncnt[y]++;
return calc();
}
Compilation message
elephants.cpp: In function 'void refb(int)':
elephants.cpp:21:6: warning: unused variable 'K' [-Wunused-variable]
21 | int K = bukkit[b].size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
3 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
3 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
6 ms |
7372 KB |
Output is correct |
5 |
Correct |
6 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
3 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
6 ms |
7372 KB |
Output is correct |
5 |
Correct |
6 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Execution timed out |
9042 ms |
12660 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
3 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
6 ms |
7372 KB |
Output is correct |
5 |
Correct |
6 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Execution timed out |
9042 ms |
12660 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
3 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
6 ms |
7372 KB |
Output is correct |
5 |
Correct |
6 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Execution timed out |
9042 ms |
12660 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |