#include <bits/stdc++.h>
#include "horses.h"
using namespace std;
using ll = long long;
const ll MOD = ((ll) 1e9) + 7;
ll n;
vector < ll > ma;
vector < ll > bit;
int sts = 1;
vector < ll > mst;
set < ll > cs;
ll
binpow (ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b % 2) (ans *= a) %= MOD;
b /= 2;
if (b) (a *= a) %= MOD;
}
return ans;
}
ll
bitp (ll x)
{
ll ans = 1;
while (x >= 0)
{
(ans *= bit[x]) %= MOD;
x -= (x + 1) & -(x + 1);
}
return ans;
}
ll
bitr (ll l, ll r)
{
ll ans = bitp (r);
if (l) (ans *= binpow (bitp (l - 1), MOD - 2)) %= MOD;
return ans;
}
ll
stm (ll l, ll r)
{
l += sts, r += sts;
ll mx = -1;
while (l <= r)
{
if (l % 2 == 1) mx = max (mx, mst[l++]);
if (r % 2 == 0) mx = max (mx, mst[r--]);
l /= 2, r /= 2;
}
return mx;
}
int
calc ()
{
auto it = cs.end (), pit = prev (it);
ll vl = 0;
while (vl < MOD)
{
--it, --pit;
vl = max (vl, mst[sts + *it]);
vl *= ma[*it];
if (vl >= MOD) break;
if (*it && *pit + 1 <= *it - 1)
vl = max (vl, stm (*pit + 1, *it - 1));
if (!*pit) ++pit;
if (!*it) break;
}
vl %= MOD;
if (*it)
(vl *= bitp (*it - 1)) %= MOD;
return vl;
}
int init(int N, int x[], int y[]) {
n = N;
ma.resize (n);
bit.resize (n);
while (sts < n) sts *= 2;
mst.resize (sts * 2);
for (int i = 0; i < n; ++i)
{
if (x[i] > 1) cs.insert (i);
ma[i] = x[i];
mst[sts + i] = y[i];
bit[i] = (x[i] * (i ? bitr (i - ((i + 1) & -(i + 1)) + 1, i - 1) : 1ll)) % MOD;
}
for (int i = sts - 1; i > 0; --i) mst[i] = max (mst[i * 2], mst[i * 2 + 1]);
cs.insert (0);
cs.insert (n - 1);
return calc ();
}
int updateX(int pos, int val) {
cs.erase (pos);
if (val > 1) cs.insert (pos);
ll dl = val * binpow (ma[pos], MOD - 2) % MOD;
ma[pos] = val;
for (int i = pos; i < n; i += (i + 1) & -(i + 1)) (bit[i] *= dl) %= MOD;
cs.insert (0);
cs.insert (n - 1);
return calc ();
}
int updateY(int pos, int val) {
int i = sts + pos;
mst[i] = val;
for (i /= 2; i > 0; i /= 2) mst[i] = max (mst[i * 2], mst[i * 2 + 1]);
return calc ();
}
Compilation message
horses.cpp: In function 'int calc()':
horses.cpp:81:10: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
81 | return vl;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
424 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
44724 KB |
Output is correct |
2 |
Correct |
340 ms |
44724 KB |
Output is correct |
3 |
Correct |
312 ms |
48624 KB |
Output is correct |
4 |
Correct |
372 ms |
52540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
123 ms |
20392 KB |
Output is correct |
34 |
Correct |
121 ms |
24492 KB |
Output is correct |
35 |
Correct |
230 ms |
54784 KB |
Output is correct |
36 |
Correct |
251 ms |
55220 KB |
Output is correct |
37 |
Correct |
121 ms |
22612 KB |
Output is correct |
38 |
Correct |
169 ms |
35392 KB |
Output is correct |
39 |
Correct |
112 ms |
22456 KB |
Output is correct |
40 |
Correct |
226 ms |
49784 KB |
Output is correct |
41 |
Correct |
111 ms |
22608 KB |
Output is correct |
42 |
Correct |
114 ms |
22472 KB |
Output is correct |
43 |
Correct |
230 ms |
50396 KB |
Output is correct |
44 |
Correct |
222 ms |
50164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
270 ms |
46368 KB |
Output is correct |
34 |
Correct |
337 ms |
57292 KB |
Output is correct |
35 |
Correct |
306 ms |
48468 KB |
Output is correct |
36 |
Correct |
366 ms |
52368 KB |
Output is correct |
37 |
Correct |
120 ms |
24404 KB |
Output is correct |
38 |
Correct |
119 ms |
24404 KB |
Output is correct |
39 |
Correct |
239 ms |
54612 KB |
Output is correct |
40 |
Correct |
259 ms |
54640 KB |
Output is correct |
41 |
Correct |
121 ms |
22492 KB |
Output is correct |
42 |
Correct |
160 ms |
35216 KB |
Output is correct |
43 |
Correct |
112 ms |
22704 KB |
Output is correct |
44 |
Correct |
243 ms |
49808 KB |
Output is correct |
45 |
Correct |
111 ms |
22424 KB |
Output is correct |
46 |
Correct |
114 ms |
22472 KB |
Output is correct |
47 |
Correct |
241 ms |
50184 KB |
Output is correct |
48 |
Correct |
214 ms |
50236 KB |
Output is correct |
49 |
Correct |
165 ms |
27424 KB |
Output is correct |
50 |
Correct |
164 ms |
27476 KB |
Output is correct |
51 |
Correct |
309 ms |
56536 KB |
Output is correct |
52 |
Correct |
299 ms |
56060 KB |
Output is correct |
53 |
Correct |
207 ms |
25680 KB |
Output is correct |
54 |
Correct |
238 ms |
39252 KB |
Output is correct |
55 |
Correct |
161 ms |
23352 KB |
Output is correct |
56 |
Correct |
302 ms |
51532 KB |
Output is correct |
57 |
Correct |
142 ms |
24148 KB |
Output is correct |
58 |
Correct |
164 ms |
24656 KB |
Output is correct |
59 |
Correct |
226 ms |
50292 KB |
Output is correct |