#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 > 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 |
3 ms |
7372 KB |
Output is correct |
2 |
Correct |
3 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 |
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 |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
105 ms |
11988 KB |
Output is correct |
12 |
Correct |
108 ms |
11932 KB |
Output is correct |
13 |
Correct |
97 ms |
11964 KB |
Output is correct |
14 |
Correct |
74 ms |
11992 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
34 ms |
11980 KB |
Output is correct |
17 |
Correct |
116 ms |
11948 KB |
Output is correct |
18 |
Correct |
99 ms |
11960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
34 ms |
11980 KB |
Output is correct |
3 |
Correct |
116 ms |
11948 KB |
Output is correct |
4 |
Correct |
99 ms |
11960 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7288 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7372 KB |
Output is correct |
11 |
Correct |
4 ms |
7372 KB |
Output is correct |
12 |
Correct |
4 ms |
7372 KB |
Output is correct |
13 |
Correct |
55 ms |
9736 KB |
Output is correct |
14 |
Correct |
36 ms |
8900 KB |
Output is correct |
15 |
Correct |
27 ms |
8524 KB |
Output is correct |
16 |
Correct |
23 ms |
8196 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 |
3 ms |
7372 KB |
Output is correct |
22 |
Correct |
5 ms |
7372 KB |
Output is correct |
23 |
Correct |
6 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7372 KB |
Output is correct |
2 |
Correct |
3 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 |
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 |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
105 ms |
11988 KB |
Output is correct |
12 |
Correct |
108 ms |
11932 KB |
Output is correct |
13 |
Correct |
97 ms |
11964 KB |
Output is correct |
14 |
Correct |
74 ms |
11992 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
3 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7288 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 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 |
55 ms |
9736 KB |
Output is correct |
24 |
Correct |
36 ms |
8900 KB |
Output is correct |
25 |
Correct |
27 ms |
8524 KB |
Output is correct |
26 |
Correct |
23 ms |
8196 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 |
3 ms |
7372 KB |
Output is correct |
32 |
Correct |
5 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 |
11980 KB |
Output is correct |
36 |
Correct |
116 ms |
11948 KB |
Output is correct |
37 |
Correct |
99 ms |
11960 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 |
7372 KB |
Output is correct |
43 |
Correct |
4 ms |
7372 KB |
Output is correct |
44 |
Correct |
4 ms |
7372 KB |
Output is correct |
45 |
Correct |
7 ms |
7372 KB |
Output is correct |
46 |
Correct |
8 ms |
7500 KB |
Output is correct |
47 |
Correct |
4 ms |
7372 KB |
Output is correct |
48 |
Correct |
26 ms |
8164 KB |
Output is correct |
49 |
Correct |
4 ms |
7372 KB |
Output is correct |
50 |
Correct |
5 ms |
7288 KB |
Output is correct |
51 |
Correct |
6 ms |
7372 KB |
Output is correct |
52 |
Correct |
4 ms |
7372 KB |
Output is correct |
53 |
Correct |
4 ms |
7372 KB |
Output is correct |
54 |
Correct |
3 ms |
7372 KB |
Output is correct |
55 |
Correct |
58 ms |
9804 KB |
Output is correct |
56 |
Correct |
38 ms |
8860 KB |
Output is correct |
57 |
Correct |
31 ms |
8856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
3 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7372 KB |
Output is correct |
4 |
Correct |
4 ms |
7288 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
55 ms |
9736 KB |
Output is correct |
10 |
Correct |
36 ms |
8900 KB |
Output is correct |
11 |
Correct |
27 ms |
8524 KB |
Output is correct |
12 |
Correct |
23 ms |
8196 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 |
3 ms |
7372 KB |
Output is correct |
18 |
Correct |
5 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 |
11980 KB |
Output is correct |
22 |
Correct |
116 ms |
11948 KB |
Output is correct |
23 |
Correct |
99 ms |
11960 KB |
Output is correct |
24 |
Incorrect |
12 ms |
8024 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7372 KB |
Output is correct |
2 |
Correct |
3 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 |
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 |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
105 ms |
11988 KB |
Output is correct |
12 |
Correct |
108 ms |
11932 KB |
Output is correct |
13 |
Correct |
97 ms |
11964 KB |
Output is correct |
14 |
Correct |
74 ms |
11992 KB |
Output is correct |
15 |
Correct |
4 ms |
7372 KB |
Output is correct |
16 |
Correct |
3 ms |
7372 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7288 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 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 |
55 ms |
9736 KB |
Output is correct |
24 |
Correct |
36 ms |
8900 KB |
Output is correct |
25 |
Correct |
27 ms |
8524 KB |
Output is correct |
26 |
Correct |
23 ms |
8196 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 |
3 ms |
7372 KB |
Output is correct |
32 |
Correct |
5 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 |
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 |
7 ms |
7372 KB |
Output is correct |
42 |
Correct |
8 ms |
7500 KB |
Output is correct |
43 |
Correct |
4 ms |
7372 KB |
Output is correct |
44 |
Correct |
26 ms |
8164 KB |
Output is correct |
45 |
Correct |
4 ms |
7372 KB |
Output is correct |
46 |
Correct |
5 ms |
7288 KB |
Output is correct |
47 |
Correct |
6 ms |
7372 KB |
Output is correct |
48 |
Correct |
4 ms |
7372 KB |
Output is correct |
49 |
Correct |
4 ms |
7372 KB |
Output is correct |
50 |
Correct |
3 ms |
7372 KB |
Output is correct |
51 |
Correct |
58 ms |
9804 KB |
Output is correct |
52 |
Correct |
38 ms |
8860 KB |
Output is correct |
53 |
Correct |
31 ms |
8856 KB |
Output is correct |
54 |
Incorrect |
12 ms |
8024 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |