// #include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007;
#define pb push_back
int n, x[500005], y[500005];
set<int, greater<int>> s;
struct node
{
long long val;
node *l, *r;
} *root1, *root2;
node* build1(int l,int r)
{
node *ptr = new node;
if(l == r)
{
ptr->val = x[l];
return ptr;
}
int mid = (l + r) / 2;
ptr->l = build1(l, mid);
ptr->r = build1(mid + 1, r);
ptr->val = ptr->l->val * ptr->r->val;
ptr->val %= mod;
return ptr;
}
node* build2(int l,int r)
{
node *ptr = new node;
if(l == r)
{
ptr->val = y[l];
return ptr;
}
int mid = (l + r) / 2;
ptr->l = build2(l, mid);
ptr->r = build2(mid + 1, r);
ptr->val = max(ptr->l->val, ptr->r->val);
return ptr;
}
int ll,rr;
long long que1(node *ptr, int l,int r)
{
if(ll <= l && r <= rr) return ptr->val;
if(r < ll || rr < l) return 1;
int mid = (l + r) / 2;
return (que1(ptr->l, l, mid) * que1(ptr->r, mid + 1, r)) % mod;
}
int que2(node *ptr, int l,int r)
{
if(ll <= l && r <= rr) return ptr->val;
if(r < ll || rr < l) return 0;
int mid = (l + r) / 2;
return max(que2(ptr->l, l, mid),que2(ptr->r, mid + 1, r));
}
int up_po,up_val;
void up1(node *ptr, int l,int r)
{
if(l == r)
{
ptr->val = up_val;
return ;
}
int mid = (l + r) / 2;
if(up_po <= mid) up1(ptr->l, l, mid);
else up1(ptr->r, mid + 1, r);
ptr->val = ptr->l->val * ptr->r->val;
ptr->val %= mod;
}
void up2(node *ptr, int l,int r)
{
if(l == r)
{
ptr->val = up_val;
return ;
}
int mid = (l + r) / 2;
if(up_po <= mid) up2(ptr->l, l, mid);
else up2(ptr->r, mid + 1, r);
ptr->val = max(ptr->l->val, ptr->r->val);
}
int cal()
{
vector<int> idx = { n };
long long val = 1;
for(int sidx : s)
{
idx.pb(sidx);
val *= x[sidx];
if (val > 1e9 + 5)
break;
}
if(val <= 1e9 + 5 && idx.back() != 0)
idx.pb(0);
reverse(idx.begin(), idx.end());
long long mx = 0;
int best = 0, ybest = 0;
for(int i = 0; i+1 < idx.size(); i++)
{
ll = idx[i];
rr = idx[i + 1] - 1;
int ymax = que2(root2, 0, n - 1);
ll = idx[1];
rr = idx[i];
long long xval = que1(root1, 0, n - 1);
if (ymax * xval > mx)
mx = ymax * xval, best = i, ybest = ymax;
}
ll = 0;
rr = idx[best];
return ((que1(root1, 0, n - 1) * ybest) ) % mod;
}
int init(int N, int X[], int Y[])
{
n = N;
for(int i = 0; i < n; i++)
x[i] = X[i], y[i] = Y[i];
root1 = build1(0, n - 1);
root2 = build2(0, n - 1);
for(int i = 0; i < n; i++)
if(x[i] != 1)
s.insert(i);
return cal();
}
int updateX(int pos, int val)
{
x[pos] = val;
up_po = pos;
up_val = val;
up1(root1, 0, n - 1);
if (val == 1) s.erase(pos);
else s.insert(pos);
return cal();
}
int updateY(int pos, int val)
{
y[pos] = val;
up_po = pos;
up_val = val;
up2(root2, 0, n - 1);
return cal();
}
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static FILE *_inputFile, *_outputFile;
static inline int _read() {
if (_charsNumber < 0) {
exit(1);
}
if (!_charsNumber || _currentChar == _charsNumber) {
_charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
_currentChar = 0;
}
if (_charsNumber <= 0) {
return -1;
}
return _buffer[_currentChar++];
}
static inline int _readInt() {
int c, x, s;
c = _read();
while (c <= 32) c = _read();
x = 0;
s = 1;
if (c == '-') {
s = -1;
c = _read();
}
while (c > 32) {
x *= 10;
x += c - '0';
c = _read();
}
if (s < 0) x = -x;
return x;
}
int main() {
_inputFile = fopen("horses.in", "rb");
_outputFile = fopen("horses.out", "w");
int N; N = _readInt();
int *X = (int*)malloc(sizeof(int)*(unsigned int)N);
int *Y = (int*)malloc(sizeof(int)*(unsigned int)N);
for (int i = 0; i < N; i++) {
X[i] = _readInt();
}
for (int i = 0; i < N; i++) {
Y[i] = _readInt();
}
fprintf(_outputFile,"%d\n",init(N,X,Y));
int M; M = _readInt();
for (int i = 0; i < M; i++) {
int type; type = _readInt();
int pos; pos = _readInt();
int val; val = _readInt();
if (type == 1) {
fprintf(_outputFile,"%d\n",updateX(pos,val));
} else if (type == 2) {
fprintf(_outputFile,"%d\n",updateY(pos,val));
}
}
return 0;
}
Compilation message
horses.cpp: In function 'int que2(node*, int, int)':
horses.cpp:59:40: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
59 | if(ll <= l && r <= rr) return ptr->val;
| ~~~~~^~~
horses.cpp: In function 'int cal()':
horses.cpp:102:7: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
102 | if (val > 1e9 + 5)
| ^~~
horses.cpp:106:5: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
106 | if(val <= 1e9 + 5 && idx.back() != 0)
| ^~~
horses.cpp:112:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i = 0; i+1 < idx.size(); i++)
| ~~~~^~~~~~~~~~~~
horses.cpp:126:44: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
126 | return ((que1(root1, 0, n - 1) * ybest) ) % mod;
| ^
horses.cpp: In function 'int _readInt()':
horses.cpp:186:12: warning: declaration of 'x' shadows a global declaration [-Wshadow]
186 | int c, x, s;
| ^
horses.cpp:8:8: note: shadowed declaration is here
8 | int n, x[500005], y[500005];
| ^
horses.cpp:186:15: warning: declaration of 's' shadows a global declaration [-Wshadow]
186 | int c, x, s;
| ^
horses.cpp:9:24: note: shadowed declaration is here
9 | set<int, greater<int>> s;
| ^
/usr/bin/ld: /tmp/ccgkrGk6.o: in function `main':
grader.c:(.text.startup+0x0): multiple definition of `main'; /tmp/ccMTxli5.o:horses.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status