#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.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 |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
584 ms |
52860 KB |
Output is correct |
2 |
Correct |
1055 ms |
53196 KB |
Output is correct |
3 |
Correct |
635 ms |
55192 KB |
Output is correct |
4 |
Correct |
920 ms |
55340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |