//#define _CRT_SECURE_NO_WARNINGS
//#include "grader.cpp"
#include "xylophone.h"
using namespace std;
#include <vector>
#include <bitset>
#include <unordered_map>
typedef vector<int> vi;
typedef pair<int, int> pii;
struct pairhash
{
public:
template <typename T, typename U>
size_t operator()(const pair<T, U>& x) const
{
return hash<T>()(x.first) * 100003 ^ hash<U>()(x.second);
}
};
unordered_map<pii, int, pairhash> queries;
int ownQuery(int a, int b)
{
if (queries.find({ a, b }) != queries.end())
{
return queries[{a, b}];
}
return queries[{a, b}] = query(a, b);
}
void queryAndProcess(const int& i, const int& d, bool& canProgress, const int& N, bitset<5001>& notFound, vi& values, const bool& doExtra)
{
if (i + d > N || i + d <= 0) return;
if (values[i] == -1) return;
if (values[i + d] != -1) return;
int v = d > 0 ? ownQuery(i, i + d) : ownQuery(i + d, i);
int opt1 = values[i] + v;
int opt2 = values[i] - v;
int result = 0; // 1 = opt1, 2 = opt2, 0 is unknown
if (opt1 < 1) result = 2;
else if (opt2 < 1) result = 1;
else if (opt1 > N) result = 2;
else if (opt2 > N) result = 1;
else if (!notFound[opt1]) result = 2;
else if (!notFound[opt2]) result = 1;
if (result == 0)
{
if (!doExtra) return;
int v2 = d > 0 ? query(i - d, i + d) : query(i + d, i - d);
int ma = max(values[i - d], values[i]);
int mi = min(values[i - d], values[i]);
int v2Opt1 = max(opt1, ma) - min(opt1, mi);
int v2Opt2 = max(opt2, ma) - min(opt2, mi);
result = v2 == v2Opt1 ? 1 : 2;
}
if (result == 1)
{
values[i + d] = opt1;
notFound[opt1] = false;
}
else
{
values[i + d] = opt2;
notFound[opt2] = false;
}
canProgress = true;
}
void solve(int N)
{
int a = 1, b = N;
while (a < b)
{
int k = (a + b) / 2;
int value = ownQuery(1, k);
if (value == N - 1) b = k;
else a = k + 1;
}
int maxIndex = a;
a = 1, b = maxIndex - 1;
while (a < b)
{
int k = (a + b + 1) / 2;
int value = ownQuery(k, maxIndex);
if (value == N - 1) a = k;
else b = k - 1;
}
int oneIndex = a;
bitset<5001> notFound;
for (int i = 2; i < N; i++) notFound[i] = true;
vi values(N + 1, -1);
values[oneIndex] = 1;
values[maxIndex] = N;
bool canProgress = true;
while (canProgress)
{
canProgress = false;
for (int i = 1; i <= N; i++)
{
queryAndProcess(i, -1, canProgress, N, notFound, values, false);
queryAndProcess(i, 1, canProgress, N, notFound, values, false);
if (notFound.none()) break;
}
}
canProgress = !notFound.none();
while (canProgress)
{
canProgress = false;
for (int i = 1; i <= N; i++)
{
queryAndProcess(i, -1, canProgress, N, notFound, values, true);
queryAndProcess(i, 1, canProgress, N, notFound, values, true);
if (notFound.none()) break;
}
}
for (int i = 1; i <= N; i++)
{
if (values[i] == -1) throw;
answer(i, values[i]);
}
}
Compilation message
xylophone.cpp: In function 'void queryAndProcess(const int&, const int&, bool&, const int&, std::bitset<5001>&, vi&, const bool&)':
xylophone.cpp:54:7: warning: unused variable 'v2Opt2' [-Wunused-variable]
54 | int v2Opt2 = max(opt2, ma) - min(opt2, mi);
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
2 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
2 ms |
208 KB |
Output is correct |
8 |
Correct |
2 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
2 ms |
288 KB |
Output is correct |
12 |
Correct |
2 ms |
292 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Correct |
2 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
2 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
2 ms |
208 KB |
Output is correct |
8 |
Correct |
2 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
2 ms |
288 KB |
Output is correct |
12 |
Correct |
2 ms |
292 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Correct |
2 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
296 KB |
Output is correct |
17 |
Correct |
7 ms |
208 KB |
Output is correct |
18 |
Correct |
10 ms |
308 KB |
Output is correct |
19 |
Correct |
13 ms |
328 KB |
Output is correct |
20 |
Correct |
14 ms |
328 KB |
Output is correct |
21 |
Correct |
17 ms |
308 KB |
Output is correct |
22 |
Correct |
11 ms |
328 KB |
Output is correct |
23 |
Correct |
9 ms |
336 KB |
Output is correct |
24 |
Correct |
21 ms |
316 KB |
Output is correct |
25 |
Correct |
10 ms |
328 KB |
Output is correct |
26 |
Correct |
16 ms |
316 KB |
Output is correct |
27 |
Correct |
20 ms |
336 KB |
Output is correct |
28 |
Correct |
13 ms |
208 KB |
Output is correct |
29 |
Correct |
9 ms |
336 KB |
Output is correct |
30 |
Correct |
8 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
2 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
2 ms |
208 KB |
Output is correct |
8 |
Correct |
2 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
2 ms |
288 KB |
Output is correct |
12 |
Correct |
2 ms |
292 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Correct |
2 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
2 ms |
296 KB |
Output is correct |
17 |
Correct |
7 ms |
208 KB |
Output is correct |
18 |
Correct |
10 ms |
308 KB |
Output is correct |
19 |
Correct |
13 ms |
328 KB |
Output is correct |
20 |
Correct |
14 ms |
328 KB |
Output is correct |
21 |
Correct |
17 ms |
308 KB |
Output is correct |
22 |
Correct |
11 ms |
328 KB |
Output is correct |
23 |
Correct |
9 ms |
336 KB |
Output is correct |
24 |
Correct |
21 ms |
316 KB |
Output is correct |
25 |
Correct |
10 ms |
328 KB |
Output is correct |
26 |
Correct |
16 ms |
316 KB |
Output is correct |
27 |
Correct |
20 ms |
336 KB |
Output is correct |
28 |
Correct |
13 ms |
208 KB |
Output is correct |
29 |
Correct |
9 ms |
336 KB |
Output is correct |
30 |
Correct |
8 ms |
312 KB |
Output is correct |
31 |
Correct |
34 ms |
364 KB |
Output is correct |
32 |
Correct |
66 ms |
460 KB |
Output is correct |
33 |
Correct |
107 ms |
508 KB |
Output is correct |
34 |
Correct |
164 ms |
660 KB |
Output is correct |
35 |
Correct |
158 ms |
528 KB |
Output is correct |
36 |
Correct |
41 ms |
564 KB |
Output is correct |
37 |
Correct |
580 ms |
564 KB |
Output is correct |
38 |
Correct |
44 ms |
588 KB |
Output is correct |
39 |
Correct |
146 ms |
512 KB |
Output is correct |
40 |
Correct |
60 ms |
624 KB |
Output is correct |
41 |
Correct |
125 ms |
548 KB |
Output is correct |
42 |
Correct |
42 ms |
484 KB |
Output is correct |
43 |
Correct |
555 ms |
540 KB |
Output is correct |
44 |
Correct |
36 ms |
508 KB |
Output is correct |
45 |
Correct |
71 ms |
468 KB |
Output is correct |
46 |
Correct |
91 ms |
492 KB |
Output is correct |
47 |
Correct |
53 ms |
556 KB |
Output is correct |
48 |
Correct |
91 ms |
520 KB |
Output is correct |
49 |
Correct |
169 ms |
580 KB |
Output is correct |
50 |
Correct |
185 ms |
660 KB |
Output is correct |
51 |
Correct |
52 ms |
556 KB |
Output is correct |
52 |
Correct |
186 ms |
452 KB |
Output is correct |
53 |
Correct |
140 ms |
664 KB |
Output is correct |
54 |
Correct |
82 ms |
492 KB |
Output is correct |
55 |
Correct |
70 ms |
528 KB |
Output is correct |
56 |
Correct |
129 ms |
580 KB |
Output is correct |
57 |
Correct |
99 ms |
652 KB |
Output is correct |
58 |
Correct |
220 ms |
556 KB |
Output is correct |
59 |
Correct |
114 ms |
564 KB |
Output is correct |
60 |
Correct |
127 ms |
548 KB |
Output is correct |