#include <bits/stdc++.h>
#ifdef atom
#include "grader.cpp"
#else
#include "horses.h"
#endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii;
#define X first
#define Y second
#define vi vector<int>
#define vii vector< ii >
#define pb push_back
const int maxn = 500005;
const int md = 1e9 + 7;
ll prod[4*maxn];
ll most[4*maxn];
int x[4*maxn];
int y[4*maxn];
int n;
int mul(ll a, ll b)
{
a %= md; b %= md;
return (1LL*a*b)%md;
}
void update(ll *st, int x, int dx, int md, int p = 1, int L = 1, int R = n)
{
if(L> x || R< x) return;
if(L == R && L == x)
{
st[p] = dx;
return;
}
int M = (L+R)/2;
//printf("%d %d %d\n", L, R, M);
//system("pause");
update(st, x, dx, md, 2*p, L, M); update(st, x, dx, md, 2*p+1, M+1, R);
if(md == 1) st[p] = mul(st[2*p], st[2*p+1]);
else st[p] = max(st[2*p], st[2*p+1]);
}
ll ask(ll *st, int i, int j, int md, int p = 1, int L = 1, int R = n)
{
if(i> R || j< L) return 1;
if(i<= L && R<= j) return st[p];
int M = (L+R)/2;
ll x = ask(st, i, j, md, 2*p, L, M);
ll y = ask(st, i, j, md, 2*p+1, M+1, R);
if(md == 1) return mul(x, y);
else return max(x, y);
}
set<int> bst;
int query()
{
vi tmp;
ll product = 1;
int bef = -1;
for(auto it = bst.rbegin(); it != bst.rend(); it++)
{
if(x[*it]*product<= 1e9)
{
product *= x[*it];
tmp.pb(*it);
}
else
{
bef = *it;
break;
}
}
reverse(tmp.begin(), tmp.end());
if(tmp.empty()) return ask(most, 1, n, 2);
int q = tmp.size();
if(bef == -1)
{
ll best = ask(most, 1, tmp[0]-1, 2);
ll run = 1;
for(int i = 0; i< q; i++)
{
run *= x[tmp[i]];
int rend = i+1< q?tmp[i+1]:n+1;
best = max(best, run*ask(most, tmp[i], rend-1, 2));
}
return best%md;
}
ll best = ask(most, bef, tmp[0]-1, 2);
ll run = 1;
for(int i = 0; i< q; i++)
{
run *= x[tmp[i]];
int rend = i+1<q?tmp[i+1]:n+1;
best = max(best, run*ask(most, tmp[i], rend-1, 2));
}
return mul(best, ask(prod, 1, tmp[0]-1, 1));
}
int init(int N, int X[], int Y[])
{
n = N;
for(int i = 1; i<= n; i++) x[i] = X[i-1], y[i] = Y[i-1];
for(int i = 1; i<= N; i++) update(prod, i, x[i], 1);
for(int i = 1; i<= N; i++) update(most, i, y[i], 2);
for(int i = 1; i<= N; i++) if(x[i]> 1) bst.insert(i);
return query();
}
int updateX(int pos, int val)
{
pos++;
x[pos] = val;
if(val> 1) bst.insert(pos);
else bst.erase(pos);
update(prod, pos, val, 1);
return query();
}
int updateY(int pos, int val)
{
pos++;
y[pos] = val;
update(most, pos, val, 2);
return query();
}
Compilation message
horses.cpp: In function 'int mul(ll, ll)':
horses.cpp:24:18: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
return (1LL*a*b)%md;
~~~~~~~~~^~~
horses.cpp: In function 'void update(ll*, int, int, int, int, int, int)':
horses.cpp:26:75: warning: declaration of 'md' shadows a global declaration [-Wshadow]
void update(ll *st, int x, int dx, int md, int p = 1, int L = 1, int R = n)
^
horses.cpp:15:11: note: shadowed declaration is here
const int md = 1e9 + 7;
^~
horses.cpp:26:75: warning: declaration of 'x' shadows a global declaration [-Wshadow]
void update(ll *st, int x, int dx, int md, int p = 1, int L = 1, int R = n)
^
horses.cpp:18:5: note: shadowed declaration is here
int x[4*maxn];
^
horses.cpp: In function 'll ask(ll*, int, int, int, int, int, int)':
horses.cpp:41:69: warning: declaration of 'md' shadows a global declaration [-Wshadow]
ll ask(ll *st, int i, int j, int md, int p = 1, int L = 1, int R = n)
^
horses.cpp:15:11: note: shadowed declaration is here
const int md = 1e9 + 7;
^~
horses.cpp:46:5: warning: declaration of 'x' shadows a global declaration [-Wshadow]
ll x = ask(st, i, j, md, 2*p, L, M);
^
horses.cpp:18:5: note: shadowed declaration is here
int x[4*maxn];
^
horses.cpp:47:5: warning: declaration of 'y' shadows a global declaration [-Wshadow]
ll y = ask(st, i, j, md, 2*p+1, M+1, R);
^
horses.cpp:19:5: note: shadowed declaration is here
int y[4*maxn];
^
horses.cpp: In function 'int query()':
horses.cpp:59:12: warning: conversion to 'double' from 'll {aka long long int}' may alter its value [-Wconversion]
if(x[*it]*product<= 1e9)
~~~~~~^~~~~~~~
horses.cpp:71:28: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
if(tmp.empty()) return ask(most, 1, n, 2);
~~~^~~~~~~~~~~~~~~
horses.cpp:72:18: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
int q = tmp.size();
~~~~~~~~^~
horses.cpp:83:14: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
return best%md;
~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
368 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
380 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
276 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
4 ms |
504 KB |
Output is correct |
24 |
Correct |
4 ms |
504 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
3 ms |
504 KB |
Output is correct |
27 |
Correct |
8 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
4 ms |
376 KB |
Output is correct |
30 |
Correct |
4 ms |
504 KB |
Output is correct |
31 |
Correct |
7 ms |
504 KB |
Output is correct |
32 |
Correct |
8 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1490 ms |
53092 KB |
Output is correct |
2 |
Correct |
835 ms |
61796 KB |
Output is correct |
3 |
Correct |
867 ms |
52984 KB |
Output is correct |
4 |
Correct |
973 ms |
56812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
316 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
380 KB |
Output is correct |
23 |
Correct |
4 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
4 ms |
508 KB |
Output is correct |
27 |
Correct |
8 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
4 ms |
376 KB |
Output is correct |
30 |
Correct |
4 ms |
504 KB |
Output is correct |
31 |
Correct |
6 ms |
504 KB |
Output is correct |
32 |
Correct |
7 ms |
504 KB |
Output is correct |
33 |
Correct |
408 ms |
28832 KB |
Output is correct |
34 |
Correct |
407 ms |
28720 KB |
Output is correct |
35 |
Correct |
610 ms |
59144 KB |
Output is correct |
36 |
Correct |
604 ms |
59192 KB |
Output is correct |
37 |
Correct |
496 ms |
27008 KB |
Output is correct |
38 |
Correct |
496 ms |
39684 KB |
Output is correct |
39 |
Correct |
393 ms |
26720 KB |
Output is correct |
40 |
Correct |
578 ms |
54080 KB |
Output is correct |
41 |
Correct |
438 ms |
26744 KB |
Output is correct |
42 |
Correct |
470 ms |
26844 KB |
Output is correct |
43 |
Correct |
563 ms |
54464 KB |
Output is correct |
44 |
Correct |
566 ms |
54520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
404 KB |
Output is correct |
19 |
Correct |
2 ms |
400 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
4 ms |
472 KB |
Output is correct |
24 |
Correct |
4 ms |
376 KB |
Output is correct |
25 |
Correct |
4 ms |
516 KB |
Output is correct |
26 |
Correct |
3 ms |
564 KB |
Output is correct |
27 |
Correct |
8 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
4 ms |
380 KB |
Output is correct |
30 |
Correct |
4 ms |
504 KB |
Output is correct |
31 |
Correct |
6 ms |
376 KB |
Output is correct |
32 |
Correct |
7 ms |
376 KB |
Output is correct |
33 |
Correct |
1494 ms |
52972 KB |
Output is correct |
34 |
Correct |
838 ms |
61704 KB |
Output is correct |
35 |
Correct |
871 ms |
52912 KB |
Output is correct |
36 |
Correct |
972 ms |
56692 KB |
Output is correct |
37 |
Correct |
406 ms |
28840 KB |
Output is correct |
38 |
Correct |
406 ms |
28760 KB |
Output is correct |
39 |
Correct |
621 ms |
59256 KB |
Output is correct |
40 |
Correct |
594 ms |
59084 KB |
Output is correct |
41 |
Correct |
500 ms |
27208 KB |
Output is correct |
42 |
Correct |
510 ms |
39732 KB |
Output is correct |
43 |
Correct |
393 ms |
26744 KB |
Output is correct |
44 |
Correct |
579 ms |
54244 KB |
Output is correct |
45 |
Correct |
442 ms |
26808 KB |
Output is correct |
46 |
Correct |
471 ms |
26888 KB |
Output is correct |
47 |
Correct |
582 ms |
54464 KB |
Output is correct |
48 |
Correct |
600 ms |
54520 KB |
Output is correct |
49 |
Correct |
625 ms |
31868 KB |
Output is correct |
50 |
Correct |
589 ms |
31844 KB |
Output is correct |
51 |
Correct |
892 ms |
60948 KB |
Output is correct |
52 |
Correct |
731 ms |
60380 KB |
Output is correct |
53 |
Correct |
1442 ms |
30128 KB |
Output is correct |
54 |
Correct |
860 ms |
43556 KB |
Output is correct |
55 |
Correct |
554 ms |
27796 KB |
Output is correct |
56 |
Correct |
741 ms |
55848 KB |
Output is correct |
57 |
Correct |
1010 ms |
28384 KB |
Output is correct |
58 |
Correct |
1315 ms |
28912 KB |
Output is correct |
59 |
Correct |
565 ms |
54452 KB |
Output is correct |