Submission #522838

#TimeUsernameProblemLanguageResultExecution timeMemory
522838DeepessonHorses (IOI15_horses)C++17
100 / 100
1002 ms83004 KiB
#include <bits/stdc++.h> #include "horses.h" #define MAX 505000 typedef long long ll; typedef std::pair<ll,int> plb; long long MOD = 1e9+7; plb mul(plb a,plb b){ plb c; c.first=a.first*b.first; c.second=a.second+b.second; if(c.first>=MOD){ c.second++; 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)); } int query(int x){ int l=0,r=x; while(l<r){ int m = (l+r)/2; if(nrmquery(m,x).first.second){ l=m+1; }else r=m; } return l; } 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); } ll x[MAX],y[MAX]; int get(int x=n-1){ int l = query(x); long long base=1; if(l){ auto u = nrmquery(0,l-1); base=u.first.first; } matriz a={}; a[0][0]=base; matriz b = matquery(l,n-1); if(l){ matriz g={};g[0][0]=1; matriz k=mul(g,b); long long rate = std::max(k[0][1],y[l-1]); rate%=MOD; return (rate*base)%MOD; } a=mul(a,b); return a[0][1]%MOD; } 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: In function 'int query(int)':
horses.cpp:56:24: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   56 |         if(nrmquery(m,x).first.second){
      |                        ^
horses.cpp: At global scope:
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:107:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  107 | int get(int x=n-1){
      |               ~^~
horses.cpp: In function 'int get(int)':
horses.cpp:107:13: warning: declaration of 'x' shadows a global declaration [-Wshadow]
  107 | int get(int x=n-1){
      |         ~~~~^~~~~
horses.cpp:106:4: note: shadowed declaration is here
  106 | ll x[MAX],y[MAX];
      |    ^
horses.cpp:111:32: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  111 |         auto u = nrmquery(0,l-1);
      |                                ^
horses.cpp:116:28: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  116 |     matriz b = matquery(l,n-1);
      |                           ~^~
horses.cpp:116:30: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  116 |     matriz b = matquery(l,n-1);
      |                              ^
horses.cpp:122:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  122 |         return (rate*base)%MOD;
      |                ~~~~~~~~~~~^~~~
horses.cpp:125:19: warning: conversion from 'std::array<long long int, 2>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
  125 |     return a[0][1]%MOD;
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:132:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  132 |         update(i,X[i]);
      |                      ^
horses.cpp:136:40: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  136 |         matupdate(i,gerarmat(X[i],Y[i]));
      |                                        ^
horses.cpp:138:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  138 |  return get();
      |             ^
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:143:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  143 |     update(pos,val);
      |                   ^
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]));
      |                                          ^
horses.cpp:145:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  145 |  return get();
      |             ^
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:150:33: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  150 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                            ~~~~~^
horses.cpp:150:40: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  150 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                                   ~~~~~^
horses.cpp:150:42: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  150 |     matupdate(pos,gerarmat(x[pos],y[pos]));
      |                                          ^
horses.cpp:151:13: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  151 |  return get();
      |             ^
#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...