#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pll pair<ll, int>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 3e5+5;
const int M = 8;
const int mod = 998244353;
const int base = 350;
const ll inf = 1e18+7;
int m, n, k, t, c[N], d[N], dp[N][3], L, R, in[N], out[N], pos[N];
ll ans, tong, b[N], a[N];
vector<int> adj[N];
string s;
int r, sz, ntime;
struct BIT
{
int n;
vector<int> fe;
BIT(){}
void init(int _n)
{
n = _n;
fe.assign(n+1, 0);
}
void add(int id, int x)
{
++id;
for(; id <= n; id += id & -id)fe[id] += x;
}
int get(int id)
{
++id;
id = min(id, n);
if(id <= 0)return 0;
int total = 0;
for(; id; id -= id & -id)total += fe[id];
return total;
}
int get(int l, int r)
{
return get(r) - get(l-1);
}
}bit[3];
void dfs(int u, int p = 0)
{
int mx = 0;
d[u] = 1;
for(int v: adj[u])
{
if(v == p || b[v])continue;
dfs(v, u);
d[u] += d[v];
mx = max(d[v], mx);
}
mx = max(mx, sz-d[u]);
if(mx*2 <= sz)r = u;
}
void cal(int u, int p = 0)
{
if(L <= c[u] && c[u] <= R)++ans;
in[u] = ++ntime;
pos[ntime] = u;
for(int v: adj[u])
{
if(v == p || b[v])continue;
c[v] = c[u];
if(p && a[u] > a[p] && a[u] > a[v])++c[v];
cal(v, u);
}
out[u] = ntime;
}
void decom(int u)
{
//cout << u << endl;
int all = sz;
b[u] = 1;
ntime = 0;
c[u] = 0;
cal(u);
//cout << u << '\n';
//for(int j = in[u]; j <= out[u]; j ++)cout << pos[j] <<" "<< c[pos[j]] << '\n';
for(int v: adj[u])
{
if(b[v])continue;
if(a[v] < a[u])
{
for(int j = in[v]; j <= out[v]; j ++)
{
int x = pos[j];
ans += bit[0].get(L-c[x], R-c[x]);
ans += bit[1].get(L-c[x]-1, R-c[x]-1);
}
for(int j = in[v]; j <= out[v]; j ++)
{
int x = pos[j];
bit[1].add(c[x], 1);
}
}
else
{
for(int j = in[v]; j <= out[v]; j ++)
{
int x = pos[j];
ans += bit[0].get(L-c[x], R-c[x]);
ans += bit[1].get(L-c[x], R-c[x]);
}
for(int j = in[v]; j <= out[v]; j ++)
{
int x = pos[j];
bit[0].add(c[x], 1);
}
}
}
for(int v: adj[u])
{
if(b[v])continue;
if(a[v] < a[u])
{
for(int j = in[v]; j <= out[v]; j ++)
{
int x = pos[j];
bit[1].add(c[x], -1);
}
}
else
{
for(int j = in[v]; j <= out[v]; j ++)
{
int x = pos[j];
bit[0].add(c[x], -1);
}
}
}
for(int v: adj[u])
{
if(b[v])continue;
sz = d[v] > d[u] ? all-d[u] : d[v];
dfs(v);
decom(r);
}
}
void sol()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)cin >> a[i];
for(int i = 1; i <= n; i ++)cin >> b[i];
ll l = 1, r = inf, mid;
while(l <= r)
{
mid = (l+r)>>1;
ll need = 0, have = 0;
for(int i = 1; i <= n; i ++)
{
if(a[i] >= b[i])
{
if(a[i]*m >= mid)have += m-(mid+a[i]-1)/a[i];
else need += (mid-a[i]*m+b[i]-1)/b[i];
}
else
{
if(b[i]*m >= mid)have += m-(mid+b[i]-1)/b[i];
else need += (mid-b[i]*m+b[i]-1)/b[i];
}
if(need > 1ll*n*m)break;
}
if(have >= need)l = mid+1;
else r = mid-1;
}
cout << r;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:184:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
184 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:185:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
185 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
3 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
106 ms |
12028 KB |
Output is correct |
12 |
Correct |
111 ms |
11996 KB |
Output is correct |
13 |
Correct |
99 ms |
12012 KB |
Output is correct |
14 |
Correct |
75 ms |
11972 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
34 ms |
12024 KB |
Output is correct |
17 |
Correct |
121 ms |
11960 KB |
Output is correct |
18 |
Correct |
98 ms |
11972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
34 ms |
12024 KB |
Output is correct |
3 |
Correct |
121 ms |
11960 KB |
Output is correct |
4 |
Correct |
98 ms |
11972 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7360 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
5 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7372 KB |
Output is correct |
13 |
Correct |
51 ms |
9648 KB |
Output is correct |
14 |
Correct |
37 ms |
8844 KB |
Output is correct |
15 |
Correct |
27 ms |
8460 KB |
Output is correct |
16 |
Correct |
25 ms |
8320 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7372 KB |
Output is correct |
19 |
Correct |
4 ms |
7372 KB |
Output is correct |
20 |
Correct |
4 ms |
7372 KB |
Output is correct |
21 |
Correct |
4 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7372 KB |
Output is correct |
23 |
Correct |
6 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
3 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
106 ms |
12028 KB |
Output is correct |
12 |
Correct |
111 ms |
11996 KB |
Output is correct |
13 |
Correct |
99 ms |
12012 KB |
Output is correct |
14 |
Correct |
75 ms |
11972 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
4 ms |
7360 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7372 KB |
Output is correct |
19 |
Correct |
4 ms |
7372 KB |
Output is correct |
20 |
Correct |
4 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7372 KB |
Output is correct |
23 |
Correct |
51 ms |
9648 KB |
Output is correct |
24 |
Correct |
37 ms |
8844 KB |
Output is correct |
25 |
Correct |
27 ms |
8460 KB |
Output is correct |
26 |
Correct |
25 ms |
8320 KB |
Output is correct |
27 |
Correct |
4 ms |
7372 KB |
Output is correct |
28 |
Correct |
4 ms |
7372 KB |
Output is correct |
29 |
Correct |
4 ms |
7372 KB |
Output is correct |
30 |
Correct |
4 ms |
7372 KB |
Output is correct |
31 |
Correct |
4 ms |
7372 KB |
Output is correct |
32 |
Correct |
4 ms |
7372 KB |
Output is correct |
33 |
Correct |
6 ms |
7372 KB |
Output is correct |
34 |
Correct |
4 ms |
7372 KB |
Output is correct |
35 |
Correct |
34 ms |
12024 KB |
Output is correct |
36 |
Correct |
121 ms |
11960 KB |
Output is correct |
37 |
Correct |
98 ms |
11972 KB |
Output is correct |
38 |
Correct |
4 ms |
7372 KB |
Output is correct |
39 |
Correct |
4 ms |
7372 KB |
Output is correct |
40 |
Correct |
4 ms |
7372 KB |
Output is correct |
41 |
Correct |
4 ms |
7372 KB |
Output is correct |
42 |
Correct |
4 ms |
7320 KB |
Output is correct |
43 |
Correct |
4 ms |
7296 KB |
Output is correct |
44 |
Correct |
4 ms |
7372 KB |
Output is correct |
45 |
Correct |
6 ms |
7372 KB |
Output is correct |
46 |
Correct |
7 ms |
7520 KB |
Output is correct |
47 |
Correct |
4 ms |
7372 KB |
Output is correct |
48 |
Correct |
23 ms |
8180 KB |
Output is correct |
49 |
Correct |
4 ms |
7372 KB |
Output is correct |
50 |
Correct |
4 ms |
7372 KB |
Output is correct |
51 |
Correct |
6 ms |
7372 KB |
Output is correct |
52 |
Correct |
5 ms |
7372 KB |
Output is correct |
53 |
Correct |
4 ms |
7372 KB |
Output is correct |
54 |
Correct |
4 ms |
7372 KB |
Output is correct |
55 |
Correct |
67 ms |
9680 KB |
Output is correct |
56 |
Correct |
38 ms |
8908 KB |
Output is correct |
57 |
Correct |
30 ms |
8860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7360 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
51 ms |
9648 KB |
Output is correct |
10 |
Correct |
37 ms |
8844 KB |
Output is correct |
11 |
Correct |
27 ms |
8460 KB |
Output is correct |
12 |
Correct |
25 ms |
8320 KB |
Output is correct |
13 |
Correct |
4 ms |
7372 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
4 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7372 KB |
Output is correct |
19 |
Correct |
6 ms |
7372 KB |
Output is correct |
20 |
Correct |
4 ms |
7372 KB |
Output is correct |
21 |
Correct |
34 ms |
12024 KB |
Output is correct |
22 |
Correct |
121 ms |
11960 KB |
Output is correct |
23 |
Correct |
98 ms |
11972 KB |
Output is correct |
24 |
Correct |
33 ms |
8012 KB |
Output is correct |
25 |
Correct |
74 ms |
11984 KB |
Output is correct |
26 |
Correct |
16 ms |
8444 KB |
Output is correct |
27 |
Correct |
101 ms |
17652 KB |
Output is correct |
28 |
Correct |
108 ms |
17648 KB |
Output is correct |
29 |
Correct |
109 ms |
17628 KB |
Output is correct |
30 |
Correct |
155 ms |
17736 KB |
Output is correct |
31 |
Correct |
209 ms |
17732 KB |
Output is correct |
32 |
Correct |
111 ms |
13220 KB |
Output is correct |
33 |
Correct |
253 ms |
17740 KB |
Output is correct |
34 |
Correct |
107 ms |
13200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
3 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
106 ms |
12028 KB |
Output is correct |
12 |
Correct |
111 ms |
11996 KB |
Output is correct |
13 |
Correct |
99 ms |
12012 KB |
Output is correct |
14 |
Correct |
75 ms |
11972 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
4 ms |
7360 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7372 KB |
Output is correct |
19 |
Correct |
4 ms |
7372 KB |
Output is correct |
20 |
Correct |
4 ms |
7372 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
4 ms |
7372 KB |
Output is correct |
23 |
Correct |
51 ms |
9648 KB |
Output is correct |
24 |
Correct |
37 ms |
8844 KB |
Output is correct |
25 |
Correct |
27 ms |
8460 KB |
Output is correct |
26 |
Correct |
25 ms |
8320 KB |
Output is correct |
27 |
Correct |
4 ms |
7372 KB |
Output is correct |
28 |
Correct |
4 ms |
7372 KB |
Output is correct |
29 |
Correct |
4 ms |
7372 KB |
Output is correct |
30 |
Correct |
4 ms |
7372 KB |
Output is correct |
31 |
Correct |
4 ms |
7372 KB |
Output is correct |
32 |
Correct |
4 ms |
7372 KB |
Output is correct |
33 |
Correct |
6 ms |
7372 KB |
Output is correct |
34 |
Correct |
4 ms |
7372 KB |
Output is correct |
35 |
Correct |
4 ms |
7372 KB |
Output is correct |
36 |
Correct |
4 ms |
7372 KB |
Output is correct |
37 |
Correct |
4 ms |
7372 KB |
Output is correct |
38 |
Correct |
4 ms |
7320 KB |
Output is correct |
39 |
Correct |
4 ms |
7296 KB |
Output is correct |
40 |
Correct |
4 ms |
7372 KB |
Output is correct |
41 |
Correct |
6 ms |
7372 KB |
Output is correct |
42 |
Correct |
7 ms |
7520 KB |
Output is correct |
43 |
Correct |
4 ms |
7372 KB |
Output is correct |
44 |
Correct |
23 ms |
8180 KB |
Output is correct |
45 |
Correct |
4 ms |
7372 KB |
Output is correct |
46 |
Correct |
4 ms |
7372 KB |
Output is correct |
47 |
Correct |
6 ms |
7372 KB |
Output is correct |
48 |
Correct |
5 ms |
7372 KB |
Output is correct |
49 |
Correct |
4 ms |
7372 KB |
Output is correct |
50 |
Correct |
4 ms |
7372 KB |
Output is correct |
51 |
Correct |
67 ms |
9680 KB |
Output is correct |
52 |
Correct |
38 ms |
8908 KB |
Output is correct |
53 |
Correct |
30 ms |
8860 KB |
Output is correct |
54 |
Correct |
33 ms |
8012 KB |
Output is correct |
55 |
Correct |
74 ms |
11984 KB |
Output is correct |
56 |
Correct |
16 ms |
8444 KB |
Output is correct |
57 |
Correct |
101 ms |
17652 KB |
Output is correct |
58 |
Correct |
108 ms |
17648 KB |
Output is correct |
59 |
Correct |
109 ms |
17628 KB |
Output is correct |
60 |
Correct |
155 ms |
17736 KB |
Output is correct |
61 |
Correct |
209 ms |
17732 KB |
Output is correct |
62 |
Correct |
111 ms |
13220 KB |
Output is correct |
63 |
Correct |
253 ms |
17740 KB |
Output is correct |
64 |
Correct |
107 ms |
13200 KB |
Output is correct |
65 |
Correct |
4 ms |
7372 KB |
Output is correct |
66 |
Correct |
34 ms |
12024 KB |
Output is correct |
67 |
Correct |
121 ms |
11960 KB |
Output is correct |
68 |
Correct |
98 ms |
11972 KB |
Output is correct |
69 |
Correct |
26 ms |
8460 KB |
Output is correct |
70 |
Correct |
10 ms |
7628 KB |
Output is correct |
71 |
Correct |
4 ms |
7372 KB |
Output is correct |
72 |
Correct |
35 ms |
8780 KB |
Output is correct |
73 |
Correct |
10 ms |
7644 KB |
Output is correct |
74 |
Correct |
5 ms |
7372 KB |
Output is correct |
75 |
Correct |
122 ms |
17736 KB |
Output is correct |
76 |
Correct |
131 ms |
17784 KB |
Output is correct |
77 |
Correct |
137 ms |
17736 KB |
Output is correct |
78 |
Correct |
162 ms |
17732 KB |
Output is correct |
79 |
Correct |
215 ms |
17732 KB |
Output is correct |
80 |
Correct |
229 ms |
17808 KB |
Output is correct |
81 |
Correct |
230 ms |
15860 KB |
Output is correct |
82 |
Correct |
227 ms |
15800 KB |
Output is correct |