#include "horses.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
const ll mod = 1e9 + 7;
ll x[maxn], y[maxn], n;
ll prod_tree[4 * maxn];
void build_prod_tree(int root, int left, int right)
{
if (left == right)
{
prod_tree[root] = x[left];
return;
}
int mid = (left + right) / 2;
build_prod_tree(root * 2, left, mid);
build_prod_tree(root * 2 + 1, mid + 1, right);
prod_tree[root] = (prod_tree[root * 2] * prod_tree[root * 2 + 1]) % mod;
}
void update_prod(int root, int left, int right, int pos)
{
if (left == right)
{
prod_tree[root] = x[left];
return;
}
int mid = (left + right) / 2;
if (pos <= mid)
update_prod(root * 2, left, mid, pos);
else
update_prod(root * 2 + 1, mid + 1, right, pos);
prod_tree[root] = (prod_tree[root * 2] * prod_tree[root * 2 + 1]) % mod;
}
ll prod_query(int root, int left, int right, int qleft, int qright)
{
if (left > qright || right < qleft)
return 1;
if (left >= qleft && right <= qright)
return prod_tree[root];
int mid = (left + right) / 2;
return (prod_query(root * 2, left, mid, qleft, qright) *
prod_query(root * 2 + 1, mid + 1, right, qleft, qright)) % mod;
}
ll breed_tree[4 * maxn];
void build_breed_tree(int root, int left, int right)
{
if (left == right)
{
breed_tree[root] = y[left];
return;
}
int mid = (left + right) / 2;
build_breed_tree(root * 2, left, mid);
build_breed_tree(root * 2 + 1, mid + 1, right);
breed_tree[root] = max(breed_tree[root * 2], breed_tree[root * 2 + 1]);
}
void update_breed(int root, int left, int right, int pos)
{
if (left == right)
{
breed_tree[root] = y[left];
return;
}
int mid = (left + right) / 2;
if (pos <= mid)
update_breed(root * 2, left, mid, pos);
else
update_breed(root * 2 + 1, mid + 1, right, pos);
breed_tree[root] = max(breed_tree[root * 2], breed_tree[root * 2 + 1]);
}
ll breed_query(int root, int left, int right, int qleft, int qright)
{
if (left > qright || right < qleft)
return 0;
if (left >= qleft && right <= qright)
return breed_tree[root];
int mid = (left + right) / 2;
return max(breed_query(root * 2, left, mid, qleft, qright),
breed_query(root * 2 + 1, mid + 1, right, qleft, qright));
}
set < int > active;
ll action()
{
ll up = y[n - 1], dw = 1, pos = n - 1;
ll div = 1;
set < int > :: reverse_iterator it;
if (*active.begin() != 0)
active.insert(0);
for (it = active.rbegin(); it != active.rend(); it ++)
{
ll cur = breed_query(1, 0, n - 1, *it, n - 1);
if (div > 1e9)
break;
///cout << *it << " " << cur << " " << div << endl;
if (cur * dw >= up * div)
{
up = cur;
dw = div;
pos = *it;
}
div = div * x[*it];
}
/**for (int i = n - 2; i >= 0; i --)
{
div = div * x[i + 1];
if (div > 1e9)
break;
///cout << i << " : " << y[i] << " " << div << endl;
/// y[i] / div >? up / dw
if (y[i] * dw >= up * div)
{
///cout << " " << up << " : " << dw << endl;
up = y[i];
dw = div;
pos = i;
}
}*/
///cout << pos << endl;
ll ans = prod_query(1, 0, n - 1, 0, pos);
//for (int i = 0; i <= pos; i ++)
// ans = (ans * x[i]) % mod;
ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
return ans;
}
int init(int N, int X[], int Y[])
{
n = N;
for (int i = 0; i < n; i ++)
{
if (X[i] > 1)
active.insert(i);
x[i] = X[i];
y[i] = Y[i];
}
build_prod_tree(1, 0, n - 1);
build_breed_tree(1, 0, n - 1);
return action();
}
int updateX(int pos, int val)
{
if (x[pos] > 1 && pos != 0)
active.erase(pos);
x[pos] = val;
if (x[pos] > 1 && pos != 0)
active.insert(pos);
update_prod(1, 0, n - 1, pos);
return action();
}
int updateY(int pos, int val)
{
y[pos] = val;
update_breed(1, 0, n - 1, pos);
return action();
}
Compilation message
horses.cpp: In function 'll action()':
horses.cpp:115:38: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
115 | ll cur = breed_query(1, 0, n - 1, *it, n - 1);
| ~~^~~
horses.cpp:115:50: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
115 | ll cur = breed_query(1, 0, n - 1, *it, n - 1);
| ~~^~~
horses.cpp:116:13: warning: conversion from 'll' {aka 'long long int'} to 'double' may change value [-Wconversion]
116 | if (div > 1e9)
| ^~~
horses.cpp:146:33: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
146 | ll ans = prod_query(1, 0, n - 1, 0, pos);
| ~~^~~
horses.cpp:146:41: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
146 | ll ans = prod_query(1, 0, n - 1, 0, pos);
| ^~~
horses.cpp:149:38: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
149 | ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
| ~~^~~
horses.cpp:149:43: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
149 | ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
| ^~~
horses.cpp:149:50: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
149 | ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
| ~~^~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:162:29: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
162 | build_prod_tree(1, 0, n - 1);
| ~~^~~
horses.cpp:163:30: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
163 | build_breed_tree(1, 0, n - 1);
| ~~^~~
horses.cpp:164:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
164 | return action();
| ~~~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:174:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
174 | update_prod(1, 0, n - 1, pos);
| ~~^~~
horses.cpp:175:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
175 | return action();
| ~~~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:181:26: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
181 | update_breed(1, 0, n - 1, pos);
| ~~^~~
horses.cpp:182:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
182 | return action();
| ~~~~~~^~
# |
Verdict |
Execution time |
Memory |
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 |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
518 ms |
52944 KB |
Output is correct |
2 |
Correct |
317 ms |
52864 KB |
Output is correct |
3 |
Correct |
291 ms |
52924 KB |
Output is correct |
4 |
Correct |
372 ms |
52904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |