#include "prize.h"
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 55;
const int K = N/455;
vector<int>memo[N]; int done[N];
int Q;
set<int>alive;
void del(int l, int r) {
while (alive.size()) {
auto it = alive.lower_bound(l);
if (it == alive.end()) break;
if (*it <= r) alive.erase(it);
else break;
}
}
set<int>pos[N];
vector<int>qwe(int i) {
if (done[i]) return memo[i];
done[i] = 1; memo[i] = ask(i);
int x = memo[i][0] + memo[i][1];
if (x == 0) return memo[i];
{
auto it = pos[x].lower_bound(i);
if (it == pos[x].end() && memo[i][1] == 0) del(i, N);
if (it != pos[x].end() && memo[i][1] == memo[*it][1]) del(i, *it);
if (it == pos[x].begin() && memo[i][0] == 0) del(0, i);
if (it != pos[x].begin() && memo[i][0] == memo[*prev(it)][0]) del(*prev(it), i);
pos[x].insert(i);
}
return memo[i];
}
int solve(int need, int ql, int qr, int mx) {
if (ql > qr) return -1;
int l = ql, r = qr, ret = -1;
while (l <= r) {
int mid = (l + r) >> 1;
auto it = alive.lower_bound(mid);
int m = -1;
if (it != alive.end() && *it <= r) m = *it;
else if (it != alive.begin() && *prev(it) >= l && *prev(it) <= r) m = *prev(it);
else break;
if (qwe(m)[0] + qwe(m)[1] == 0) return m;
if (qwe(m)[0] + qwe(m)[1] != mx) ret = m, r = m - 1;
else if (qwe(m)[0] == need) l = m + 1;
else r = m - 1;
}
if (ret == -1) return -1;
r = ret;
while (r <= qr) {
//if (alive.count(r) == 0) {
// r++;
// continue;
//}
if (qwe(r)[0] + qwe(r)[1] == 0) return r;
if (qwe(r)[0] + qwe(r)[1] == mx) break;
++r;
}
return solve(qwe(r)[0], r, qr, mx);
}
int find_best(int n) {
for (int i = 0 ; i < n ; ++ i) alive.insert(i);
//if (n <= 10000) {
//for (int i = 0 ; i < n ; ++ i) {
//if (alive.count(i) == 0) continue;
//if (qwe(i)[0] + qwe(i)[1] == 0) return i;
//}
//}
vector<int>ids;
int mx = 0;
for (int i = 0 ; i < n - 1 ; i += K) {
if (qwe(i)[0] + qwe(i)[1] == 0) return i;
mx = max(mx, qwe(i)[0] + qwe(i)[1]);
ids.emplace_back(i);
}
if (qwe(n - 1)[0] + qwe(n - 1)[1] == 0) return n - 1;
mx = max(mx, qwe(n-1)[0] + qwe(n-1)[1]);
ids.emplace_back(n - 1);
for (int i = 0 ; i + 1 < (int)ids.size() ; ++ i) {
int x = ids[i], y = ids[i + 1];
while (x <= y) {
if (qwe(x)[0] + qwe(x)[1] == 0) return x;
if (qwe(x)[0] + qwe(x)[1] == mx) break;
++x;
}
while (x <= y) {
if (qwe(y)[0] + qwe(y)[1] == 0) return y;
if (qwe(y)[0] + qwe(y)[1] == mx) break;
--y;
}
if (x > y) continue;
int res = solve(qwe(x)[0], x, y, mx);
if (res != -1) return res;
}
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
24480 KB |
Output is correct |
2 |
Correct |
89 ms |
24464 KB |
Output is correct |
3 |
Correct |
105 ms |
24488 KB |
Output is correct |
4 |
Correct |
93 ms |
24516 KB |
Output is correct |
5 |
Correct |
93 ms |
24512 KB |
Output is correct |
6 |
Correct |
68 ms |
23740 KB |
Output is correct |
7 |
Correct |
112 ms |
24460 KB |
Output is correct |
8 |
Correct |
84 ms |
24512 KB |
Output is correct |
9 |
Correct |
87 ms |
24508 KB |
Output is correct |
10 |
Correct |
84 ms |
24476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
24476 KB |
Output is correct |
2 |
Correct |
85 ms |
24512 KB |
Output is correct |
3 |
Correct |
87 ms |
24444 KB |
Output is correct |
4 |
Correct |
84 ms |
24536 KB |
Output is correct |
5 |
Correct |
90 ms |
24476 KB |
Output is correct |
6 |
Correct |
67 ms |
23716 KB |
Output is correct |
7 |
Correct |
82 ms |
24532 KB |
Output is correct |
8 |
Correct |
87 ms |
24476 KB |
Output is correct |
9 |
Correct |
100 ms |
24460 KB |
Output is correct |
10 |
Correct |
82 ms |
24460 KB |
Output is correct |
11 |
Correct |
103 ms |
24468 KB |
Output is correct |
12 |
Correct |
92 ms |
24488 KB |
Output is correct |
13 |
Correct |
89 ms |
24444 KB |
Output is correct |
14 |
Correct |
20 ms |
15432 KB |
Output is correct |
15 |
Correct |
78 ms |
24484 KB |
Output is correct |
16 |
Correct |
114 ms |
24584 KB |
Output is correct |
17 |
Correct |
75 ms |
24584 KB |
Output is correct |
18 |
Correct |
154 ms |
24588 KB |
Output is correct |
19 |
Correct |
81 ms |
24496 KB |
Output is correct |
20 |
Correct |
57 ms |
19416 KB |
Output is correct |
21 |
Correct |
91 ms |
24492 KB |
Output is correct |
22 |
Correct |
71 ms |
24512 KB |
Output is correct |
23 |
Correct |
84 ms |
24512 KB |
Output is correct |
24 |
Correct |
84 ms |
24504 KB |
Output is correct |
25 |
Correct |
114 ms |
24480 KB |
Output is correct |
26 |
Correct |
85 ms |
24460 KB |
Output is correct |
27 |
Correct |
91 ms |
24472 KB |
Output is correct |
28 |
Correct |
147 ms |
24532 KB |
Output is correct |
29 |
Correct |
115 ms |
24604 KB |
Output is correct |
30 |
Correct |
125 ms |
24640 KB |
Output is correct |
31 |
Correct |
71 ms |
24488 KB |
Output is correct |
32 |
Correct |
102 ms |
24472 KB |
Output is correct |
33 |
Correct |
12 ms |
14280 KB |
Output is correct |
34 |
Correct |
91 ms |
24488 KB |
Output is correct |
35 |
Correct |
87 ms |
24480 KB |
Output is correct |
36 |
Correct |
83 ms |
24548 KB |
Output is correct |
37 |
Correct |
88 ms |
24580 KB |
Output is correct |
38 |
Correct |
89 ms |
24484 KB |
Output is correct |
39 |
Correct |
81 ms |
24592 KB |
Output is correct |
40 |
Correct |
128 ms |
24512 KB |
Output is correct |
41 |
Correct |
104 ms |
24516 KB |
Output is correct |
42 |
Correct |
117 ms |
24488 KB |
Output is correct |
43 |
Correct |
115 ms |
24488 KB |
Output is correct |
44 |
Correct |
99 ms |
24552 KB |
Output is correct |
45 |
Correct |
94 ms |
24588 KB |
Output is correct |
46 |
Correct |
71 ms |
24488 KB |
Output is correct |
47 |
Correct |
98 ms |
24512 KB |
Output is correct |
48 |
Correct |
153 ms |
24512 KB |
Output is correct |
49 |
Correct |
74 ms |
24548 KB |
Output is correct |
50 |
Correct |
151 ms |
24484 KB |
Output is correct |
51 |
Correct |
104 ms |
24592 KB |
Output is correct |
52 |
Correct |
66 ms |
24484 KB |
Output is correct |
53 |
Correct |
87 ms |
24440 KB |
Output is correct |
54 |
Correct |
105 ms |
24588 KB |
Output is correct |
55 |
Correct |
66 ms |
24508 KB |
Output is correct |
56 |
Correct |
134 ms |
24472 KB |
Output is correct |
57 |
Correct |
122 ms |
24612 KB |
Output is correct |
58 |
Correct |
97 ms |
24600 KB |
Output is correct |
59 |
Correct |
120 ms |
24624 KB |
Output is correct |
60 |
Correct |
118 ms |
24508 KB |
Output is correct |
61 |
Correct |
83 ms |
24512 KB |
Output is correct |
62 |
Correct |
86 ms |
24496 KB |
Output is correct |
63 |
Correct |
80 ms |
24528 KB |
Output is correct |
64 |
Correct |
102 ms |
24508 KB |
Output is correct |
65 |
Correct |
89 ms |
24424 KB |
Output is correct |
66 |
Correct |
84 ms |
24540 KB |
Output is correct |
67 |
Correct |
80 ms |
24464 KB |
Output is correct |
68 |
Correct |
91 ms |
24568 KB |
Output is correct |
69 |
Correct |
83 ms |
24460 KB |
Output is correct |
70 |
Correct |
78 ms |
24476 KB |
Output is correct |
71 |
Correct |
123 ms |
24588 KB |
Output is correct |
72 |
Correct |
87 ms |
24512 KB |
Output is correct |
73 |
Correct |
155 ms |
24536 KB |
Output is correct |
74 |
Correct |
147 ms |
24496 KB |
Output is correct |
75 |
Correct |
87 ms |
24512 KB |
Output is correct |
76 |
Correct |
150 ms |
24512 KB |
Output is correct |
77 |
Correct |
142 ms |
24512 KB |
Output is correct |
78 |
Correct |
100 ms |
24424 KB |
Output is correct |
79 |
Correct |
112 ms |
24512 KB |
Output is correct |
80 |
Correct |
107 ms |
24536 KB |
Output is correct |
81 |
Correct |
117 ms |
24512 KB |
Output is correct |
82 |
Correct |
140 ms |
24584 KB |
Output is correct |
83 |
Correct |
103 ms |
24452 KB |
Output is correct |
84 |
Correct |
102 ms |
24456 KB |
Output is correct |
85 |
Correct |
126 ms |
24640 KB |
Output is correct |
86 |
Correct |
106 ms |
24512 KB |
Output is correct |
87 |
Correct |
86 ms |
24480 KB |
Output is correct |
88 |
Correct |
95 ms |
24492 KB |
Output is correct |
89 |
Correct |
108 ms |
24428 KB |
Output is correct |
90 |
Correct |
84 ms |
24508 KB |
Output is correct |
91 |
Correct |
98 ms |
24532 KB |
Output is correct |
92 |
Correct |
107 ms |
24512 KB |
Output is correct |
93 |
Correct |
100 ms |
24456 KB |
Output is correct |
94 |
Correct |
97 ms |
24512 KB |
Output is correct |
95 |
Correct |
97 ms |
24512 KB |
Output is correct |
96 |
Correct |
103 ms |
24456 KB |
Output is correct |
97 |
Correct |
95 ms |
24452 KB |
Output is correct |