to sum up
Mid due to impact, did not cut within the game time \ (T3 \)
Difficulty thinking \ (Tl <T2 <T3 \) , Code difficulty \ (T1> T2> T3 \ )
P5557 Travel
Obviously after the jump ring up direct mode, so the experience over and over again to find whether each point on the ring
As determined on the ring on the ring \ (len \) , determined as not need to go to the loop length \ (Len \) , each point of the pre-walking \ (2 ^ i \) a position
\ (t1 ^ t2 \) are too large can not be determined directly, is obtained in the rapid determination of the power is greater than \ (Len \)
Is larger than the first record, the other come to the ring, the final position on the fall distance of rings of the ring header \ (x \ equiv t1 ^ t2 -Len (mod ~ len) \)
Description less than the number is not large, doubling to go directly
#include<bits/stdc++.h>
typedef long long LL;
const LL maxn=400009;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
}return x*f;
}
LL n,m,top,tim;
LL a[maxn],f[maxn][21],seed[21],visit[maxn],len[maxn],sta[maxn],dep[maxn],Len[maxn],dfn[maxn];
inline LL Pow(LL base,LL b,LL mod){
LL ret(1);
while(b){
if(b&1) ret=1ll*ret*base%mod;
base=1ll*base*base%mod; b>>=1;
}return ret;
}
void Dfs1(LL u){
sta[++top]=u; dfn[u]=++tim;
LL v(a[u]);
visit[u]=1;
if(visit[v] && !Len[v]){
len[u]=dep[u]-dep[v]+1;
LL now;
do{
now=sta[top--]; len[now]=len[u]; visit[now]=false;
}while(now!=v);
return;
}
dep[v]=dep[u]+1;
if(!dfn[v]) Dfs1(v);
if(!len[u]){
Len[u]=Len[v]+1;
}
}
int main(){
n=Read();
for(LL i=1;i<=n;++i) a[i]=Read(),f[i][0]=a[i];
for(LL i=1;i<=n;++i){
if(!dfn[i]) dep[i]=0,Dfs1(i);
}
for(LL j=1;j<=20;++j)
for(LL i=1;i<=n;++i)
f[i][j]=f[f[i][j-1]][j-1];
seed[0]=1;
for(LL i=1;i<=20;++i) seed[i]=seed[i-1]*2;
m=Read();
while(m--){
LL s(Read()),t1(Read()),t2(Read()),now1(s);
if(Len[s]){
LL base(t1),b(t2),R(1),flag(0),tmp(Len[s]);
while(b){
if(b&1){
R=R*base;
if(R>tmp){
flag=true; break;
}
} base=base*base;
if(b!=1){
if(base>tmp){
flag=1; break;
}
}
b>>=1;
}
if(!flag){
tmp=R;
for(LL i=20;i>=0;--i){
if(tmp>=seed[i]){
tmp-=seed[i]; now1=f[now1][i];
}
if(!tmp){
printf("%lld\n",now1); break;
}
}
continue;
}else{
for(LL i=20;i>=0;--i){
if(tmp>=seed[i]){
tmp-=seed[i]; now1=f[now1][i];
}
}
}
}
LL ret,now(now1);
ret=(Pow(t1,t2,len[now])-Len[s]%len[now]+len[now])%len[now];、
if(!ret){
printf("%lld\n",now); continue;
}
for(LL j=20;j>=0;--j){
if(ret>=seed[j])
ret-=seed[j],now=f[now][j];
if(!ret){
printf("%lld\n",now); break;
}
}
}
return 0;
}
P5558 heart autumn
Found side is positive and less than the constant color \ (5 \) , determined \ (inc_ {i, j, c1, c2}, low_ {i, j, c1, c2} \) are \ (I \) to \ (I \) a \ (2 ^ j \) level ancestor sequence header \ (C1 \) tail \ (C2 \) , - decreasing / increasing monotonically, the longest length
For each query, to \ (x-lca \) to \ (inc \) together, to \ (y-lca \) to \ (low \) together, and then in the \ (dp \) it again
\ (O (5 ^ 4 (n-Q +) logN) \) , the time since the transfer \ (^ 5 4 \) card dissatisfaction, it does not run slow
#include<bits/stdc++.h>
typedef int LL;
const LL maxn=30009;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
}return x*f;
}
struct node{
LL to,nxt,w;
}dis[maxn<<1];
LL n,m,num;
LL head[maxn],f1[6][6],f2[6][6],inc[maxn][16][6][6],low[maxn][16][6][6],F[maxn][16],dep[maxn],tmp[6][6];
inline void Add(LL u,LL v,LL w){
dis[++num]=(node){v,head[u],w}; head[u]=num;
}
void Dfs(LL u,LL f,LL c){
if(u!=1){
inc[u][0][c][c]=1; low[u][0][c][c]=1;
F[u][0]=f; for(LL i=1;i<=15;++i) F[u][i]=F[F[u][i-1]][i-1];
for(LL i=1;i<=15;++i){
for(LL j=1;j<=5;++j)
for(LL k=j;k<=5;++k)
for(LL jj=j;jj<=k;++jj)
for(LL kk=jj;kk<=k;++kk)
inc[u][i][j][k]=std::max(inc[u][i][j][k],inc[u][i-1][j][jj]+inc[F[u][i-1]][i-1][kk][k]);
}
for(LL i=1;i<=15;++i){
for(LL j=1;j<=5;++j)
for(LL k=1;k<=j;++k)
for(LL jj=k;jj<=j;++jj)
for(LL kk=k;kk<=jj;++kk)
low[u][i][j][k]=std::max(low[u][i][j][k],low[u][i-1][j][jj]+low[F[u][i-1]][i-1][kk][k]);
}
}
for(LL i=head[u];i;i=dis[i].nxt){
LL v(dis[i].to);
if(v==f) continue;
dep[v]=dep[u]+1;
Dfs(v,u,dis[i].w);
}
}
inline LL Lca(LL u,LL v){
if(dep[u]<dep[v]) std::swap(u,v);
for(LL i=15;i>=0;--i)
if(dep[F[u][i]]>=dep[v]) u=F[u][i];
if(u==v) return u;
for(LL i=15;i>=0;--i)
if(F[u][i]!=F[v][i]) u=F[u][i],v=F[v][i];
return F[u][0];
}
inline void Solve1(LL u,LL f){
memset(f1,0,sizeof(f1));
LL ret(dep[u]-dep[f]);
for(LL i=15;i>=0;--i){
if((ret&(1<<i))==(1<<i)){
memcpy(tmp,f1,sizeof(f1));
memset(f1,0,sizeof(f1));
for(LL j=1;j<=5;++j)
for(LL k=j;k<=5;++k)
for(LL jj=j;jj<=k;++jj)
for(LL kk=jj;kk<=k;++kk)
f1[j][k]=std::max(f1[j][k],tmp[j][jj]+inc[u][i][kk][k]);
u=F[u][i];
}
}
}
inline void Solve2(LL u,LL f){
memset(f2,0,sizeof(f2));
LL ret(dep[u]-dep[f]);
for(LL i=15;i>=0;--i){
if((ret&(1<<i))==(1<<i)){
memcpy(tmp,f2,sizeof(f2));
memset(f2,0,sizeof(f2));
for(LL j=1;j<=5;++j)
for(LL k=1;k<=j;++k)
for(LL jj=k;jj<=j;++jj)
for(LL kk=k;kk<=jj;++kk)
f2[j][k]=std::max(f2[j][k],tmp[j][jj]+low[u][i][kk][k]);
u=F[u][i];
}
}
}
int main(){
n=Read();
for(LL i=1;i<n;++i){
LL u(Read()),v(Read()),w(Read());
Add(u,v,w); Add(v,u,w);
}
Dfs(1,0,0);
m=Read();
while(m--){
LL u(Read()),v(Read());
LL lca(Lca(u,v));
Solve1(u,lca); Solve2(v,lca);
LL ans(0);
for(LL i=1;i<=5;++i)
for(LL j=i;j<=5;++j)
for(LL ii=i;ii<=j;++ii)
for(LL jj=ii;jj<=j;++jj)
ans=std::max(ans,f1[i][ii]+f2[j][jj]);
printf("%d\n",ans);
}
return 0;
}
P5559 lost defensive star of the day the city
The meaning of problems: there are trees right side, the right starting point are \ (1 \) , each time taking a little right of the trans / a given point \ ((u, v) \ ) required for each point in the chain to its