# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
522821 | Deepesson | Horses (IOI15_horses) | C++17 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
//#include "horses.h"
#define MAX 105000
typedef long long ll;
typedef std::pair<ll,bool> plb;
long long MOD = 1e9+7;
plb mul(plb a,plb b){
plb c;
c.first=a.first*b.first;
c.second=std::max(a.second,b.second);
if(c.first>=MOD){
c.second=true;
c.first%=MOD;
}
return c;
}
typedef std::pair<plb,int> ppi;
int n;
ppi mul(ppi a,ppi b){
plb c=mul(a.first,b.first);
ppi d;
d.first=c;
d.second=a.second;
return d;
}
ppi tab[MAX*4];
void update(int t,int j,int la=0,int ra=0,int pos=1){
int m = (la+ra)/2;
if(ra<t||la>t)return;
if(la==ra){
tab[pos].second=t;
tab[pos].first.first=j;
tab[pos].first.second=false;
return;
}
update(t,j,la,m,pos*2);
update(t,j,m+1,ra,(pos*2)+1);
tab[pos]=mul(tab[pos*2],tab[(pos*2)+1]);
}
ppi omega;
ppi nrmquery(int l,int r,int la=0,int ra=n-1,int pos=1){
if(l>ra||r<la)return omega;
if(l>=la&&r<=ra){
return tab[pos];
}
int m = (la+ra)/2;
return mul(nrmquery(l,r,la,m,pos*2),nrmquery(l,r,m+1,ra,(pos*2)+1));
}
ppi query(int la=0,int ra=n-1,int pos=1){
int m = (la+ra)/2;
if(!tab[pos].first.second){
return tab[pos];
}else return query(m+1,ra,(pos*2)+1);
}
typedef std::array<std::array<long long,2>,2> matriz;
matriz mul(matriz a,matriz b){
matriz c={};
for(int i=0;i!=2;++i){
for(int j=0;j!=2;++j){
for(int k=0;k!=2;++k){
///Nunca vai ter q usar o MOD, estou fazendo malandragem para isso
c[i][j]=std::max(c[i][j],a[i][k]*b[k][j]);
}
}
}
return c;
}
matriz mattab[MAX*4];
matriz neutro;
void matupdate(int t,matriz k,int la=0,int ra=n-1,int pos=1){
if(ra<t||la>t)return;
if(la==ra){
mattab[pos]=k;
return;
}
int m = (la+ra)/2;
matupdate(t,k,la,m,pos*2);
matupdate(t,k,m+1,ra,(pos*2)+1);
mattab[pos]=mul(mattab[pos*2],mattab[(pos*2)+1]);
}
matriz matquery(int l,int r,int la=0,int ra=n-1,int pos=1){
if(l>ra||r<la)return neutro;
if(l>=la&&r<=ra){
return mattab[pos];
}
int m = (la+ra)/2;
return mul(matquery(l,r,la,m,pos*2),matquery(l,r,m+1,ra,(pos*2)+1));
}
matriz gerarmat(int X,int Y){
matriz a={},b={};
a[0][0]=X;
a[1][1]=1;
b[0][0]=1;
b[1][1]=1;
b[0][1]=Y;
return mul(a,b);
}
int get(void){
ppi u = query();
int l = u.second;
if(tab[0].second)++l;
long long base=1;
if(l){
base=nrmquery(0,l-1).first.first;
}
matriz a={};
a[0][0]=base;
a=mul(a,matquery(l,n-1));
assert(a[0][1]<MOD);
return a[0][1]%MOD;
}
int x[MAX],y[MAX];
int init(int N, int X[], int Y[]) {
omega.first.first=1;
neutro[0][0]=neutro[1][1]=1;
n=N;
for(int i=0;i!=N;++i){
update(i,X[i]);
}
for(int i=0;i!=N;++i){
x[i]=X[i],y[i]=Y[i];
matupdate(i,gerarmat(X[i],Y[i]));
}
return get();
}
int updateX(int pos, int val) {
x[pos]=val;
update(pos,val);
matupdate(pos,gerarmat(x[pos],y[pos]));
return get();
}
int updateY(int pos, int val) {
y[pos]=val;
matupdate(pos,gerarmat(x[pos],y[pos]));
return get();
}
int main(){
int a[]={2,1,3},b[]={3,2,1};
std::cout<<init(3,a,b)<<"\n";
std::cout<<updateY(1,4)<<"\n";
}