#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
constexpr int maxn = 1.5e5;
const int r = sqrt(maxn) + 1, b = maxn / r;
map<int, int> m;
int n, l;
struct Node {
int siz;
// Positions of the elephants sorted. First is first element, last is last element
// Number of cameras starting in this group given that a number of elephants in allready
// acounted for
vector <pii> cam;
// cam[i] means number of cameras needed given that i-th elephant is first we need
vector <int> pos;
void init(const vector <int> &positions)
{
pos = positions;
siz = positions.size();
calc();
}
void ins(const int &p) // O(n)
{
pos.insert(upper_bound(pos.begin(), pos.end(), p), p);
siz++;
calc();
}
void rem(const int &p) // O(n)
{
pos.erase(lower_bound(pos.begin(), pos.end(), p));
siz--;
calc();
}
void calc() // O(r**2 )
{
cam = vector <pii> (siz + 1);
cam[siz].first = 0; // All elephants are covered
cam[siz].second = -1;
int furthest;
for (int y = 0; y < siz; y++)
{
furthest = -1;
for (int i = y; i < siz; i++)
{
if (pos[i] > furthest)
{
furthest = pos[i] + l;
cam[y].first++;
}
}
cam[y].second = furthest;
}
}
// Function for splitting the group if it is 2r in size
} ;
vector <Node> groups(r);
int find_group(int pos)
{
int lower = 0, higher = groups.size() - 1, mid;
while (lower != higher)
{
mid = (lower + higher + 1) >> 1;
// cout << lower << " " << mid << " " << higher << endl;
if (groups[mid].pos.size() == 0 || groups[mid].pos[0] > pos) higher = mid - 1;
else lower = mid;
}
return lower;
}
void print_groups()
{
cout << r << "\n";
for (int i = 0; i < r; i++)
{
cout << i << "\n";
cout << groups[i].siz << "\n";
if (groups[i].siz == 0) continue;
for (int q : groups[i].pos) cout << q << " ";
cout << "\n";
for (pii q : groups[i].cam) cout << q.first << " ";
cout << "\n";
for (pii q : groups[i].cam) cout << q.second << " ";
cout << "\n\n";
}
}
int all()
{
int out = 0,
furthest = -1,
pos;
for (auto g : groups) {
if (g.siz == 0) continue;
pos = upper_bound(g.pos.begin(), g.pos.end(), furthest) - g.pos.begin();
out += g.cam[pos].first;
furthest = max(furthest, g.cam[pos].second);
}
return out;
}
void init(int N, int L, int X[])
{
n = N, l = L;
// Keeping track of the positions of the elephants by number
for (int i = 0; i < n; i++) // O(n log(n))
{
m.insert(pii(i, X[i]));
}
int i;
for (i = 0; i < n - r; i += r)
{
groups[i / r].init(vector<int>(X + i, X + i + r));
}
groups[i / r].init(vector<int>(X + i, X + n));
}
int update(int i, int y)
{
groups[find_group(m[i])].rem(m[i]);
m[i] = y;
groups[find_group(y)].ins(y);
return all();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Execution timed out |
9102 ms |
2840 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Execution timed out |
9102 ms |
2840 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Execution timed out |
9102 ms |
2840 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |