#include "prize.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second
const int INF = 1e9+1;
const int P = 1000000007;
const ll LLINF = (ll)1e18+1;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }
pii query(int i) {
static vector<pii> M = vector<pii>(202020, {-1, -1});
if(M[i].fi < 0 && M[i].se < 0) {
vector<int> res = ask(i);
return M[i] = {res[0], res[1]};
} else return M[i];
}
bool validate(int x, int s, int e, int ma, int &mi, pii &qmi, vector<pii> &R) {
if(x >= e || x < s) return false;
pii tmp = query(x);
if(tmp.fi+tmp.se != ma) {
R.push_back({x, tmp.fi+tmp.se});
return false;
} else {
mi = x; qmi = tmp;
return true;
}
}
vector<pii> solve(int s, int e, int cnt, int lcnt, int rcnt, int ma) {
//cout << s << " " << e << " " << cnt << " " << lcnt << " " << rcnt << " " << ma << "\n";
if(cnt == 0) return vector<pii>();
vector<pii> ret;
int cl = s+e>>1, cr = s+e>>1;
pii qmi; int mi;
while(true) {
if(validate(cr, s, e, ma, mi, qmi, ret)) break;
else cr++;
if(validate(cl-1, s, e, ma, mi, qmi, ret)) break;
else cl--;
if(cl < s && cr >= e) return ret;
}
qmi.fi -= lcnt, qmi.se -= rcnt;
if(cr == mi) qmi.fi -= cr-cl;
else qmi.se -= cr-cl;
//cout << mi << " " << cl << " " << cr << " " << qmi << "\n";
vector<pii> L = solve(s, cl, qmi.fi, lcnt, rcnt+cr-cl+qmi.se, ma);
vector<pii> R = solve(cr, e, qmi.se, lcnt+cr-cl+qmi.fi, rcnt, ma);
for(auto i : L) ret.push_back(i);
for(auto i : R) ret.push_back(i);
return ret;
}
int find_best(int n) {
int sq = (sqrt(n)+1)*2;
vector<int> X;
for(int i = 0; i <= n/sq; i++) X.push_back(sq*i);
if(X.back() != n) X.push_back(n);
//cout << X;
vector<int> Y(X);
int ma = 0;
vector<pii> Q, LR;
for(int i = 0; i+1 < X.size(); i++) {
pii tmp = query(X[i]);
Q.push_back(tmp);
LR.push_back({X[i], X[i]});
ma = max(tmp.fi+tmp.se, ma);
}
for(int i = 0; i+1 < X.size(); i++) {
for(int j = 1; j < 4; j++) {
int x = X[i]+sq/4*j;
if(x >= n) continue;
pii tmp = query(x);
ma = max(tmp.fi+tmp.se, ma);
}
}
//cout << ma << "\n";
//cout << Q;
vector<pii> ans;
for(int i = 0; i+1 < X.size(); i++) {
if(Q[i].fi+Q[i].se != ma) {
ans.push_back({X[i], Q[i].fi+Q[i].se});
int s = (i == 0 ? 0 : Y[i-1]), e = Y[i+1];
while(LR[i].fi >= s && LR[i].se < e) {
if(validate(LR[i].se, s, e, ma, X[i], Q[i], ans)) break;
else LR[i].se++;
if(validate(LR[i].fi-1, s, e, ma, X[i], Q[i], ans)) break;
else LR[i].fi--;
}
LR[i].fi = max(s, LR[i].fi);
LR[i].se = min(e, LR[i].se);
}
}
Q.push_back({ma, 0});
LR.push_back({n, n});
//cout << ans;
//cout << LR;
for(int i = 1; i < Q.size(); i++) {
int lcnt = Q[i-1].fi, rcnt = Q[i].se;
if(X[i-1] != LR[i-1].se) lcnt += LR[i-1].se-LR[i-1].fi;
if(X[i] == LR[i].se) rcnt += LR[i].se-LR[i].fi;
//cout << LR[i-1].se << " " << LR[i].fi << " " << lcnt << " " << rcnt << "\n";
vector<pii> tmp = solve(LR[i-1].se, LR[i].fi, ma-lcnt-rcnt, lcnt, rcnt, ma);
for(auto i : tmp) ans.push_back(i);
}
for(auto i : ans) if(i.se == 0) return i.fi;
exit(EXIT_FAILURE);
}
Compilation message
prize.cpp: In function 'std::vector<std::pair<int, int> > solve(int, int, int, int, int, int)':
prize.cpp:43:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int cl = s+e>>1, cr = s+e>>1;
| ~^~
prize.cpp:43:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int cl = s+e>>1, cr = s+e>>1;
| ~^~
prize.cpp: In function 'int find_best(int)':
prize.cpp:72:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i+1 < X.size(); i++) {
| ~~~~^~~~~~~~~~
prize.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i+1 < X.size(); i++) {
| ~~~~^~~~~~~~~~
prize.cpp:89:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i = 0; i+1 < X.size(); i++) {
| ~~~~^~~~~~~~~~
prize.cpp:107:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i = 1; i < Q.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1872 KB |
Output is correct |
2 |
Correct |
4 ms |
1904 KB |
Output is correct |
3 |
Correct |
7 ms |
1824 KB |
Output is correct |
4 |
Correct |
8 ms |
1872 KB |
Output is correct |
5 |
Correct |
7 ms |
1832 KB |
Output is correct |
6 |
Correct |
8 ms |
1896 KB |
Output is correct |
7 |
Correct |
7 ms |
1872 KB |
Output is correct |
8 |
Correct |
7 ms |
1828 KB |
Output is correct |
9 |
Correct |
6 ms |
1872 KB |
Output is correct |
10 |
Correct |
6 ms |
1872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1824 KB |
Output is correct |
2 |
Correct |
7 ms |
1872 KB |
Output is correct |
3 |
Correct |
7 ms |
1828 KB |
Output is correct |
4 |
Correct |
7 ms |
1828 KB |
Output is correct |
5 |
Correct |
7 ms |
1828 KB |
Output is correct |
6 |
Correct |
7 ms |
1828 KB |
Output is correct |
7 |
Correct |
7 ms |
1816 KB |
Output is correct |
8 |
Correct |
8 ms |
1872 KB |
Output is correct |
9 |
Correct |
4 ms |
1896 KB |
Output is correct |
10 |
Correct |
9 ms |
1872 KB |
Output is correct |
11 |
Correct |
8 ms |
1816 KB |
Output is correct |
12 |
Correct |
12 ms |
1824 KB |
Output is correct |
13 |
Correct |
9 ms |
1832 KB |
Output is correct |
14 |
Correct |
9 ms |
1896 KB |
Output is correct |
15 |
Correct |
32 ms |
1864 KB |
Output is correct |
16 |
Correct |
22 ms |
1896 KB |
Output is correct |
17 |
Correct |
21 ms |
1896 KB |
Output is correct |
18 |
Correct |
37 ms |
1880 KB |
Output is correct |
19 |
Correct |
40 ms |
1892 KB |
Output is correct |
20 |
Correct |
21 ms |
1996 KB |
Output is correct |
21 |
Correct |
24 ms |
1888 KB |
Output is correct |
22 |
Correct |
26 ms |
1884 KB |
Output is correct |
23 |
Correct |
11 ms |
1872 KB |
Output is correct |
24 |
Correct |
8 ms |
1872 KB |
Output is correct |
25 |
Correct |
16 ms |
1908 KB |
Output is correct |
26 |
Correct |
29 ms |
1828 KB |
Output is correct |
27 |
Correct |
8 ms |
1832 KB |
Output is correct |
28 |
Correct |
38 ms |
1896 KB |
Output is correct |
29 |
Correct |
31 ms |
1872 KB |
Output is correct |
30 |
Correct |
42 ms |
1888 KB |
Output is correct |
31 |
Correct |
36 ms |
1884 KB |
Output is correct |
32 |
Correct |
9 ms |
1820 KB |
Output is correct |
33 |
Correct |
1 ms |
1872 KB |
Output is correct |
34 |
Correct |
40 ms |
1872 KB |
Output is correct |
35 |
Correct |
6 ms |
1824 KB |
Output is correct |
36 |
Correct |
32 ms |
1872 KB |
Output is correct |
37 |
Correct |
11 ms |
1872 KB |
Output is correct |
38 |
Correct |
5 ms |
1896 KB |
Output is correct |
39 |
Correct |
45 ms |
1872 KB |
Output is correct |
40 |
Correct |
37 ms |
1900 KB |
Output is correct |
41 |
Correct |
32 ms |
1888 KB |
Output is correct |
42 |
Correct |
45 ms |
1884 KB |
Output is correct |
43 |
Correct |
17 ms |
1948 KB |
Output is correct |
44 |
Correct |
43 ms |
1888 KB |
Output is correct |
45 |
Correct |
36 ms |
1948 KB |
Output is correct |
46 |
Correct |
36 ms |
1872 KB |
Output is correct |
47 |
Correct |
33 ms |
1884 KB |
Output is correct |
48 |
Correct |
34 ms |
1824 KB |
Output is correct |
49 |
Correct |
31 ms |
1824 KB |
Output is correct |
50 |
Correct |
36 ms |
1892 KB |
Output is correct |
51 |
Correct |
18 ms |
1948 KB |
Output is correct |
52 |
Correct |
40 ms |
1888 KB |
Output is correct |
53 |
Correct |
10 ms |
1872 KB |
Output is correct |
54 |
Correct |
27 ms |
1936 KB |
Output is correct |
55 |
Correct |
18 ms |
1948 KB |
Output is correct |
56 |
Correct |
21 ms |
1900 KB |
Output is correct |
57 |
Correct |
18 ms |
1888 KB |
Output is correct |
58 |
Correct |
37 ms |
1884 KB |
Output is correct |
59 |
Correct |
41 ms |
1904 KB |
Output is correct |
60 |
Correct |
42 ms |
1892 KB |
Output is correct |
61 |
Correct |
8 ms |
1824 KB |
Output is correct |
62 |
Correct |
5 ms |
1884 KB |
Output is correct |
63 |
Correct |
5 ms |
1896 KB |
Output is correct |
64 |
Correct |
10 ms |
1820 KB |
Output is correct |
65 |
Correct |
9 ms |
1872 KB |
Output is correct |
66 |
Correct |
12 ms |
1872 KB |
Output is correct |
67 |
Correct |
11 ms |
1872 KB |
Output is correct |
68 |
Correct |
6 ms |
1912 KB |
Output is correct |
69 |
Correct |
16 ms |
1828 KB |
Output is correct |
70 |
Correct |
10 ms |
1828 KB |
Output is correct |
71 |
Correct |
32 ms |
1884 KB |
Output is correct |
72 |
Correct |
9 ms |
1872 KB |
Output is correct |
73 |
Correct |
29 ms |
1892 KB |
Output is correct |
74 |
Correct |
44 ms |
1872 KB |
Output is correct |
75 |
Correct |
10 ms |
1872 KB |
Output is correct |
76 |
Correct |
31 ms |
1872 KB |
Output is correct |
77 |
Correct |
41 ms |
1888 KB |
Output is correct |
78 |
Correct |
11 ms |
1872 KB |
Output is correct |
79 |
Correct |
26 ms |
1824 KB |
Output is correct |
80 |
Correct |
44 ms |
1892 KB |
Output is correct |
81 |
Correct |
32 ms |
1884 KB |
Output is correct |
82 |
Correct |
33 ms |
1872 KB |
Output is correct |
83 |
Correct |
12 ms |
1872 KB |
Output is correct |
84 |
Correct |
39 ms |
1904 KB |
Output is correct |
85 |
Correct |
40 ms |
1900 KB |
Output is correct |
86 |
Correct |
7 ms |
1904 KB |
Output is correct |
87 |
Correct |
11 ms |
1872 KB |
Output is correct |
88 |
Correct |
14 ms |
1880 KB |
Output is correct |
89 |
Correct |
11 ms |
1872 KB |
Output is correct |
90 |
Correct |
10 ms |
1872 KB |
Output is correct |
91 |
Correct |
14 ms |
1872 KB |
Output is correct |
92 |
Correct |
11 ms |
1872 KB |
Output is correct |
93 |
Correct |
14 ms |
1908 KB |
Output is correct |
94 |
Correct |
15 ms |
1872 KB |
Output is correct |
95 |
Correct |
12 ms |
1904 KB |
Output is correct |
96 |
Correct |
14 ms |
1884 KB |
Output is correct |
97 |
Correct |
17 ms |
1872 KB |
Output is correct |