Submission #72031170


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int n,q,a[N],vis[N],bel[N],pos[N],len[N],cnt;
int cyc_sum[N];
vector<int>v[N];
void dfs(int u){
    if(vis[u]){
        if(!bel[u]){
            cnt++;
            int x=u;
            int sz=0;
            vector<int>tmp;
            do{
                tmp.push_back(x);
                bel[x]=cnt;
                pos[x]=sz++;
                x=a[x];
            }while(x!=u);
            len[cnt]=sz;
            v[cnt]=tmp;
            
            for(auto val:tmp)cyc_sum[cnt]+=val;
        }
        return;
    }
    vis[u]=1;
    dfs(a[u]);
    if(!bel[u]){
        bel[u]=bel[a[u]];
        pos[u]=pos[a[u]]-1;
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)if(!vis[i])dfs(i);
    for(int i=1;i<=q;i++){
        int T,B;
        cin>>T>>B;
        int cur=B,ans=0,steps=0;
        while(pos[cur]<0 && steps<T){
            ans+=cur;
            cur=a[cur];
            steps++;
        }
        T-=steps;
        if(T>0){
            int c=bel[cur];
            int p=pos[cur];
            int l=len[c];
            int full_cycles=T/l;
            ans+=full_cycles*cyc_sum[c];
            int rem=T%l;
            for(int j=0;j<rem;j++){
                int person=v[c][(p+j)%l];
                ans+=person;
            }
        }
        
        cout<<ans<<'\n';
    }
    return 0;
}

Submission Info

Submission Time
Task E - Heavy Buckets
User kac17
Language C++23 (GCC 15.2.0)
Score 0
Code Size 1566 Byte
Status WA
Exec Time > 3000 ms
Memory 31648 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 475
Status
AC × 1
AC × 13
WA × 14
TLE × 8
Set Name Test Cases
Sample sample00.txt
All sample00.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt
Case Name Status Exec Time Memory
sample00.txt AC 2 ms 3724 KiB
testcase00.txt AC 7 ms 3736 KiB
testcase01.txt AC 20 ms 3716 KiB
testcase02.txt AC 10 ms 3764 KiB
testcase03.txt AC 22 ms 3728 KiB
testcase04.txt WA 191 ms 6788 KiB
testcase05.txt WA 417 ms 9984 KiB
testcase06.txt WA 94 ms 4824 KiB
testcase07.txt WA 583 ms 9908 KiB
testcase08.txt WA 34 ms 4804 KiB
testcase09.txt WA 509 ms 10012 KiB
testcase10.txt WA 217 ms 7576 KiB
testcase11.txt WA 450 ms 9956 KiB
testcase12.txt WA 20 ms 7748 KiB
testcase13.txt WA 576 ms 10000 KiB
testcase14.txt WA 324 ms 9176 KiB
testcase15.txt WA 658 ms 9840 KiB
testcase16.txt WA 49 ms 6872 KiB
testcase17.txt WA 467 ms 9988 KiB
testcase18.txt TLE > 3000 ms 22204 KiB
testcase19.txt TLE > 3000 ms 31560 KiB
testcase20.txt TLE > 3000 ms 11852 KiB
testcase21.txt TLE > 3000 ms 31648 KiB
testcase22.txt TLE > 3000 ms 16668 KiB
testcase23.txt TLE > 3000 ms 31600 KiB
testcase24.txt AC 595 ms 4464 KiB
testcase25.txt TLE > 3000 ms 19960 KiB
testcase26.txt AC 16 ms 4020 KiB
testcase27.txt TLE > 3000 ms 22600 KiB
testcase28.txt AC 33 ms 23644 KiB
testcase29.txt AC 60 ms 23936 KiB
testcase30.txt AC 25 ms 8196 KiB
testcase31.txt AC 38 ms 9860 KiB
testcase32.txt AC 17 ms 5724 KiB
testcase33.txt AC 55 ms 9848 KiB