#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define ft first
#define nd second
#define pb push_back
#define ll long long
#define L(node) node * 2
#define R(node) node * 2 + 1
#define LOGN 32
const int modulo = 1e9 + 7;
ll tree[4 * 500005], maks[4 * 500005], x[500005], y[500005], n;
set<int> s;
ll add(ll a, ll b){
if (a + b < modulo) return a + b;
return a + b - modulo;
}
ll mul(ll a, ll b){
return (a * b) % modulo;
}
ll fe(ll a, ll b){
if (b == 0) return 1;
if (b % 2) return mul(a, fe(a, b - 1));
int tmp = fe(a, b / 2);
return mul(tmp, tmp);
}
void build(int node, int l, int r){
if (l == r){
tree[node] = x[l];
maks[node] = y[l];
return;
}
int mid = (l + r) / 2;
build(L(node), l, mid);
build(R(node), mid + 1, r);
tree[node] = mul(tree[L(node)], tree[R(node)]);
maks[node] = max(maks[L(node)], maks[R(node)]);
}
void update_x(int node, int l, int r, int sl, int val){
if (l > r || l > sl || r < sl) return;
if (l == r){
if (l == sl) tree[node] = val;
return;
}
int mid = (l + r) / 2;
update_x(L(node), l, mid, sl, val);
update_x(R(node), mid + 1, r, sl, val);
tree[node] = mul(tree[L(node)], tree[R(node)]);
}
void update_y(int node, int l, int r, int sl, int val){
if (l > r || l > sl || r < sl) return;
if (l == r){
if (l == sl) maks[node] = val;
return;
}
int mid = (l + r) / 2;
update_y(L(node), l, mid, sl, val);
update_y(R(node), mid + 1, r, sl, val);
maks[node] = max(maks[L(node)], maks[R(node)]);
}
ll query_mul(int node, int l, int r, int sl, int sr){
if (l > r || l > sr || r < sl) return 1;
if (l >= sl && r <= sr) return tree[node];
int mid = (l + r) / 2;
return mul(query_mul(L(node), l, mid, sl, sr), query_mul(R(node), mid + 1, r, sl, sr));
}
ll query_maks(int node, int l, int r, int sl, int sr){
if (l > r || l > sr || r < sl) return 0;
if (l >= sl && r <= sr) return maks[node];
int mid = (l + r) / 2;
return max(query_maks(L(node), l, mid, sl, sr), query_maks(R(node), mid + 1, r, sl, sr));
}
int calc(void){
vector<int> v;
auto it = s.rbegin();
while(v.size() < LOGN && it != s.rend()){
v.pb(*it);
it++;
}
if (v.size() < LOGN && (v.empty() || v.back() > 1)){
v.pb(1);
}
reverse(v.begin(), v.end());
if (v.empty()){
return query_maks(1, 1, n, 1, n);
}
//cout<<query_maks(1, 1, n, 1, 2)<<endl;
int pos = 0;
for (int i = 0; i < v.size(); i++){
pos = i;
int nxt = n + 1;
if (i + 1 < v.size()) nxt = v[i + 1];
ll curr = query_maks(1, 1, n, v[pos], nxt - 1);
ll m = 1;
//cout<<"-> "<<v[pos]<<sp<<nxt<<sp<<curr<<endl;
for (int j = i + 1; j < v.size(); j++)
{
int nw = v[j];
m *= x[nw];
int nxt = n + 1;
if (j + 1 < v.size()) nxt = v[j + 1];
if (curr < m || curr < m * query_maks(1, 1, n, nw, nxt - 1)){
pos = j;
i = j - 1;
break;
}
}
if (pos == i) break;
}
int nxt = n + 1;
if (pos + 1 < v.size()) nxt = v[pos + 1];
//cout<<v[pos]<<sp<<nxt<<endl;
//cout<<query_mul(1, 1, n, 1, v[pos])<<sp<<query_maks(1, 1, n, v[pos], nxt - 1)<<endl;
return mul(query_mul(1, 1, n, 1, v[pos]), query_maks(1, 1, n, v[pos], nxt - 1));
}
int init(int N, int X[], int Y[]) {
n = N;
for (int i = 0; i < N; i++)
x[i + 1] = X[i], y[i + 1] = Y[i];
build(1, 1, n);
int it = 1;
while(it <= n){
if (x[it] != 1){
s.insert(it);
}
it++;
}
return calc();
}
int updateX(int pos, int val) {
pos++;
update_x(1, 1, n, pos, val);
if (val == 1){
s.erase(pos);
}
else s.insert(pos);
x[pos] = val;
return calc();
}
int updateY(int pos, int val) {
pos++;
update_y(1, 1, n, pos, val);
y[pos] = val;
return calc();
}
Compilation message
horses.cpp: In function 'long long int fe(long long int, long long int)':
horses.cpp:32:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
32 | int tmp = fe(a, b / 2);
| ~~^~~~~~~~~~
horses.cpp: In function 'int calc()':
horses.cpp:109:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
109 | return query_maks(1, 1, n, 1, n);
| ^
horses.cpp:109:33: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
109 | return query_maks(1, 1, n, 1, n);
| ^
horses.cpp:109:20: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
109 | return query_maks(1, 1, n, 1, n);
| ~~~~~~~~~~^~~~~~~~~~~~~~~
horses.cpp:113:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
horses.cpp:115:15: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
115 | int nxt = n + 1;
| ~~^~~
horses.cpp:116:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | if (i + 1 < v.size()) nxt = v[i + 1];
| ~~~~~~^~~~~~~~~~
horses.cpp:117:30: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
117 | ll curr = query_maks(1, 1, n, v[pos], nxt - 1);
| ^
horses.cpp:120:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int j = i + 1; j < v.size(); j++)
| ~~^~~~~~~~~~
horses.cpp:124:8: warning: declaration of 'nxt' shadows a previous local [-Wshadow]
124 | int nxt = n + 1;
| ^~~
horses.cpp:115:7: note: shadowed declaration is here
115 | int nxt = n + 1;
| ^~~
horses.cpp:124:16: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
124 | int nxt = n + 1;
| ~~^~~
horses.cpp:125:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | if (j + 1 < v.size()) nxt = v[j + 1];
| ~~~~~~^~~~~~~~~~
horses.cpp:126:48: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
126 | if (curr < m || curr < m * query_maks(1, 1, n, nw, nxt - 1)){
| ^
horses.cpp:134:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
134 | int nxt = n + 1;
| ~~^~~
horses.cpp:135:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | if (pos + 1 < v.size()) nxt = v[pos + 1];
| ~~~~~~~~^~~~~~~~~~
horses.cpp:138:29: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
138 | return mul(query_mul(1, 1, n, 1, v[pos]), query_maks(1, 1, n, v[pos], nxt - 1));
| ^
horses.cpp:138:61: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
138 | return mul(query_mul(1, 1, n, 1, v[pos]), query_maks(1, 1, n, v[pos], nxt - 1));
| ^
horses.cpp:138:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
138 | return mul(query_mul(1, 1, n, 1, v[pos]), query_maks(1, 1, n, v[pos], nxt - 1));
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:146:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
146 | build(1, 1, n);
| ^
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:159:17: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
159 | update_x(1, 1, n, pos, val);
| ^
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:171:17: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
171 | update_y(1, 1, n, pos, val);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
280 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
0 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
6 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
6 ms |
472 KB |
Output is correct |
26 |
Correct |
5 ms |
468 KB |
Output is correct |
27 |
Correct |
4 ms |
316 KB |
Output is correct |
28 |
Correct |
6 ms |
444 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
448 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
588 ms |
52944 KB |
Output is correct |
2 |
Correct |
959 ms |
52840 KB |
Output is correct |
3 |
Correct |
587 ms |
52940 KB |
Output is correct |
4 |
Correct |
895 ms |
52900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
6 ms |
320 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
5 ms |
444 KB |
Output is correct |
26 |
Correct |
5 ms |
468 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
5 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
324 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
139 ms |
32600 KB |
Output is correct |
34 |
Correct |
99 ms |
32532 KB |
Output is correct |
35 |
Correct |
212 ms |
59304 KB |
Output is correct |
36 |
Correct |
210 ms |
59344 KB |
Output is correct |
37 |
Correct |
94 ms |
30764 KB |
Output is correct |
38 |
Correct |
139 ms |
43480 KB |
Output is correct |
39 |
Correct |
35 ms |
30528 KB |
Output is correct |
40 |
Correct |
169 ms |
57836 KB |
Output is correct |
41 |
Correct |
52 ms |
30540 KB |
Output is correct |
42 |
Correct |
68 ms |
30712 KB |
Output is correct |
43 |
Correct |
127 ms |
58296 KB |
Output is correct |
44 |
Correct |
132 ms |
58432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
304 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
304 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
304 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
6 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
416 KB |
Output is correct |
25 |
Correct |
5 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
468 KB |
Output is correct |
27 |
Correct |
4 ms |
408 KB |
Output is correct |
28 |
Correct |
5 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
551 ms |
56720 KB |
Output is correct |
34 |
Correct |
944 ms |
60236 KB |
Output is correct |
35 |
Correct |
591 ms |
56604 KB |
Output is correct |
36 |
Correct |
899 ms |
60228 KB |
Output is correct |
37 |
Correct |
139 ms |
32632 KB |
Output is correct |
38 |
Correct |
99 ms |
32584 KB |
Output is correct |
39 |
Correct |
214 ms |
59260 KB |
Output is correct |
40 |
Correct |
216 ms |
59268 KB |
Output is correct |
41 |
Correct |
86 ms |
30732 KB |
Output is correct |
42 |
Correct |
139 ms |
43476 KB |
Output is correct |
43 |
Correct |
34 ms |
30488 KB |
Output is correct |
44 |
Correct |
173 ms |
57888 KB |
Output is correct |
45 |
Correct |
51 ms |
30556 KB |
Output is correct |
46 |
Correct |
64 ms |
30544 KB |
Output is correct |
47 |
Correct |
128 ms |
58208 KB |
Output is correct |
48 |
Correct |
133 ms |
58328 KB |
Output is correct |
49 |
Correct |
941 ms |
35696 KB |
Output is correct |
50 |
Correct |
750 ms |
35612 KB |
Output is correct |
51 |
Correct |
845 ms |
60280 KB |
Output is correct |
52 |
Correct |
873 ms |
60224 KB |
Output is correct |
53 |
Correct |
561 ms |
33848 KB |
Output is correct |
54 |
Correct |
798 ms |
47524 KB |
Output is correct |
55 |
Correct |
130 ms |
31620 KB |
Output is correct |
56 |
Correct |
613 ms |
59772 KB |
Output is correct |
57 |
Correct |
318 ms |
32204 KB |
Output is correct |
58 |
Correct |
452 ms |
32812 KB |
Output is correct |
59 |
Correct |
134 ms |
58248 KB |
Output is correct |