#include <bits/stdc++.h>
//#include "prize.h"
#define MAX_N 200000
#define xx first
#define yy second
using namespace std;
typedef long long lint;
vector<int> ask(int i);
int N;
int mx;
int rez = -1;
int spec[MAX_N + 1];
int xd[MAX_N + 1];
pair <int, int> q[MAX_N + 1];
vector <int> tmp;
int ST, DR;
int aib[MAX_N + 1];
void baga(int poz, int nr)
{
while(poz <= N)
{
aib[poz] += nr;
poz += poz & (-poz);
}
}
int getSum(int poz)
{
int rez = 0;
while(poz > 0)
{
rez += aib[poz];
poz -= poz & (-poz);
}
return rez;
}
int getS(int st, int dr)
{
int rez = getSum(dr);
rez -= getSum(st - 1);
return rez;
}
int XX;
vector <int> doAsk(int poz)
{
static int CNT = 0;
if(rez != -1)
return {0, 0};
if(xd[poz] == 1)
{
tmp[0] = q[poz].xx;
tmp[1] = q[poz].yy;
return tmp;
}
xd[poz] = 1;
CNT ++;
assert(CNT <= 10000);
vector <int> a = ask(poz - 1);
q[poz] = {a[0], a[1]};
if(a[0] + a[1] > mx && XX)
while(1)cout << " PUSCA " << a[0] << " " << a[1] << " " << mx << "\n";
if(a[0] + a[1] == 0)
rez = poz;
else if(a[0] + a[1] < mx)
{
spec[poz] = 1;
baga(poz, 1);
if(a[0] == 0)
ST = max(ST, poz);
if(a[1] == 0)
DR = min(DR, poz);
}
return a;
}
bool verif(int poz, int &cst, int &cdr, int &px)
{
if(rez != -1)
return 0;
vector <int> a = doAsk(poz);
if(a[0] + a[1] == mx)
{
cst = a[0];
cdr = a[1];
px = poz;
return 1;
}
spec[poz] = 1;
return 0;
}
int lst, ldr;
void intreaba(int poz, int &cst, int &cdr, int &px)
{
int n = N;
if(rez != -1)
return;
if(verif(poz, cst, cdr, px))
return;
int st = poz - 1;
int dr = poz + 1;
while(st >= lst || dr <= ldr)
{
if(st >= lst)
{
if(verif(st, cst, cdr, px))
return;
st --;
}
if(dr <= ldr)
{
if(verif(dr, cst, cdr, px))
return;
dr ++;
}
}
}
void divide(int st, int dr, int pst, int pdr, int cate)
{
if(rez != -1)
return;
st = max(st, ST);
dr = min(dr, DR);
if(st > dr || cate == 0)
return;
int nou = cate - getS(st, dr);
if(nou <= 0)
return;
// cout << " SUNTEM " << st << " " << dr << " AVEM INSRE ST " << pst << " DR " << pdr << " " << cate << "\n";
if((dr - st + 1) - cate <= 1)
{
for(int i = st; i <= dr; i ++)
doAsk(i);
return;
}
int cst, cdr, px;
int mid = (st + dr) >> 1;
lst = st;
ldr = dr;
intreaba(mid, cst, cdr, px);
divide(st, px - 1, pst, cdr, cst - pst);
divide(px + 1, dr, cst, pdr, cdr - pdr);
}
int find_best(int n)
{
ST = 1;
DR = n;
N = n;
tmp.resize(2);
if(n <= 5000)
{
for(int i = 0; i < n; i ++)
{
vector<int> a = ask(i);
if(a[0] + a[1] == 0)
return i;
}
assert(1 == 0);
//return;
}
srand(time(0));
int rad = sqrt(n);
int change = 0;
for(int i = 1; i <= 480; i ++)
{
lint poz = i - 1;
vector <int> a = doAsk(poz + 1);
if(a[0] + a[1] == 0)
return poz;
if(a[0] + a[1] > mx)
change ++;
mx = max(mx, a[0] + a[1]);
if(mx >= rad)
break;
if(change == 4)
break;
}
XX = 1;
divide(1, n, 0, 0, mx);
return rez - 1;
}
Compilation message
prize.cpp: In function 'void intreaba(int, int&, int&, int&)':
prize.cpp:131:9: warning: unused variable 'n' [-Wunused-variable]
131 | int n = N;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
496 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
396 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
416 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
396 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
392 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
1016 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
11 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
912 KB |
Output is correct |
16 |
Correct |
43 ms |
3576 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
47 ms |
4272 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
8 ms |
1016 KB |
Output is correct |
21 |
Correct |
18 ms |
1912 KB |
Output is correct |
22 |
Correct |
12 ms |
760 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
392 KB |
Output is correct |
25 |
Correct |
13 ms |
2388 KB |
Output is correct |
26 |
Correct |
23 ms |
2424 KB |
Output is correct |
27 |
Correct |
6 ms |
616 KB |
Output is correct |
28 |
Correct |
40 ms |
3960 KB |
Output is correct |
29 |
Correct |
23 ms |
3512 KB |
Output is correct |
30 |
Correct |
39 ms |
4244 KB |
Output is correct |
31 |
Correct |
1 ms |
388 KB |
Output is correct |
32 |
Correct |
9 ms |
1400 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
13 ms |
1656 KB |
Output is correct |
35 |
Correct |
7 ms |
760 KB |
Output is correct |
36 |
Correct |
13 ms |
1280 KB |
Output is correct |
37 |
Correct |
4 ms |
668 KB |
Output is correct |
38 |
Correct |
6 ms |
376 KB |
Output is correct |
39 |
Correct |
23 ms |
2168 KB |
Output is correct |
40 |
Correct |
34 ms |
4244 KB |
Output is correct |
41 |
Correct |
25 ms |
2684 KB |
Output is correct |
42 |
Correct |
20 ms |
2552 KB |
Output is correct |
43 |
Correct |
22 ms |
2424 KB |
Output is correct |
44 |
Correct |
11 ms |
1944 KB |
Output is correct |
45 |
Correct |
17 ms |
1528 KB |
Output is correct |
46 |
Correct |
1 ms |
376 KB |
Output is correct |
47 |
Correct |
11 ms |
1952 KB |
Output is correct |
48 |
Correct |
35 ms |
3188 KB |
Output is correct |
49 |
Correct |
4 ms |
744 KB |
Output is correct |
50 |
Correct |
50 ms |
4220 KB |
Output is correct |
51 |
Correct |
22 ms |
1912 KB |
Output is correct |
52 |
Correct |
1 ms |
488 KB |
Output is correct |
53 |
Correct |
8 ms |
896 KB |
Output is correct |
54 |
Correct |
14 ms |
1988 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
38 ms |
4216 KB |
Output is correct |
57 |
Correct |
35 ms |
3064 KB |
Output is correct |
58 |
Correct |
27 ms |
3156 KB |
Output is correct |
59 |
Correct |
31 ms |
2552 KB |
Output is correct |
60 |
Correct |
25 ms |
2424 KB |
Output is correct |
61 |
Correct |
7 ms |
928 KB |
Output is correct |
62 |
Correct |
5 ms |
512 KB |
Output is correct |
63 |
Correct |
6 ms |
884 KB |
Output is correct |
64 |
Correct |
5 ms |
672 KB |
Output is correct |
65 |
Correct |
4 ms |
504 KB |
Output is correct |
66 |
Correct |
4 ms |
384 KB |
Output is correct |
67 |
Correct |
2 ms |
376 KB |
Output is correct |
68 |
Correct |
3 ms |
400 KB |
Output is correct |
69 |
Correct |
4 ms |
376 KB |
Output is correct |
70 |
Correct |
7 ms |
376 KB |
Output is correct |
71 |
Correct |
45 ms |
4216 KB |
Output is correct |
72 |
Correct |
12 ms |
1792 KB |
Output is correct |
73 |
Correct |
29 ms |
4320 KB |
Output is correct |
74 |
Correct |
44 ms |
4216 KB |
Output is correct |
75 |
Correct |
4 ms |
816 KB |
Output is correct |
76 |
Correct |
48 ms |
4216 KB |
Output is correct |
77 |
Correct |
32 ms |
4344 KB |
Output is correct |
78 |
Correct |
11 ms |
1792 KB |
Output is correct |
79 |
Correct |
29 ms |
4156 KB |
Output is correct |
80 |
Correct |
50 ms |
4216 KB |
Output is correct |
81 |
Correct |
52 ms |
4344 KB |
Output is correct |
82 |
Correct |
53 ms |
4216 KB |
Output is correct |
83 |
Correct |
7 ms |
896 KB |
Output is correct |
84 |
Correct |
47 ms |
4220 KB |
Output is correct |
85 |
Correct |
34 ms |
4228 KB |
Output is correct |
86 |
Correct |
12 ms |
512 KB |
Output is correct |
87 |
Correct |
7 ms |
512 KB |
Output is correct |
88 |
Correct |
11 ms |
504 KB |
Output is correct |
89 |
Correct |
6 ms |
512 KB |
Output is correct |
90 |
Correct |
5 ms |
512 KB |
Output is correct |
91 |
Correct |
7 ms |
512 KB |
Output is correct |
92 |
Correct |
5 ms |
512 KB |
Output is correct |
93 |
Correct |
9 ms |
1280 KB |
Output is correct |
94 |
Correct |
9 ms |
1408 KB |
Output is correct |
95 |
Correct |
8 ms |
1408 KB |
Output is correct |
96 |
Correct |
6 ms |
1280 KB |
Output is correct |
97 |
Correct |
5 ms |
384 KB |
Output is correct |