Submission #522826

#TimeUsernameProblemLanguageResultExecution timeMemory
522826DeepessonHorses (IOI15_horses)C++17
17 / 100
13 ms8412 KiB
#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,ll> ppi;
ll 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=n-1,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 {
        if(!tab[(pos*2)+1].first.second){
            return query(la,m,pos*2);
        }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])%MOD);
            }
        }
    }
    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();
    ll l = u.second;
    if(tab[1].first.second)++l;
    long long base=1;
    if(l){
        auto u = nrmquery(0,l-1);
        base=u.first.first;
    }
    matriz a={};
    a[0][0]=base;
    a=mul(a,matquery(l,n-1));
    return a[0][1]%MOD;
}
ll 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();
}

Compilation message (stderr)

horses.cpp:29:42: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   29 | void update(int t,int j,int la=0,int ra=n-1,int pos=1){
      |                                         ~^~
horses.cpp:43:43: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   43 | ppi nrmquery(int l,int r,int la=0,int ra=n-1,int pos=1){
      |                                          ~^~
horses.cpp:52:28: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   52 | ppi query(int la=0,int ra=n-1,int pos=1){
      |                           ~^~
horses.cpp:78:48: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   78 | void matupdate(int t,matriz k,int la=0,int ra=n-1,int pos=1){
      |                                               ~^~
horses.cpp:89:46: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   89 | matriz matquery(int l,int r,int la=0,int ra=n-1,int pos=1){
      |                                             ~^~
horses.cpp: In function 'int get()':
horses.cpp:107:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  107 |     ppi u = query();
      |                   ^
horses.cpp:112:14: warning: declaration of 'u' shadows a previous local [-Wshadow]
  112 |         auto u = nrmquery(0,l-1);
      |              ^
horses.cpp:107:9: note: shadowed declaration is here
  107 |     ppi u = query();
      |         ^
horses.cpp:112:30: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  112 |         auto u = nrmquery(0,l-1);
      |                             ~^~
horses.cpp:112:32: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  112 |         auto u = nrmquery(0,l-1);
      |                                ^
horses.cpp:117:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  117 |     a=mul(a,matquery(l,n-1));
      |                      ^
horses.cpp:117:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  117 |     a=mul(a,matquery(l,n-1));
      |                        ~^~
horses.cpp:117:27: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  117 |     a=mul(a,matquery(l,n-1));
      |                           ^
horses.cpp:118:19: warning: conversion from 'std::array<long long int, 2>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
  118 |     return a[0][1]%MOD;
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:126:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  126 |         update(i,X[i]);
      |                      ^
horses.cpp:130:40: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  130 |         matupdate(i,gerarmat(X[i],Y[i]));
      |                                        ^
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:137:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  137 |     update(pos,val);
      |                   ^
horses.cpp:138:33: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  138 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                            ~~~~~^
horses.cpp:138:40: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  138 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                                   ~~~~~^
horses.cpp:138:42: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  138 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                                          ^
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:144:33: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  144 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                            ~~~~~^
horses.cpp:144:40: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  144 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                                   ~~~~~^
horses.cpp:144:42: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  144 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                                          ^
#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...