#include <bits/stdc++.h>
#include "horses.h"
#define DIMN 500010
#define MOD 1000000007
using namespace std;
long long nt , xt[DIMN] , yt[DIMN];
set < pair < pair <long long , long long> , pair <long long , long long> > > state;
set < pair < pair <long long , long long> , pair <long long , long long> > > :: iterator aux , it;
set < pair < pair <long long , long long> , pair <long long , long long> > > :: reverse_iterator rit;
long long aint[4*DIMN] , prod[4 * DIMN];
void build (long long nod , long long st , long long dr){
long long mid = (st + dr)/2;
if (st == dr){
aint[nod] = st;
prod[nod] = xt[st];
return;
}
build (2 * nod , st , mid);
build (2 * nod + 1 , mid + 1 , dr);
if (yt[aint[2 * nod]] > yt[aint[2 * nod + 1]])
aint[nod] = aint[2 * nod];
else aint[nod] = aint[2 * nod + 1];
prod[nod] = (1LL * prod[2 * nod] * prod[2 * nod + 1])%MOD;
}
void update_prod (long long nod , long long st , long long dr , long long pos , long long val){
long long mid = (st + dr)/2;
if (st == dr){
prod[nod] = val;
return;
}
if (pos <= mid)
update_prod(2 * nod , st , mid , pos , val);
else
update_prod(2 * nod + 1 , mid + 1 , dr , pos , val);
prod[nod] = (1LL * prod[2 * nod] * prod[2 * nod + 1])%MOD;
}
long long query (long long nod , long long st , long long dr , long long x , long long y){
long long mid = (st + dr)/2 , maxi = -1 , val;
if (x <= st && dr <= y){
return aint[nod];
}
if (x <= mid){
val = query(2 * nod , st , mid , x , y);
if (maxi == -1 || yt[maxi] < yt[val])
maxi = val;
}
if (mid + 1 <= y){
val = query(2 * nod + 1 , mid + 1 , dr , x , y);
if (maxi == -1 || yt[maxi] < yt[val])
maxi = val;
}
return maxi;
}
long long query_prod (long long nod , long long st , long long dr , long long x , long long y){
long long mid = (st + dr)/2 , sol = 1;
if (x <= st && dr <= y){
return prod[nod];
}
if (x <= mid)
sol = (1LL * sol * query_prod(2 * nod , st , mid , x , y))%MOD;
if (mid + 1 <= y)
sol = (1LL * sol * query_prod(2 * nod + 1 , mid + 1 , dr , x , y))%MOD;
return sol;
}
void update (long long nod , long long st , long long dr , long long pos , long long val){
long long mid = (st + dr)/2;
if (st == dr){
aint[nod] = pos;
return;
}
if (pos <= mid)
update(2 * nod , st , mid , pos , val);
else
update(2 * nod + 1 , mid + 1 , dr , pos , val);
if (yt[aint[2 * nod]] > yt[aint[2 * nod + 1]])
aint[nod] = aint[2 * nod];
else aint[nod] = aint[2 * nod + 1];
}
long long solve_current_state (){
long long idx , inc , ymax , taken;
long long have_sol;
/// !! solutia e in ultimele 60 de pozitii
have_sol = 0;
idx = 0;
for (rit = state.rbegin() , taken = 1 ; rit != state.rend() && taken <= 60; rit++ , taken++){
inc = (*rit).first.first;
ymax = (*rit).second.second;
if (have_sol == 0){
have_sol = 1LL * xt[inc] * yt[ymax];
idx = ymax;
}
else {
if (yt[ymax] < have_sol){
if (have_sol <= 1000000000)
have_sol = (have_sol * xt[ymax]);
}
else {
have_sol = 1LL * xt[inc] * yt[ymax];
idx = ymax;
}
}
}
return (1LL * query_prod (1 , 0 , nt - 1 , 0 , idx) * yt[idx])%MOD;
}
int init(int n, int x[], int y[]){
long long i , inc , sf , ymax;
nt = n;
for (i = 0 ; i < n ; i++){
xt[i] = x[i];
yt[i] = y[i];
}
for (i = 0 ; i < n ; i++){
if (x[i] != 1){
state.insert(make_pair( make_pair(i , i) , make_pair(x[i] , i) ));
}
else {
inc = i;
sf = i;
ymax = i;
while (sf + 1 < n && x[sf + 1] == 1){
if (y[ymax] < y[sf + 1])
ymax = sf + 1;
sf++;
}
state.insert(make_pair( make_pair(inc , sf) , make_pair(1 , ymax) ));
i = sf;
}
}
build (1 , 0 , nt - 1);
return solve_current_state();
}
int updateX(int pos, int val){
long long inc , sf , vcurr , ymax , inc2 , sf2 , ymax2;
it = state.upper_bound(make_pair ( make_pair(pos , nt) , make_pair(0 , 0) ));
it--;
inc = (*it).first.first;
sf = (*it).first.second;
vcurr = (*it).second.first;
ymax = (*it).second.second;
if (val == 1){ /// cazul cand poate se prelungeste un long longerval de 1
/// , sau se form unul nou
if (vcurr != 1){
vcurr = 1;
aux = it;
aux--;
if (it != state.begin() && (*aux).second.first == 1){ /// aux exista
/// ai un long longerval de 1 la stanga
inc2 = (*aux).first.first;
sf2 = (*aux).first.second;
ymax2 = (*aux).second.second;
if (yt[ymax] < yt[ymax2])
ymax = ymax2;
inc = inc2;
state.erase(aux);
}
aux = it;
aux++;
if (aux != state.end() && (*aux).second.first == 1){
/// ai un long longerval de 1 la dreapta
inc2 = (*aux).first.first;
sf2 = (*aux).first.second;
ymax2 = (*aux).second.second;
if (yt[ymax] < yt[ymax2])
ymax = ymax2;
sf = sf2;
state.erase(aux);
}
state.erase(it);
state.insert(make_pair( make_pair(inc , sf) , make_pair(1 , ymax) ));
}
}
else { /// cazul cand se destrama un long longerval de 1, sau doar se updateaza o val
if (vcurr != 1){ /// se updateaza o valoare normala
state.erase(it);
state.insert(make_pair( make_pair(inc , sf) , make_pair(val , ymax) ));
}
else { /// se destrama un long longerval
state.erase(it);
state.insert(make_pair( make_pair(pos , pos) , make_pair(val , pos) ));
if (inc < pos){
ymax = query (1 , 0 , nt - 1 , inc , pos - 1);
state.insert(make_pair( make_pair(inc , pos - 1) , make_pair(1 , ymax) ));
}
if (pos < sf){
ymax = query (1 , 0 , nt - 1 , pos + 1 , sf);
state.insert(make_pair( make_pair(pos + 1 , sf) , make_pair(1 , ymax) ));
}
}
}
xt[pos] = val;
update_prod (1 , 0 , nt - 1 , pos , val);
return solve_current_state();
}
int updateY(int pos, int val){
long long inc , sf , vcurr , ymax;
it = state.upper_bound(make_pair ( make_pair(pos , nt) , make_pair(0 , 0) ));
it--;
inc = (*it).first.first;
sf = (*it).first.second;
vcurr = (*it).second.first;
ymax = (*it).second.second;
state.erase(it);
yt[pos] = val;
update (1 , 0 , nt - 1 , pos , val);
ymax = query(1 , 0 , nt - 1 , inc , sf);
state.insert(make_pair( make_pair(inc , sf) , make_pair(vcurr , ymax) ));
return solve_current_state();
}
Compilation message
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:196:31: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
return solve_current_state();
~~~~~~~~~~~~~~~~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:306:31: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
return solve_current_state();
~~~~~~~~~~~~~~~~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:331:31: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
return solve_current_state();
~~~~~~~~~~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
6 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
316 ms |
68728 KB |
Output is correct |
2 |
Correct |
517 ms |
68728 KB |
Output is correct |
3 |
Correct |
441 ms |
68600 KB |
Output is correct |
4 |
Correct |
456 ms |
68856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
62 ms |
32888 KB |
Output is correct |
34 |
Correct |
63 ms |
32872 KB |
Output is correct |
35 |
Correct |
224 ms |
78712 KB |
Output is correct |
36 |
Correct |
220 ms |
78584 KB |
Output is correct |
37 |
Correct |
52 ms |
31100 KB |
Output is correct |
38 |
Correct |
195 ms |
70776 KB |
Output is correct |
39 |
Correct |
43 ms |
30588 KB |
Output is correct |
40 |
Correct |
201 ms |
73784 KB |
Output is correct |
41 |
Correct |
43 ms |
30712 KB |
Output is correct |
42 |
Correct |
45 ms |
30712 KB |
Output is correct |
43 |
Correct |
186 ms |
73976 KB |
Output is correct |
44 |
Correct |
196 ms |
74152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
768 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
310 ms |
72440 KB |
Output is correct |
34 |
Correct |
481 ms |
81272 KB |
Output is correct |
35 |
Correct |
444 ms |
72588 KB |
Output is correct |
36 |
Correct |
490 ms |
76408 KB |
Output is correct |
37 |
Correct |
63 ms |
33016 KB |
Output is correct |
38 |
Correct |
59 ms |
33016 KB |
Output is correct |
39 |
Correct |
226 ms |
78712 KB |
Output is correct |
40 |
Correct |
228 ms |
78584 KB |
Output is correct |
41 |
Correct |
49 ms |
31096 KB |
Output is correct |
42 |
Correct |
183 ms |
70776 KB |
Output is correct |
43 |
Correct |
40 ms |
30592 KB |
Output is correct |
44 |
Correct |
199 ms |
73848 KB |
Output is correct |
45 |
Correct |
42 ms |
30712 KB |
Output is correct |
46 |
Correct |
45 ms |
30696 KB |
Output is correct |
47 |
Correct |
198 ms |
74080 KB |
Output is correct |
48 |
Correct |
187 ms |
74104 KB |
Output is correct |
49 |
Correct |
279 ms |
38264 KB |
Output is correct |
50 |
Correct |
321 ms |
38136 KB |
Output is correct |
51 |
Correct |
351 ms |
80536 KB |
Output is correct |
52 |
Correct |
322 ms |
80120 KB |
Output is correct |
53 |
Correct |
229 ms |
36344 KB |
Output is correct |
54 |
Correct |
303 ms |
72568 KB |
Output is correct |
55 |
Correct |
111 ms |
31608 KB |
Output is correct |
56 |
Correct |
330 ms |
75512 KB |
Output is correct |
57 |
Correct |
125 ms |
32248 KB |
Output is correct |
58 |
Correct |
164 ms |
32760 KB |
Output is correct |
59 |
Correct |
186 ms |
73980 KB |
Output is correct |