#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;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
2248 KB |
Output is correct |
2 |
Correct |
78 ms |
2248 KB |
Output is correct |
3 |
Correct |
63 ms |
2204 KB |
Output is correct |
4 |
Correct |
103 ms |
2208 KB |
Output is correct |
5 |
Correct |
63 ms |
2212 KB |
Output is correct |
6 |
Correct |
2 ms |
2208 KB |
Output is correct |
7 |
Correct |
57 ms |
2248 KB |
Output is correct |
8 |
Correct |
44 ms |
2248 KB |
Output is correct |
9 |
Correct |
44 ms |
2248 KB |
Output is correct |
10 |
Correct |
83 ms |
2248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
2248 KB |
Output is correct |
2 |
Correct |
49 ms |
2208 KB |
Output is correct |
3 |
Correct |
67 ms |
2248 KB |
Output is correct |
4 |
Correct |
92 ms |
2204 KB |
Output is correct |
5 |
Correct |
38 ms |
2248 KB |
Output is correct |
6 |
Correct |
2 ms |
2248 KB |
Output is correct |
7 |
Correct |
60 ms |
2212 KB |
Output is correct |
8 |
Correct |
59 ms |
2216 KB |
Output is correct |
9 |
Correct |
73 ms |
2204 KB |
Output is correct |
10 |
Correct |
81 ms |
2248 KB |
Output is correct |
11 |
Correct |
50 ms |
2240 KB |
Output is correct |
12 |
Correct |
4 ms |
2248 KB |
Output is correct |
13 |
Correct |
103 ms |
2248 KB |
Output is correct |
14 |
Correct |
3 ms |
592 KB |
Output is correct |
15 |
Correct |
23 ms |
2248 KB |
Output is correct |
16 |
Correct |
110 ms |
2248 KB |
Output is correct |
17 |
Correct |
2 ms |
2248 KB |
Output is correct |
18 |
Correct |
122 ms |
2284 KB |
Output is correct |
19 |
Correct |
2 ms |
2204 KB |
Output is correct |
20 |
Correct |
20 ms |
1320 KB |
Output is correct |
21 |
Correct |
52 ms |
2208 KB |
Output is correct |
22 |
Correct |
17 ms |
2204 KB |
Output is correct |
23 |
Correct |
19 ms |
2248 KB |
Output is correct |
24 |
Correct |
3 ms |
2208 KB |
Output is correct |
25 |
Correct |
58 ms |
2208 KB |
Output is correct |
26 |
Correct |
100 ms |
2248 KB |
Output is correct |
27 |
Correct |
27 ms |
2112 KB |
Output is correct |
28 |
Correct |
108 ms |
2248 KB |
Output is correct |
29 |
Correct |
126 ms |
2248 KB |
Output is correct |
30 |
Correct |
165 ms |
2272 KB |
Output is correct |
31 |
Correct |
2 ms |
2208 KB |
Output is correct |
32 |
Correct |
79 ms |
2248 KB |
Output is correct |
33 |
Correct |
1 ms |
208 KB |
Output is correct |
34 |
Correct |
42 ms |
2196 KB |
Output is correct |
35 |
Correct |
62 ms |
2248 KB |
Output is correct |
36 |
Correct |
25 ms |
2188 KB |
Output is correct |
37 |
Correct |
17 ms |
2212 KB |
Output is correct |
38 |
Correct |
21 ms |
2208 KB |
Output is correct |
39 |
Correct |
63 ms |
2204 KB |
Output is correct |
40 |
Correct |
168 ms |
2212 KB |
Output is correct |
41 |
Correct |
84 ms |
2204 KB |
Output is correct |
42 |
Correct |
94 ms |
2208 KB |
Output is correct |
43 |
Correct |
72 ms |
2212 KB |
Output is correct |
44 |
Correct |
38 ms |
2248 KB |
Output is correct |
45 |
Correct |
46 ms |
2208 KB |
Output is correct |
46 |
Correct |
2 ms |
2248 KB |
Output is correct |
47 |
Correct |
56 ms |
2204 KB |
Output is correct |
48 |
Correct |
116 ms |
2204 KB |
Output is correct |
49 |
Correct |
10 ms |
2204 KB |
Output is correct |
50 |
Correct |
118 ms |
2312 KB |
Output is correct |
51 |
Correct |
61 ms |
2204 KB |
Output is correct |
52 |
Correct |
2 ms |
2188 KB |
Output is correct |
53 |
Correct |
83 ms |
2204 KB |
Output is correct |
54 |
Correct |
70 ms |
2212 KB |
Output is correct |
55 |
Correct |
2 ms |
2204 KB |
Output is correct |
56 |
Correct |
121 ms |
2340 KB |
Output is correct |
57 |
Correct |
72 ms |
2208 KB |
Output is correct |
58 |
Correct |
89 ms |
2248 KB |
Output is correct |
59 |
Correct |
73 ms |
2204 KB |
Output is correct |
60 |
Correct |
69 ms |
2228 KB |
Output is correct |
61 |
Correct |
65 ms |
2248 KB |
Output is correct |
62 |
Correct |
11 ms |
2240 KB |
Output is correct |
63 |
Correct |
110 ms |
2248 KB |
Output is correct |
64 |
Correct |
82 ms |
2204 KB |
Output is correct |
65 |
Correct |
93 ms |
2204 KB |
Output is correct |
66 |
Correct |
106 ms |
2260 KB |
Output is correct |
67 |
Correct |
89 ms |
2248 KB |
Output is correct |
68 |
Correct |
2 ms |
2200 KB |
Output is correct |
69 |
Correct |
109 ms |
2208 KB |
Output is correct |
70 |
Correct |
111 ms |
2204 KB |
Output is correct |
71 |
Correct |
125 ms |
2268 KB |
Output is correct |
72 |
Correct |
137 ms |
2248 KB |
Output is correct |
73 |
Correct |
94 ms |
2364 KB |
Output is correct |
74 |
Correct |
106 ms |
2372 KB |
Output is correct |
75 |
Correct |
70 ms |
2248 KB |
Output is correct |
76 |
Correct |
133 ms |
2432 KB |
Output is correct |
77 |
Correct |
102 ms |
2304 KB |
Output is correct |
78 |
Correct |
73 ms |
2248 KB |
Output is correct |
79 |
Correct |
101 ms |
2248 KB |
Output is correct |
80 |
Correct |
99 ms |
2256 KB |
Output is correct |
81 |
Correct |
112 ms |
2268 KB |
Output is correct |
82 |
Correct |
164 ms |
2404 KB |
Output is correct |
83 |
Correct |
83 ms |
2248 KB |
Output is correct |
84 |
Correct |
116 ms |
2248 KB |
Output is correct |
85 |
Correct |
122 ms |
2264 KB |
Output is correct |
86 |
Correct |
71 ms |
2204 KB |
Output is correct |
87 |
Correct |
114 ms |
2248 KB |
Output is correct |
88 |
Correct |
75 ms |
2248 KB |
Output is correct |
89 |
Correct |
97 ms |
2248 KB |
Output is correct |
90 |
Correct |
132 ms |
2248 KB |
Output is correct |
91 |
Correct |
77 ms |
2248 KB |
Output is correct |
92 |
Correct |
112 ms |
2248 KB |
Output is correct |
93 |
Correct |
86 ms |
2248 KB |
Output is correct |
94 |
Correct |
81 ms |
2248 KB |
Output is correct |
95 |
Correct |
74 ms |
2248 KB |
Output is correct |
96 |
Correct |
98 ms |
2248 KB |
Output is correct |
97 |
Correct |
72 ms |
2248 KB |
Output is correct |