#include "prize.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma optimize
using namespace std;
const int M = 2e5 + 10;
vector<int> r;
pair<int, int> A[M];
tuple<int, int, int, int> Q[M << 2];
inline pair<int, int> doAsk(int i) {
if (A[i].first != -1) {
return A[i];
}
r = ask(i);
return A[i] = { r[0], r[1] };
}
int find_best(int n) {
for (int i = 0; i < n; i++) {
A[i].first = -1;
}
vector<int> I(n), J;
for (int i = 0; i < n; i++) {
I[i] = i;
}
while (I.size() != 1) {
int x = 0, p = 0;
Q[p++] = { 0, 0, 0, (int)I.size() - 1 };
for (int j = 0; j < min((int)I.size(), (int)sqrt(I.size()) + 26); j++) {
auto [l, r, lo, hi] = Q[--p];
auto [a, b] = doAsk(I[lo + hi >> 1]);
x = max(x, a + b);
Q[p++] = { 0, 0, lo, lo + hi >> 1 };
Q[p++] = { 0, 0, 1 + (lo + hi >> 1), hi };
}
p = 0;
Q[p++] = { 0, 0, 0, (int)I.size() - 1 };
while (p != 0) {
auto [l, r, lo, hi] = Q[--p];
if (lo == hi) {
J.push_back(I[lo]);
continue;
}
int m = lo + hi >> 1;
auto [vl, vr] = doAsk(I[m]);
int c = 0, off = 0, ll = m, rr = m;
while (vl + vr != x) {
J.push_back(I[m + off]);
c += 1;
if (off <= 0) {
off = -off + 1;
rr = m + off;
}
else {
off = -off;
ll = m + off;
}
if (m + off < lo || m + off > hi) {
break;
}
auto [nl, nr] = doAsk(I[m + off]);
vl = nl; vr = nr;
}
if (m + off < lo || m + off > hi) {
continue;
}
if (off <= 0) {
if (l + vr != x) Q[p++] = { l, vr, lo, m + off };
if (vl + c + r != x) Q[p++] = { vl + c, r, m - off + 1, hi };
}
else {
if (l + vr + c != x) Q[p++] = { l, vr + c, lo, m - off };
if (vl + r != x) Q[p++] = { vl, r, m + off, hi };
}
}
I = J; J.clear();
sort(I.begin(), I.end());
}
return I[0];
}
Compilation message
prize.cpp:7: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
7 | #pragma optimize
|
prize.cpp: In function 'int find_best(int)':
prize.cpp:38:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | auto [a, b] = doAsk(I[lo + hi >> 1]);
| ~~~^~~~
prize.cpp:41:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | Q[p++] = { 0, 0, lo, lo + hi >> 1 };
| ~~~^~~~
prize.cpp:42:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | Q[p++] = { 0, 0, 1 + (lo + hi >> 1), hi };
| ~~~^~~~
prize.cpp:54:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int m = lo + hi >> 1;
| ~~~^~~~
prize.cpp:57:24: warning: variable 'll' set but not used [-Wunused-but-set-variable]
57 | int c = 0, off = 0, ll = m, rr = m;
| ^~
prize.cpp:57:32: warning: variable 'rr' set but not used [-Wunused-but-set-variable]
57 | int c = 0, off = 0, ll = m, rr = m;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2608 KB |
Output is correct |
2 |
Correct |
2 ms |
2596 KB |
Output is correct |
3 |
Correct |
2 ms |
2604 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2640 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2596 KB |
Output is correct |
9 |
Correct |
2 ms |
2608 KB |
Output is correct |
10 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2656 KB |
Output is correct |
2 |
Correct |
3 ms |
2608 KB |
Output is correct |
3 |
Correct |
3 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
2 ms |
2584 KB |
Output is correct |
6 |
Correct |
2 ms |
2596 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
2 ms |
2600 KB |
Output is correct |
9 |
Correct |
2 ms |
2604 KB |
Output is correct |
10 |
Correct |
2 ms |
2612 KB |
Output is correct |
11 |
Correct |
6 ms |
2592 KB |
Output is correct |
12 |
Correct |
6 ms |
2640 KB |
Output is correct |
13 |
Correct |
3 ms |
2648 KB |
Output is correct |
14 |
Correct |
12 ms |
464 KB |
Output is correct |
15 |
Correct |
35 ms |
2656 KB |
Output is correct |
16 |
Correct |
38 ms |
2660 KB |
Output is correct |
17 |
Correct |
29 ms |
2640 KB |
Output is correct |
18 |
Correct |
37 ms |
2596 KB |
Output is correct |
19 |
Correct |
39 ms |
2656 KB |
Output is correct |
20 |
Correct |
22 ms |
1448 KB |
Output is correct |
21 |
Correct |
39 ms |
2640 KB |
Output is correct |
22 |
Correct |
29 ms |
2604 KB |
Output is correct |
23 |
Correct |
3 ms |
2612 KB |
Output is correct |
24 |
Correct |
5 ms |
2632 KB |
Output is correct |
25 |
Correct |
31 ms |
2672 KB |
Output is correct |
26 |
Correct |
30 ms |
2596 KB |
Output is correct |
27 |
Correct |
2 ms |
2632 KB |
Output is correct |
28 |
Correct |
29 ms |
2600 KB |
Output is correct |
29 |
Correct |
31 ms |
2596 KB |
Output is correct |
30 |
Correct |
19 ms |
2660 KB |
Output is correct |
31 |
Correct |
31 ms |
2632 KB |
Output is correct |
32 |
Correct |
5 ms |
2664 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
34 ms |
2592 KB |
Output is correct |
35 |
Correct |
3 ms |
2640 KB |
Output is correct |
36 |
Correct |
35 ms |
2656 KB |
Output is correct |
37 |
Correct |
5 ms |
2604 KB |
Output is correct |
38 |
Correct |
3 ms |
2608 KB |
Output is correct |
39 |
Correct |
42 ms |
2640 KB |
Output is correct |
40 |
Correct |
24 ms |
2584 KB |
Output is correct |
41 |
Correct |
35 ms |
2656 KB |
Output is correct |
42 |
Correct |
34 ms |
2640 KB |
Output is correct |
43 |
Correct |
26 ms |
2632 KB |
Output is correct |
44 |
Correct |
17 ms |
2672 KB |
Output is correct |
45 |
Correct |
32 ms |
2600 KB |
Output is correct |
46 |
Correct |
37 ms |
2660 KB |
Output is correct |
47 |
Correct |
32 ms |
2656 KB |
Output is correct |
48 |
Correct |
34 ms |
2656 KB |
Output is correct |
49 |
Correct |
36 ms |
2728 KB |
Output is correct |
50 |
Correct |
38 ms |
2640 KB |
Output is correct |
51 |
Correct |
34 ms |
2640 KB |
Output is correct |
52 |
Correct |
31 ms |
2656 KB |
Output is correct |
53 |
Correct |
2 ms |
2672 KB |
Output is correct |
54 |
Correct |
26 ms |
2644 KB |
Output is correct |
55 |
Correct |
33 ms |
2672 KB |
Output is correct |
56 |
Correct |
36 ms |
2660 KB |
Output is correct |
57 |
Correct |
33 ms |
2656 KB |
Output is correct |
58 |
Correct |
34 ms |
2652 KB |
Output is correct |
59 |
Correct |
37 ms |
2604 KB |
Output is correct |
60 |
Correct |
46 ms |
2640 KB |
Output is correct |
61 |
Correct |
3 ms |
2640 KB |
Output is correct |
62 |
Correct |
2 ms |
2664 KB |
Output is correct |
63 |
Correct |
2 ms |
2668 KB |
Output is correct |
64 |
Correct |
2 ms |
2600 KB |
Output is correct |
65 |
Correct |
2 ms |
2632 KB |
Output is correct |
66 |
Correct |
6 ms |
2600 KB |
Output is correct |
67 |
Correct |
5 ms |
2640 KB |
Output is correct |
68 |
Correct |
5 ms |
2604 KB |
Output is correct |
69 |
Correct |
5 ms |
2600 KB |
Output is correct |
70 |
Correct |
4 ms |
2676 KB |
Output is correct |
71 |
Correct |
43 ms |
2640 KB |
Output is correct |
72 |
Correct |
5 ms |
2640 KB |
Output is correct |
73 |
Correct |
38 ms |
2640 KB |
Output is correct |
74 |
Correct |
40 ms |
2672 KB |
Output is correct |
75 |
Correct |
2 ms |
2672 KB |
Output is correct |
76 |
Correct |
35 ms |
2640 KB |
Output is correct |
77 |
Correct |
37 ms |
2640 KB |
Output is correct |
78 |
Correct |
5 ms |
2672 KB |
Output is correct |
79 |
Correct |
16 ms |
2660 KB |
Output is correct |
80 |
Correct |
33 ms |
2668 KB |
Output is correct |
81 |
Correct |
34 ms |
2640 KB |
Output is correct |
82 |
Correct |
35 ms |
2640 KB |
Output is correct |
83 |
Correct |
2 ms |
2640 KB |
Output is correct |
84 |
Correct |
36 ms |
2604 KB |
Output is correct |
85 |
Correct |
31 ms |
2604 KB |
Output is correct |
86 |
Correct |
6 ms |
2640 KB |
Output is correct |
87 |
Correct |
3 ms |
2640 KB |
Output is correct |
88 |
Correct |
4 ms |
2676 KB |
Output is correct |
89 |
Correct |
6 ms |
2604 KB |
Output is correct |
90 |
Correct |
3 ms |
2640 KB |
Output is correct |
91 |
Correct |
5 ms |
2640 KB |
Output is correct |
92 |
Correct |
6 ms |
2640 KB |
Output is correct |
93 |
Correct |
5 ms |
2668 KB |
Output is correct |
94 |
Correct |
4 ms |
2640 KB |
Output is correct |
95 |
Correct |
4 ms |
2640 KB |
Output is correct |
96 |
Correct |
4 ms |
2640 KB |
Output is correct |
97 |
Correct |
2 ms |
2640 KB |
Output is correct |