This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int OFF=(1<<19);
const int MAXN=5e5+5;
int n;
pii T[2*OFF];
int Tmax[2*OFF];
int x[MAXN], y[MAXN];
set <int> S;
int mul(int a, int b) {
return (ll)a*b%MOD;
}
pii f(pii a, pii b) {
return mp(mul(a.X, b.X), min((ll)MOD, (ll)a.Y*b.Y));
}
pii umn(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return T[pos];
if (lo>=b || hi<=a) return mp(1, 1);
int mid=(lo+hi)/2;
return f(umn(a, b, pos*2, lo, mid), umn(a, b, pos*2+1, mid, hi));
}
int maxi(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return Tmax[pos];
if (lo>=b || hi<=a) return 1;
int mid=(lo+hi)/2;
return max(maxi(a, b, pos*2, lo, mid), maxi(a, b, pos*2+1, mid, hi));
}
int mrg(int a, int b, int ya, int yb) {
if (a==n) return b;
if (b==n) return a;
int bb=f(umn(a+1, b+1), mp(yb, yb)).Y;
return bb>=ya ? b : a;
}
int answer() {
int res=n, cnt=31, yres=0;
set <int>::iterator it=S.end(); it--;
while (it!=S.begin() && cnt) {
auto nx=it;
it--;
cnt--;
int curr_y=maxi(*it, *nx);
res=mrg(*it, res, curr_y, yres);
if (res==*it) yres=curr_y;
}
return mul(umn(0, res+1).X, yres);
}
int init(int N, int XX[], int YY[]) {
n=N;
for (int i=0; i<n; ++i) x[i]=XX[i], y[i]=YY[i];
for (int i=0; i<n; ++i) if (x[i]!=1) S.insert(i);
S.insert(n); S.insert(0);
memset(Tmax, 0, sizeof Tmax);
fill(T, T+2*OFF, mp(1, 1));
for (int i=0; i<n; ++i) Tmax[OFF+i]=y[i], T[OFF+i].X=T[OFF+i].Y=x[i];
for (int i=OFF-1; i>0; --i) T[i]=f(T[i*2], T[i*2+1]), Tmax[i]=max(Tmax[i*2], Tmax[i*2+1]);
return answer();
}
int updateY(int pos, int val) {
y[pos]=val;
pos+=OFF;
Tmax[pos]=val;
pos/=2;
while (pos) {
Tmax[pos]=max(Tmax[pos*2], Tmax[pos*2+1]);
pos/=2;
}
return answer();
}
int updateX(int pos, int val) {
if (val!=1) S.insert(pos);
x[pos]=val;
if (val==1 && S.count(pos)) S.erase(pos);
S.insert(0);
pos+=OFF;
T[pos].X=T[pos].Y=val;
pos/=2;
while (pos) {
T[pos]=f(T[pos*2], T[pos*2+1]);
pos/=2;
}
return answer();
}
Compilation message (stderr)
horses.cpp: In function 'int mul(int, int)':
horses.cpp:28:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
28 | return (ll)a*b%MOD;
| ~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |