// Ignut
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> ask(int i);
int Q = 0;
struct SegmentTree {
vector<int> t;
void Init(int n) {
t.assign(4 * n, 0);
}
void Add(int v, int l, int r, int pos) {
t[v] ++;
if (l == r) return;
int mid = l + (r - l) / 2;
if (pos <= mid)
Add(v * 2, l, mid, pos);
else
Add(v * 2 + 1, mid + 1, r, pos);
}
int Sum(int v, int l, int r, int ql, int qr) {
if (l > qr || ql > r)
return 0;
if (l >= ql && r <= qr)
return t[v];
int mid = l + (r - l) / 2;
return Sum(v * 2, l, mid, ql, qr) + Sum(v * 2 + 1, mid + 1, r, ql, qr);
}
};
int n;
int cntHave = 0;
SegmentTree st[10];
map<int, int> index_;
void Inc(int lvl, int pos) {
if (!index_.count(lvl)) {
st[cntHave].Init(n);
index_[lvl] = cntHave ++;
}
st[index_[lvl]].Add(1, 0, n - 1, pos);
}
int Get(int lvl, int l, int r) {
if (!index_.count(lvl)) return 0;
return st[index_[lvl]].Sum(1, 0, n - 1, l, r);
}
int find_best(int nn) {
n = nn;
int L = -1;
while (L < n - 1) {
int lo = L + 1, hi = n - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
Q ++;
if (Q == 10000) {
Q /= 0;
}
vector<int> vec = ask(mid);
if (vec[0] + vec[1] == 0)
return mid;
int lvl = vec[0] + vec[1];
int comp = 0;
for (auto [a, b] : index_) if (a < lvl) comp += Get(a, 0, mid - 1);
Inc(lvl, mid);
if (vec[0] > comp)
hi = mid - 1;
else {
lo = mid + 1;
}
}
// cout << "lo = " << lo << '\n';
Q ++;
if (Q == 10000) {
Q /= 0;
}
vector<int> vec = ask(lo);
if (vec[0] + vec[1] == 0)
return lo;
int lvl = vec[0] + vec[1];
Inc(lvl, lo);
L = lo;
}
return n - 1;
}
/*
8
3 2 3 1 3 3 2 3
*/
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:66:7: warning: division by zero [-Wdiv-by-zero]
66 | Q /= 0;
| ~~^~~~
prize.cpp:84:6: warning: division by zero [-Wdiv-by-zero]
84 | Q /= 0;
| ~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3416 KB |
Output is correct |
3 |
Correct |
2 ms |
3416 KB |
Output is correct |
4 |
Correct |
1 ms |
3416 KB |
Output is correct |
5 |
Correct |
2 ms |
3416 KB |
Output is correct |
6 |
Correct |
2 ms |
3508 KB |
Output is correct |
7 |
Correct |
2 ms |
3416 KB |
Output is correct |
8 |
Correct |
2 ms |
3492 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
2 ms |
3416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3500 KB |
Output is correct |
2 |
Correct |
2 ms |
3500 KB |
Output is correct |
3 |
Correct |
2 ms |
3524 KB |
Output is correct |
4 |
Correct |
2 ms |
3416 KB |
Output is correct |
5 |
Correct |
1 ms |
3416 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
2 ms |
3500 KB |
Output is correct |
8 |
Correct |
2 ms |
3416 KB |
Output is correct |
9 |
Correct |
0 ms |
428 KB |
Output is correct |
10 |
Correct |
2 ms |
3416 KB |
Output is correct |
11 |
Correct |
8 ms |
12900 KB |
Output is correct |
12 |
Correct |
2 ms |
3416 KB |
Output is correct |
13 |
Runtime error |
60 ms |
19556 KB |
Execution killed with signal 4 |
14 |
Halted |
0 ms |
0 KB |
- |