有没有能帮忙看下的,实在卡太久了-qwq-
对拍一直没找到bug,CF上显示WA在几百行错误的输出了-1,不知道哪里特判错了
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int mem[maxn][3],a[maxn],vis[maxn],here;
int N;
bool is(int n)
{
if(n > N || n <= 0) return true;
return false;
}
void dfs(int x,int y,int w,bool iss)
{
if(iss)
{
y += a[x];
x += a[x];
vis[1] = here;
if(is(x))
{
cout<<y<<endl;
return ;
}
dfs(x,y,2,false);
return ;
}
if(vis[x] == here)
{
cout<<-1<<endl;
return ;
}
vis[x] = here;
if(w == 1)
{
if(mem[x][1])
{
if(mem[x][1] == -1) cout<<"-1\n";
else cout<<y+mem[x][1]<<endl;
return ;
}
int orign = x;
y+=a[x];
x+=a[x];
if(is(x))
{
mem[orign][1] = a[orign];
cout<<y<<endl;
return ;
}
bool la = true;
if(vis[x] == here) la = false,mem[orign][1] = -1;
dfs(x,y,2,false);
if(mem[x][2] == -1) mem[orign][1] = -1;
else if(la) mem[orign][1] = a[orign] + mem[x][2];
}
else
{
if(mem[x][2])
{
if(mem[x][2] == -1) cout<<"-1\n";
else cout<<y + mem[x][2]<<endl;
return ;
}
int orign = x;
y+=a[x];
x-=a[x];
if(is(x))
{
mem[orign][2] = a[orign];
cout<<y<<endl;
return ;
}
bool la = true;
if(vis[x] == here) la=false,mem[orign][2] = -1;
dfs(x,y,1,false);
if(mem[x][1] == -1) mem[orign][2] = -1;
else if(la) mem[orign][2] = a[orign] + mem[x][1];
}
return ;
}
int main()
{
cin>>N;
for(int i=2;i<=N;i++)
{
cin>>a[i];
}
for(int i=1;i<N;i++)
{
here = i;
a[1] = i;
dfs(1,0,1,true);
}
return 0;
}