#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int a, int b) {return a+rng()%(b-a+1);}
#define all(x) x.begin(),x.end()
map<int,vector<int>> mem;
vector<int> query(int i) {
if (mem.count(i)) return mem[i];
mem[i]=ask(i);
return mem[i];
}
struct BIT {
vector<int> t; int n;
BIT(int n) {
this->n = n;
t.assign(n,0);
}
void add(int i, int x) {
for(;i<n;i=i|(i+1)) t[i]+=x;
}
int sum(int r) {
int q=0; for(;r>=0;r=(r&(r+1))-1) q+=t[r]; return q;
}
};
int find_best(int n) {
BIT ft(n);
int f=-1;
map<int,int> was;
vector<pi> z;
const int T=1<<8;
vector<int> p; forn(i,n) p.pb(i);
vector<int> t;
vector<int> zzz;
for (int l=0; l<n; l+=T) {
int r=l+T-1; r=min(r,n-1);
vector<int> z;
for(auto&x:p) if (l<=x && x<=r) z.pb(x);
int Z;
int k=z.size();
for (int j=k-1; j>=0; --j) {
int x=z[j];
auto q=query(x);
if (q[0]+q[1]>f) {
f=q[0]+q[1];
for(auto&x:t) ft.add(x,1);
t.clear();
}
if (q[0]+q[1]==0) return x;
if (q[0]+q[1]<f) {
ft.add(x,1);
z.pop_back();
continue;
}
t.pb(x);
Z=q[0]-ft.sum(r); break;
}
forn(it,Z) {
int k=z.size();
int r=k-1;
int lq=0, rq=r;
while (lq<rq) {
int m=(lq+rq)>>1;
int x=z[m];
auto q=query(x);
if (q[0]+q[1]==0) return x;
if (q[0]+q[1]>f) {
f=q[0]+q[1];
for(auto&x:t) ft.add(x,1);
t.clear();
}
if (q[0]+q[1]<f) {
vector<int> b; forn(i,k) if (i!=m) b.pb(z[i]);
z=b;
ft.add(x,1);
break;
}
if (q[0]==ft.sum(x)) lq=m+1;
else rq=m;
t.pb(x);
}
if (z.size()<k) continue;
int x=z[r];
if (!mem.count(x)) zzz.pb(x);
else {
auto q=query(x);
if (q[0]+q[1]==0) return x;
}
vector<int> b; forn(i,k) if (i!=r) b.pb(z[i]);
z=b;
ft.add(x,1);
if (!z.size()) break;
}
}
shuffle(all(zzz),rng);
for(auto&x:zzz) {
auto q=query(x); if (q[0]+q[1]==0) return x;
}
}
Compilation message
prize.cpp: In function 'int find_best(int)':
prize.cpp:94:16: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
94 | if (z.size()<k) continue;
| ~~~~~~~~^~
prize.cpp:35:10: warning: control reaches end of non-void function [-Wreturn-type]
35 | BIT ft(n);
| ^
prize.cpp:4:32: warning: 'Z' may be used uninitialized in this function [-Wmaybe-uninitialized]
4 | #define forn(i,n) for(int i=0;i<n;++i)
| ^
prize.cpp:51:7: note: 'Z' was declared here
51 | int Z;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
2112 KB |
Output is correct |
2 |
Correct |
55 ms |
2204 KB |
Output is correct |
3 |
Correct |
76 ms |
2204 KB |
Output is correct |
4 |
Correct |
70 ms |
2112 KB |
Output is correct |
5 |
Correct |
38 ms |
2208 KB |
Output is correct |
6 |
Correct |
2 ms |
2248 KB |
Output is correct |
7 |
Correct |
63 ms |
2208 KB |
Output is correct |
8 |
Correct |
72 ms |
2208 KB |
Output is correct |
9 |
Correct |
45 ms |
2200 KB |
Output is correct |
10 |
Correct |
124 ms |
2248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2208 KB |
Output is correct |
2 |
Correct |
70 ms |
2204 KB |
Output is correct |
3 |
Correct |
73 ms |
2240 KB |
Output is correct |
4 |
Correct |
99 ms |
2248 KB |
Output is correct |
5 |
Correct |
38 ms |
2196 KB |
Output is correct |
6 |
Correct |
2 ms |
2208 KB |
Output is correct |
7 |
Correct |
60 ms |
2200 KB |
Output is correct |
8 |
Correct |
64 ms |
2212 KB |
Output is correct |
9 |
Correct |
42 ms |
2248 KB |
Output is correct |
10 |
Correct |
104 ms |
2248 KB |
Output is correct |
11 |
Correct |
46 ms |
2208 KB |
Output is correct |
12 |
Correct |
5 ms |
2248 KB |
Output is correct |
13 |
Correct |
104 ms |
2208 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
27 ms |
2208 KB |
Output is correct |
16 |
Correct |
136 ms |
2256 KB |
Output is correct |
17 |
Correct |
2 ms |
2184 KB |
Output is correct |
18 |
Correct |
117 ms |
2356 KB |
Output is correct |
19 |
Correct |
2 ms |
2200 KB |
Output is correct |
20 |
Correct |
17 ms |
1228 KB |
Output is correct |
21 |
Correct |
43 ms |
2200 KB |
Output is correct |
22 |
Correct |
10 ms |
2136 KB |
Output is correct |
23 |
Correct |
11 ms |
2208 KB |
Output is correct |
24 |
Correct |
2 ms |
2240 KB |
Output is correct |
25 |
Correct |
59 ms |
2200 KB |
Output is correct |
26 |
Correct |
54 ms |
2208 KB |
Output is correct |
27 |
Correct |
39 ms |
2196 KB |
Output is correct |
28 |
Correct |
120 ms |
2272 KB |
Output is correct |
29 |
Correct |
157 ms |
2248 KB |
Output is correct |
30 |
Correct |
204 ms |
2272 KB |
Output is correct |
31 |
Correct |
2 ms |
2208 KB |
Output is correct |
32 |
Correct |
84 ms |
2208 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
33 ms |
2196 KB |
Output is correct |
35 |
Correct |
48 ms |
2248 KB |
Output is correct |
36 |
Correct |
36 ms |
2248 KB |
Output is correct |
37 |
Correct |
16 ms |
2196 KB |
Output is correct |
38 |
Correct |
17 ms |
2200 KB |
Output is correct |
39 |
Correct |
54 ms |
2248 KB |
Output is correct |
40 |
Correct |
104 ms |
2196 KB |
Output is correct |
41 |
Correct |
76 ms |
2208 KB |
Output is correct |
42 |
Correct |
55 ms |
2236 KB |
Output is correct |
43 |
Correct |
60 ms |
2248 KB |
Output is correct |
44 |
Correct |
60 ms |
2248 KB |
Output is correct |
45 |
Correct |
39 ms |
2208 KB |
Output is correct |
46 |
Correct |
2 ms |
2248 KB |
Output is correct |
47 |
Correct |
47 ms |
2240 KB |
Output is correct |
48 |
Correct |
80 ms |
2200 KB |
Output is correct |
49 |
Correct |
8 ms |
2204 KB |
Output is correct |
50 |
Correct |
103 ms |
2204 KB |
Output is correct |
51 |
Correct |
48 ms |
2196 KB |
Output is correct |
52 |
Correct |
2 ms |
2208 KB |
Output is correct |
53 |
Correct |
69 ms |
2204 KB |
Output is correct |
54 |
Correct |
50 ms |
2204 KB |
Output is correct |
55 |
Correct |
2 ms |
2208 KB |
Output is correct |
56 |
Correct |
129 ms |
2268 KB |
Output is correct |
57 |
Correct |
92 ms |
2200 KB |
Output is correct |
58 |
Correct |
78 ms |
2204 KB |
Output is correct |
59 |
Correct |
68 ms |
2208 KB |
Output is correct |
60 |
Correct |
84 ms |
2208 KB |
Output is correct |
61 |
Correct |
74 ms |
2208 KB |
Output is correct |
62 |
Correct |
10 ms |
2196 KB |
Output is correct |
63 |
Correct |
66 ms |
2204 KB |
Output is correct |
64 |
Correct |
43 ms |
2200 KB |
Output is correct |
65 |
Correct |
100 ms |
2224 KB |
Output is correct |
66 |
Correct |
72 ms |
2240 KB |
Output is correct |
67 |
Correct |
79 ms |
2248 KB |
Output is correct |
68 |
Correct |
3 ms |
2200 KB |
Output is correct |
69 |
Correct |
111 ms |
2204 KB |
Output is correct |
70 |
Correct |
89 ms |
2200 KB |
Output is correct |
71 |
Correct |
110 ms |
2372 KB |
Output is correct |
72 |
Correct |
97 ms |
2248 KB |
Output is correct |
73 |
Correct |
128 ms |
2364 KB |
Output is correct |
74 |
Correct |
118 ms |
2372 KB |
Output is correct |
75 |
Correct |
100 ms |
2208 KB |
Output is correct |
76 |
Correct |
101 ms |
2300 KB |
Output is correct |
77 |
Correct |
122 ms |
2252 KB |
Output is correct |
78 |
Correct |
83 ms |
2248 KB |
Output is correct |
79 |
Correct |
79 ms |
2248 KB |
Output is correct |
80 |
Correct |
192 ms |
2296 KB |
Output is correct |
81 |
Correct |
95 ms |
2280 KB |
Output is correct |
82 |
Correct |
115 ms |
2248 KB |
Output is correct |
83 |
Correct |
85 ms |
2248 KB |
Output is correct |
84 |
Correct |
130 ms |
2272 KB |
Output is correct |
85 |
Correct |
174 ms |
2324 KB |
Output is correct |
86 |
Correct |
94 ms |
2248 KB |
Output is correct |
87 |
Correct |
88 ms |
2208 KB |
Output is correct |
88 |
Correct |
92 ms |
2208 KB |
Output is correct |
89 |
Correct |
97 ms |
2248 KB |
Output is correct |
90 |
Correct |
139 ms |
2248 KB |
Output is correct |
91 |
Correct |
93 ms |
2248 KB |
Output is correct |
92 |
Correct |
73 ms |
2248 KB |
Output is correct |
93 |
Correct |
67 ms |
2248 KB |
Output is correct |
94 |
Correct |
85 ms |
2248 KB |
Output is correct |
95 |
Correct |
101 ms |
2248 KB |
Output is correct |
96 |
Correct |
79 ms |
2248 KB |
Output is correct |
97 |
Correct |
92 ms |
2248 KB |
Output is correct |