結果

問題 No.1097 Remainder Operation
ユーザー snrnsidysnrnsidy
提出日時 2021-10-04 18:31:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,184 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 206,640 KB
実行使用メモリ 12,700 KB
最終ジャッジ日時 2023-09-30 01:02:23
合計ジャッジ時間 4,922 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,104 KB
testcase_01 AC 3 ms
8,072 KB
testcase_02 AC 3 ms
8,192 KB
testcase_03 AC 3 ms
8,168 KB
testcase_04 AC 3 ms
8,112 KB
testcase_05 AC 3 ms
8,320 KB
testcase_06 AC 3 ms
8,080 KB
testcase_07 AC 7 ms
8,528 KB
testcase_08 AC 7 ms
8,516 KB
testcase_09 AC 7 ms
8,512 KB
testcase_10 AC 7 ms
8,524 KB
testcase_11 AC 7 ms
8,572 KB
testcase_12 AC 44 ms
11,628 KB
testcase_13 AC 44 ms
11,724 KB
testcase_14 AC 45 ms
11,692 KB
testcase_15 AC 44 ms
11,628 KB
testcase_16 AC 44 ms
11,696 KB
testcase_17 AC 46 ms
11,692 KB
testcase_18 AC 43 ms
12,596 KB
testcase_19 AC 43 ms
12,632 KB
testcase_20 AC 43 ms
12,600 KB
testcase_21 AC 42 ms
12,700 KB
testcase_22 AC 43 ms
12,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

vector <int> adj[100005];
int A[100005];
vector <long long int> q;
int n,m;
long long int t;
int history[100005];
long long int visited[100005][2];
long long int Left[100005];
long long int Right[100005];
int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  for(int i=0;i<n;i++)
  {
    cin >> A[i];
    adj[i].push_back((i + A[i])%n);
  }

  cin >> m;

  for(int i=0;i<m;i++)
  {
    cin >> t;
    q.push_back(t);
  }

  memset(visited,-1,sizeof(visited));
  memset(history,-1,sizeof(history));

  visited[0][0] = 0;
  visited[0][1] = 0;
  queue <int> que;
  que.push(0);
  int pos = -1;
  int pos2 = -1;
  while(!que.empty())
  {
    int now = que.front();
    que.pop();
    for(auto next : adj[now])
    {
      if(visited[next][0]==-1)
      {
        visited[next][1] = visited[now][1] + A[now];
        visited[next][0] = visited[now][0] + 1;
        history[next] = now;
        que.push(next);
      }
      else
      {
        pos = next;
        pos2 = now;
        break;
      }
    }
  }

  long long int L = visited[pos][0];
  long long int P = visited[pos2][0] - visited[pos][0] + 1;
  long long int SUM1 = visited[pos][1];
  long long int SUM2 = visited[pos2][1] - visited[pos][1] + A[pos2];

  {
    int cnt = 0;
    queue <int> que;
    que.push(0);
    Left[cnt] = 0;
    while(cnt<L)
    {
      int now = que.front();
      que.pop();
      Left[cnt+1] += Left[cnt];
      Left[cnt+1] += A[now];
      cnt++;
      for(auto next :adj[now])
      {
        que.push(next);
      }
    }
  }

  {
    int cnt = 0;
    queue <int> que;
    que.push(pos);
    Right[cnt] = 0;
    while(cnt<P)
    {
      int now = que.front();
      que.pop();
      Right[cnt+1] += Right[cnt];
      Right[cnt+1] += A[now];
      cnt++;
      for(auto next :adj[now])
      {
        que.push(next);
      }
    }
  }  
  for(int i=0;i<q.size();i++)
  {
    long long int res = 0;

    if(q[i] < L)
    {
      res = Left[q[i]];
    }
    else
    {
      res += SUM1;
      q[i]-=L;
      res += ((q[i]/P)*SUM2);
      q[i]%=P;
      res += Right[q[i]];    
    }
    cout << res << '\n';
  }

	return 0;
}
0