#include "horses.h"
#include <bits/stdc++.h>
#define forn(i, a, b) for(int i = int(a); i <= int(b); ++ i)
#define fora(i, a, b) for(int i = int(a); i >= int(b); -- i)
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int lim = 5e5;
bool stbool[4 * lim + 2];
int stmax[4 * lim + 2];
ll bit[lim + 2];
int n;
ll x[lim + 2];
int y[lim + 2];
int posicion[33];
ll valor[33];
int mayor[33];
int dif1 = 0;
ll expb(ll numero){
ll res = 1LL;
int aux = 1e9 + 5;
forn(i, 0, 29){
if(((1 << i) & aux)){
res *= numero;
res %= mod;
}
numero *= numero;
numero %= mod;
}
return res;
}
void initbool(int nodo, int l, int r){
if(l == r){
stbool[nodo] = (x[l] != 1);
return;
}
int mitad = (l + r) / 2;
initbool(nodo * 2, l, mitad);
initbool(nodo * 2 + 1, mitad + 1, r);
stbool[nodo] = stbool[nodo * 2] | stbool[nodo * 2 + 1];
}
int querybool(int nodo, int l, int r, int rango){
if(l > rango){
return 0;
}
if(!stbool[nodo]){
return 0;
}
if(l == r){
return l;
}
int mitad = (l + r) / 2, value;
value = querybool(nodo * 2 + 1, mitad + 1, r, rango);
if(!value){
value = querybool(nodo * 2, l, mitad, rango);
}
return value;
}
void updatebool(int nodo, int l, int r, int pos){
if(pos < l || pos > r){
return;
}
if(pos == l && pos == r){
stbool[nodo] ^= 1;
return;
}
int mitad = (l + r) / 2;
updatebool(nodo * 2, l, mitad, pos);
updatebool(nodo * 2, mitad + 1, r, pos);
stbool[nodo] = stbool[nodo * 2] | stbool[nodo * 2 + 1];
}
void initmax(int nodo, int l, int r){
if(l == r){
stmax[nodo] = y[l];
return;
}
int mitad = (l + r) / 2;
initmax(nodo * 2, l, mitad);
initmax(nodo * 2 + 1, mitad + 1, r);
stmax[nodo] = max(stmax[nodo * 2], stmax[nodo * 2 + 1]);
}
int querymax(int nodo, int l, int r, int li, int ls){
if(li > r || ls < l){
return 0;
}
if(li <= l && ls >= r){
return stmax[nodo];
}
int mitad = (l + r) / 2;
return max(querymax(nodo * 2, l, mitad, li, ls), querymax(nodo * 2 + 1, mitad + 1, r, li, ls));
}
void updatemax(int nodo, int l, int r, int pos, int val){
if(pos < l || pos > r){
return;
}
if(pos == l && pos == r){
stmax[nodo] = val;
return;
}
int mitad = (l + r) / 2;
updatemax(nodo * 2, l, mitad, pos, val);
updatemax(nodo * 2 + 1, mitad + 1, r, pos, val);
stmax[nodo] = max(stmax[nodo * 2], stmax[nodo * 2 + 1]);
}
void updatebit(int pos, ll valorxd){
for(int i = pos; i <= n; i += i & -i){
bit[i] *= valorxd;
bit[i] %= mod;
}
}
ll querybit(int pos){
ll res = 1LL;
for(int i = pos; i >= 1; i -= i & -i){
res *= bit[i];
res %= mod;
}
return res;
}
ll respuesta(){
int indice = 1;
ll auxiliar = 1LL, res;
if(n < 30){
forn(i, 2, n){
auxiliar *= x[i];
if(auxiliar < (ll) y[indice]){
auxiliar *= (ll) y[i];
}
if(auxiliar >= (ll) y[indice]){
indice = i;
auxiliar = 1LL;
}
else{
auxiliar /= (ll) y[i];
}
}
res = querybit(indice);
res *= (ll) y[indice];
res %= mod;
}
else{
forn(i, 2, 30){
auxiliar *= valor[i];
if(auxiliar < (ll) mayor[indice] && mayor[i]){
auxiliar *= (ll) mayor[i];
}
if(auxiliar >= (ll) mayor[indice]){
auxiliar = 1LL;
indice = i;
}
else if(mayor[i]){
auxiliar /= (ll) mayor[i];
}
}
res = (valor[indice] == 1LL) ? 1LL : querybit(posicion[indice]);
res *= (ll) mayor[indice];
res %= mod;
}
return res;
}
int init(int N, int X[], int Y[]) {
n = N;
forn(i, 0, n - 1){
x[i + 1] = (ll) X[i];
y[i + 1] = Y[i];
bit[i + 1] = 1;
}
initbool(1, 1, n);
initmax(1, 1, n);
forn(i, 1, n){
if(x[i] != 1){
updatebit(i, x[i]);
dif1 ++;
}
}
posicion[31] = n + 1;
ll res;
if(n >= 30){
fora(i, 30, max(1, 31 - dif1)){
posicion[i] = querybool(1, 1, n, posicion[i + 1] - 1);
valor[i] = x[posicion[i]];
mayor[i] = querymax(1, 1, n, posicion[i], posicion[i + 1] - 1);
}
fora(i, max(1, 31 - dif1) - 1, 1){
posicion[i] = 0;
valor[i] = 1LL;
if(posicion[i + 1]){
mayor[i] = querymax(1, 1, n, 1, posicion[i + 1] - 1);
}
else{
mayor[i] = mayor[i + 1];
}
}
}
/**forn(i, 1, 30){
cout << posicion[i] << " " << mayor[i] << " " << valor[i] << "\n";
}*/
res = respuesta();
return (int) res;
}
int updateX(int pos, int val) {
ll actualiza = x[pos + 1];
if(val != (ll) actualiza){
actualiza = expb(actualiza);
actualiza *= (ll) val;
actualiza %= mod;
updatebit(pos + 1, actualiza);
if(val == 1){
updatebool(1, 1, n, pos + 1);
dif1 --;
}
if(val > 1 && x[pos + 1] == 1){
updatebool(1, 1, n, pos + 1);
dif1 ++;
}
if(n >= 30){
if(val == 1){
if(pos + 1 > posicion[1]){
forn(i, 2, 30){
if(posicion[i] == pos + 1){
posicion[i] = posicion[i - 1];
mayor[i] = max(mayor[i], mayor[i - 1]);
valor[i] = valor[i - 1];
fora(j, i - 1, 2){
posicion[j] = posicion[j - 1];
mayor[j] = mayor[j - 1];
valor[j] = valor[j - 1];
}
break;
}
}
}
if(pos + 1 >= posicion[1]){
if(dif1 >= 30){
posicion[1] = querybool(1, 1, n, posicion[2] - 1);
mayor[1] = querymax(1, 1, n, posicion[1], posicion[2] - 1);
valor[1] = x[posicion[1]];
}
else{
posicion[1] = 0;
mayor[1] = querymax(1, 1, n, 1, posicion[31 - dif1] - 1);
valor[1] = 1LL;
}
}
}
else if(x[pos + 1] > 1 && posicion[1] <= pos + 1){
forn(i, 1, 30){
if(posicion[i] == pos + 1){
valor[i] = (ll) val;
break;
}
}
}
else if(x[pos + 1] == 1 && posicion[1] < pos + 1){
forn(i, 1, 30){
if(posicion[i + 1] > pos + 1){
posicion[i] = pos + 1;
valor[i] = (ll) val;
mayor[i] = querymax(1, 1, n, pos + 1, posicion[i + 1] - 1);
if(i != 1){
if(posicion[i - 1] == 0){
mayor[i - 1] = querymax(1, 1, n, 1, pos);
fora(j, i - 2, 1){
mayor[j] = mayor[j + 1];
}
}
else{
mayor[i - 1] = querymax(1, 1, n, posicion[i - 1], pos);
}
}
break;
}
else{
posicion[i] = posicion[i + 1];
mayor[i] = mayor[i + 1];
valor[i] = valor[i + 1];
}
}
}
}
x[pos + 1] = (ll) val;
/**forn(i, 1, 30){
cout << posicion[i] << " " << mayor[i] << " " << valor[i] << "\n";
}*/
}
ll res = respuesta();
return (int) res;
}
int updateY(int pos, int val) {
updatemax(1, 1, n, pos + 1, val);
y[pos + 1] = val;
if(n >= 30){
fora(i, 30, 1){
if(pos + 1 >= posicion[i]){
mayor[i] = querymax(1, 1, n, (posicion[i] == 0) ? 1 : posicion[i], posicion[i + 1] - 1);
if(posicion[i] == 0){
fora(j, i - 1, 1){
mayor[j] = mayor[j + 1];
}
}
break;
}
}
/**forn(i, 1, 30){
cout << posicion[i] << " " << mayor[i] << " " << valor[i] << "\n";
}*/
}
ll res = respuesta();
return (int) res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
20008 KB |
Output is correct |
2 |
Correct |
126 ms |
20132 KB |
Output is correct |
3 |
Correct |
99 ms |
20056 KB |
Output is correct |
4 |
Correct |
94 ms |
20072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
300 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
37 ms |
23108 KB |
Output is correct |
34 |
Correct |
37 ms |
23160 KB |
Output is correct |
35 |
Correct |
78 ms |
29236 KB |
Output is correct |
36 |
Correct |
72 ms |
29252 KB |
Output is correct |
37 |
Correct |
26 ms |
21236 KB |
Output is correct |
38 |
Correct |
36 ms |
22204 KB |
Output is correct |
39 |
Correct |
26 ms |
21100 KB |
Output is correct |
40 |
Correct |
59 ms |
25048 KB |
Output is correct |
41 |
Correct |
28 ms |
21240 KB |
Output is correct |
42 |
Correct |
30 ms |
21316 KB |
Output is correct |
43 |
Correct |
51 ms |
25480 KB |
Output is correct |
44 |
Correct |
58 ms |
25528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
272 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
308 KB |
Output is correct |
33 |
Correct |
87 ms |
23992 KB |
Output is correct |
34 |
Correct |
126 ms |
29616 KB |
Output is correct |
35 |
Correct |
95 ms |
24004 KB |
Output is correct |
36 |
Correct |
101 ms |
27716 KB |
Output is correct |
37 |
Correct |
36 ms |
23076 KB |
Output is correct |
38 |
Correct |
36 ms |
23032 KB |
Output is correct |
39 |
Correct |
77 ms |
28448 KB |
Output is correct |
40 |
Correct |
73 ms |
28704 KB |
Output is correct |
41 |
Correct |
27 ms |
21292 KB |
Output is correct |
42 |
Correct |
37 ms |
22212 KB |
Output is correct |
43 |
Correct |
26 ms |
21108 KB |
Output is correct |
44 |
Correct |
60 ms |
25140 KB |
Output is correct |
45 |
Correct |
26 ms |
21168 KB |
Output is correct |
46 |
Correct |
33 ms |
21304 KB |
Output is correct |
47 |
Correct |
56 ms |
25520 KB |
Output is correct |
48 |
Correct |
51 ms |
25456 KB |
Output is correct |
49 |
Correct |
94 ms |
25132 KB |
Output is correct |
50 |
Correct |
84 ms |
25136 KB |
Output is correct |
51 |
Correct |
122 ms |
29376 KB |
Output is correct |
52 |
Correct |
103 ms |
29160 KB |
Output is correct |
53 |
Correct |
76 ms |
23404 KB |
Output is correct |
54 |
Correct |
70 ms |
24040 KB |
Output is correct |
55 |
Correct |
67 ms |
22212 KB |
Output is correct |
56 |
Correct |
117 ms |
26956 KB |
Output is correct |
57 |
Correct |
70 ms |
22836 KB |
Output is correct |
58 |
Correct |
79 ms |
23364 KB |
Output is correct |
59 |
Correct |
51 ms |
25392 KB |
Output is correct |