Submission #1236669

#TimeUsernameProblemLanguageResultExecution timeMemory
1236669Zbyszek99Horses (IOI15_horses)C++20
100 / 100
490 ms48480 KiB
#include "horses.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

const int tree_siz = 1024*1024-1;
ll drzewo[tree_siz+1];
ll drzewo2[tree_siz+1];

ll get_seg(int akt, int p1, int p2, int s1, int s2)
{
	if(p2 < s1 || p1 > s2) return 1;
	if(p1 >= s1 && p2 <= s2) return drzewo[akt];
	return (get_seg(akt*2,p1,(p1+p2)/2,s1,s2) * get_seg(akt*2+1,(p1+p2)/2+1,p2,s1,s2)) % MOD;
}

void upd(int v)
{
	drzewo[v] = (drzewo[v*2] * drzewo[v*2+1]) % MOD;
	if(v != 1) upd(v/2);
}

void changeX(int ind, ll x)
{
	drzewo[tree_siz/2+ind+1] = x;
	upd((tree_siz/2+ind+1)/2);
}

ll get_max(int akt, int p1, int p2, int s1, int s2)
{
	if(p2 < s1 || p1 > s2) return 0;
	if(p1 >= s1 && p2 <= s2) return drzewo2[akt];
	return max(get_max(akt*2,p1,(p1+p2)/2,s1,s2),get_max(akt*2+1,(p1+p2)/2+1,p2,s1,s2));
}

void upd2(int v)
{
	drzewo2[v] = max(drzewo2[v*2],drzewo2[v*2+1]);
	if(v != 1) upd2(v/2);
}

void changeY(int ind, ll x)
{
	drzewo2[tree_siz/2+ind+1] = x;
	upd2((tree_siz/2+ind+1)/2);
}

ll X[500001];
set<int> non_one_X;
int n;
vi sufs;

int calc()
{
	if(siz(non_one_X) == 0)
	{
		return (int)get_max(1,0,tree_siz/2,0,n-1);
	}
	sufs = {};
	__int128_t ans = 0;
	ll cur_mul = 1;
	forall(it,non_one_X)
	{
		cur_mul *= X[-it];
		sufs.pb(-it);
		if(cur_mul > (ll)1e9) break;
	}
	reverse(all(sufs));
	cur_mul = 1;
	forall(it,sufs)
	{
		cur_mul *= X[it];
		ans = max(ans,(__int128_t)cur_mul * (__int128_t)get_max(1,0,tree_siz/2,it,n-1));
	}
	if(sufs[0] != 0)
	{
		ans *= get_seg(1,0,tree_siz/2,0,sufs[0]-1);
	}
	if(cur_mul <= 1e9)
	{
		ans = max(ans,(__int128_t)get_max(1,0,tree_siz/2,0,n-1));
	}
	ans %= MOD;
	return (int)ans;
}

int init(int N, int X2[], int Y[]) 
{
	n = N;
	rep(i,n)
	{
		X[i] = X2[i];
		changeX(i,X[i]);
		changeY(i,Y[i]);
		if(X[i] != 1)
		{
			non_one_X.insert(-i);
		}
	}
	return calc();
}

int updateX(int pos, int val) 
{	
	if(X[pos] > 1) non_one_X.erase(non_one_X.find(-pos));
	X[pos] = val;
	if(val > 1) non_one_X.insert(-pos);
	changeX(pos,X[pos]);
	return calc();
}

int updateY(int pos, int val) 
{
	changeY(pos,val);
	return calc();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...