結果

問題 No.1496 不思議な数え上げ
ユーザー SSRSSSRS
提出日時 2021-04-30 23:05:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 595 ms / 3,000 ms
コード長 1,272 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 175,220 KB
実行使用メモリ 18,852 KB
最終ジャッジ日時 2023-09-26 07:47:21
合計ジャッジ時間 24,657 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 475 ms
18,600 KB
testcase_04 AC 477 ms
18,576 KB
testcase_05 AC 470 ms
18,544 KB
testcase_06 AC 533 ms
18,644 KB
testcase_07 AC 546 ms
18,544 KB
testcase_08 AC 534 ms
18,588 KB
testcase_09 AC 544 ms
18,700 KB
testcase_10 AC 541 ms
18,704 KB
testcase_11 AC 535 ms
18,544 KB
testcase_12 AC 480 ms
18,700 KB
testcase_13 AC 508 ms
18,572 KB
testcase_14 AC 504 ms
18,560 KB
testcase_15 AC 503 ms
18,660 KB
testcase_16 AC 513 ms
18,604 KB
testcase_17 AC 586 ms
18,584 KB
testcase_18 AC 576 ms
18,640 KB
testcase_19 AC 582 ms
18,560 KB
testcase_20 AC 584 ms
18,808 KB
testcase_21 AC 577 ms
18,540 KB
testcase_22 AC 595 ms
18,596 KB
testcase_23 AC 579 ms
18,616 KB
testcase_24 AC 588 ms
18,584 KB
testcase_25 AC 582 ms
18,596 KB
testcase_26 AC 576 ms
18,620 KB
testcase_27 AC 577 ms
18,708 KB
testcase_28 AC 570 ms
18,580 KB
testcase_29 AC 566 ms
18,576 KB
testcase_30 AC 574 ms
18,852 KB
testcase_31 AC 556 ms
18,692 KB
testcase_32 AC 504 ms
16,988 KB
testcase_33 AC 265 ms
10,744 KB
testcase_34 AC 246 ms
10,096 KB
testcase_35 AC 6 ms
4,380 KB
testcase_36 AC 519 ms
17,020 KB
testcase_37 AC 285 ms
11,268 KB
testcase_38 AC 270 ms
10,680 KB
testcase_39 AC 370 ms
13,612 KB
testcase_40 AC 210 ms
9,088 KB
testcase_41 AC 384 ms
13,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> P(N);
  for (int i = 0; i < N; i++){
    cin >> P[i];
  }
  vector<long long> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<long long> S(N + 1);
  S[0] = 0;
  for (int i = 0; i < N; i++){
    S[i + 1] = S[i] + P[i];
  }
  vector<int> p(N);
  for (int i = 0; i < N; i++){
    p[P[i] - 1] = i;
  }
  set<int> st;
  st.insert(-1);
  st.insert(N);
  vector<int> L(N), R(N);
  for (int i = 0; i < N; i++){
    auto itr1 = prev(st.lower_bound(p[i]));
    L[i] = *itr1 + 1;
    auto itr2 = st.lower_bound(p[i]);
    R[i] = *itr2;
    st.insert(p[i]);
  }
  for (int i = 0; i < N; i++){
    int Lcnt = p[i] - L[i] + 1;
    int Rcnt = R[i] - p[i];
    if (Lcnt <= Rcnt){
      long long ans = 0;
      for (int j = L[i]; j <= p[i]; j++){
        auto itr = upper_bound(S.begin() + p[i] + 1, S.begin() + R[i] + 1, A[i] + S[j]);
        ans += itr - (S.begin() + p[i] + 1);
      }
      cout << ans << endl;
    } else {
      long long ans = 0;
      for (int j = p[i] + 1; j <= R[i]; j++){
        auto itr = lower_bound(S.begin() + L[i], S.begin() + p[i] + 1, S[j] - A[i]);
        ans += (S.begin() + p[i] + 1) - itr;
      }
      cout << ans << endl;
    }
  }
}
0