#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 30
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++;
}
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<<pos<<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);
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:106:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
106 | return query_maks(1, 1, n, 1, n);
| ^
horses.cpp:106:33: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
106 | return query_maks(1, 1, n, 1, n);
| ^
horses.cpp:106:20: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
106 | return query_maks(1, 1, n, 1, n);
| ~~~~~~~~~~^~~~~~~~~~~~~~~
horses.cpp:110:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
horses.cpp:112:15: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
112 | int nxt = n + 1;
| ~~^~~
horses.cpp:113:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | if (i + 1 < v.size()) nxt = v[i + 1];
| ~~~~~~^~~~~~~~~~
horses.cpp:114:30: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
114 | ll curr = query_maks(1, 1, n, v[pos], nxt - 1);
| ^
horses.cpp:117:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (int j = i + 1; j < v.size(); j++)
| ~~^~~~~~~~~~
horses.cpp:121:8: warning: declaration of 'nxt' shadows a previous local [-Wshadow]
121 | int nxt = n + 1;
| ^~~
horses.cpp:112:7: note: shadowed declaration is here
112 | int nxt = n + 1;
| ^~~
horses.cpp:121:16: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
121 | int nxt = n + 1;
| ~~^~~
horses.cpp:122:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | if (j + 1 < v.size()) nxt = v[j + 1];
| ~~~~~~^~~~~~~~~~
horses.cpp:123:48: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
123 | if (curr < m || curr < m * query_maks(1, 1, n, nw, nxt - 1)){
| ^
horses.cpp:131:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
131 | int nxt = n + 1;
| ~~^~~
horses.cpp:132:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | if (pos + 1 < v.size()) nxt = v[pos + 1];
| ~~~~~~~~^~~~~~~~~~
horses.cpp:135:29: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
135 | return mul(query_mul(1, 1, n, 1, v[pos]), query_maks(1, 1, n, v[pos], nxt - 1));
| ^
horses.cpp:135:61: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
135 | return mul(query_mul(1, 1, n, 1, v[pos]), query_maks(1, 1, n, v[pos], nxt - 1));
| ^
horses.cpp:135:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
135 | 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:143:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
143 | build(1, 1, n);
| ^
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:156:17: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
156 | update_x(1, 1, n, pos, val);
| ^
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:168:17: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
168 | 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
5 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
5 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
468 KB |
Output is correct |
27 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
598 ms |
53176 KB |
Output is correct |
2 |
Correct |
909 ms |
65596 KB |
Output is correct |
3 |
Correct |
595 ms |
56692 KB |
Output is correct |
4 |
Correct |
853 ms |
60600 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
312 KB |
Output is correct |
23 |
Correct |
7 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
4 ms |
468 KB |
Output is correct |
26 |
Correct |
4 ms |
468 KB |
Output is correct |
27 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
296 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
5 ms |
320 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
4 ms |
468 KB |
Output is correct |
26 |
Correct |
5 ms |
468 KB |
Output is correct |
27 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |