結果

問題 No.2704 L to R Graph
ユーザー maksimmaksim
提出日時 2024-03-29 22:28:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,808 bytes
コンパイル時間 1,894 ms
コンパイル使用メモリ 180,716 KB
実行使用メモリ 25,356 KB
最終ジャッジ日時 2024-03-29 22:29:47
合計ジャッジ時間 7,175 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
25,356 KB
testcase_01 AC 6 ms
18,552 KB
testcase_02 AC 6 ms
20,600 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 8 ms
20,612 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define app push_back
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
#else
#define debug(...)
#endif
#ifdef LOCAL
#define __int128 long long
#endif // LOCAL
const int inf=1e18;
const int maxn=2e5+5;
int a[maxn];int col[maxn];bool used[maxn];int h[maxn];int de[maxn];int u[maxn][20];bool cy[maxn];int di[maxn];
int me[maxn];
int mo[maxn];
int f[maxn];
vector<int> ps;
vector<int> mem[maxn];
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,l,r;cin>>n>>l>>r;
    set<int> unu;
    f[0]=0;fill(f+1,f+maxn,inf);
    for(int i=0;i<=n;++i)
    {
        for(int j=l;j<=r;++j)
        {
            f[i+j]=min(f[i+j],f[i]+1);
        }
    }
    for(int i=1;i<=n;++i) {cin>>a[i];u[i][0]=a[i];}
    for(int j=1;j<20;++j) {for(int i=1;i<=n;++i) {u[i][j]=u[u[i][j-1]][j-1];}}
    for(int i=1;i<=n;++i) {cy[u[i][19]]=true;}
    for(int i=1;i<=n;++i)
    {
        if(cy[i] && !used[i])
        {
            int x=i;int c=0;
            while(true)
            {
                col[x]=i;
                h[x]=c;
                ++c;
                x=a[x];
                if(x==i) break;
            }
            de[i]=c;
            ps.app(de[i]);
        }
    }
    for(int i=1;i<=n;++i)
    {
        if(cy[i]) {me[i]=i;continue;}
        di[i]=0;
        int x=i;
        for(int j=19;j>=0;--j)
        {
            if(!cy[u[x][j]])
            {
                di[i]+=(1<<j);x=u[x][j];
            }
        }
        me[i]=a[x];
        di[i]++;
        mo[de[col[me[i]]]]=max(mo[de[col[me[i]]]],di[i]);
    }
    sort(all(ps));ps.erase(unique(all(ps)),ps.end());
    auto ok=[&](int p,int x)->bool
    {
        return ((l+2*p-1-x)/p!=(r+2*p-x)/p);
    };
    for(int p:ps)
    {
        vector<int> ans(mo[p]+p+1);fill(all(ans),inf);ans[0]=0;
        for(int k=0;k<ans.size();++k){
        for(int i=0;i<ans.size();++i)
        {
            for(int j=l;j<=r;++j)
            {
                int x=i+j;
                while(x>=ans.size()) x-=p;
                ans[x]=min(ans[x],ans[i]+1);
            }
        }
        }
        for(int i=0;i<ans.size()-p;++i) {ans[i]=min(ans[i+p],ans[i]);}
        mem[p]=ans;
    }
    for(int i=1;i<=n;++i)
    {
        int di1=di[i];
        di[i]=0;
        int x=i;
        while(!cy[x]) {++di[i];x=a[x];}
        assert(di1==di[i]);
        assert(x==me[i]);
    }
    auto up=[&](int x,int h)
    {
        for(int i=19;i>=0;--i)
        {
            if(h & (1<<i))
            {
                x=u[x][i];
            }
        }
        return x;
    };
    int q1;cin>>q1;
    while(q1--)
    {
        int x,y;cin>>x>>y;
        if(col[me[x]]!=col[me[y]])
        {
            cout<<(-1)<<'\n';
            continue;
        }
        if(!cy[y])
        {
            if(di[x]<di[y])
            {
                cout<<(-1)<<'\n';
                continue;
            }
            if(x==y)
            {
                cout<<0<<'\n';continue;
            }
            int h=di[x]-di[y];
            if(up(x,h)!=y)
            {
                cout<<(-1)<<'\n';continue;
            }
            cout<<f[h]<<'\n';
            continue;
        }
        int p=de[col[me[x]]];
        int o=((h[y]-h[me[x]])%p+p)%p;
        o+=di[x];
        if(o<mem[p].size())
        {
            cout<<(mem[p][o]==inf ? -1 : mem[p][o])<<'\n';
        }
        else
        {
            for(int i=20;i>=0;--i)
            {
                if(o-p*(1<<i)>=((int) (mem[p].size()))) {o-=p*(1<<i);}
            }
            o-=p;
            cout<<(mem[p][o]==inf ? -1 : mem[p][o])<<'\n';
        }
    }
    return 0;
}
/*
2 101 101
2 1
1
1 2
*/
0