結果

問題 No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに
ユーザー SSRSSSRS
提出日時 2022-10-14 22:26:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 949 ms / 6,000 ms
コード長 2,030 bytes
コンパイル時間 3,392 ms
コンパイル使用メモリ 181,440 KB
実行使用メモリ 28,388 KB
最終ジャッジ日時 2023-09-08 22:35:33
合計ジャッジ時間 36,982 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 268 ms
13,784 KB
testcase_04 AC 640 ms
21,180 KB
testcase_05 AC 287 ms
13,708 KB
testcase_06 AC 558 ms
19,740 KB
testcase_07 AC 616 ms
21,896 KB
testcase_08 AC 662 ms
20,912 KB
testcase_09 AC 226 ms
12,132 KB
testcase_10 AC 233 ms
11,760 KB
testcase_11 AC 465 ms
16,528 KB
testcase_12 AC 473 ms
16,792 KB
testcase_13 AC 290 ms
12,688 KB
testcase_14 AC 462 ms
11,860 KB
testcase_15 AC 755 ms
24,028 KB
testcase_16 AC 444 ms
19,312 KB
testcase_17 AC 292 ms
10,012 KB
testcase_18 AC 919 ms
28,388 KB
testcase_19 AC 911 ms
28,228 KB
testcase_20 AC 915 ms
28,192 KB
testcase_21 AC 949 ms
28,040 KB
testcase_22 AC 912 ms
28,308 KB
testcase_23 AC 919 ms
28,148 KB
testcase_24 AC 925 ms
28,384 KB
testcase_25 AC 949 ms
28,000 KB
testcase_26 AC 903 ms
28,304 KB
testcase_27 AC 898 ms
28,228 KB
testcase_28 AC 947 ms
28,372 KB
testcase_29 AC 929 ms
28,064 KB
testcase_30 AC 916 ms
28,148 KB
testcase_31 AC 930 ms
28,068 KB
testcase_32 AC 925 ms
28,376 KB
testcase_33 AC 771 ms
22,540 KB
testcase_34 AC 771 ms
22,588 KB
testcase_35 AC 786 ms
22,668 KB
testcase_36 AC 782 ms
22,712 KB
testcase_37 AC 762 ms
22,560 KB
testcase_38 AC 418 ms
11,704 KB
testcase_39 AC 510 ms
13,792 KB
testcase_40 AC 603 ms
20,248 KB
testcase_41 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct dual_invertible_binary_indexed_tree{
  int N;
  vector<T> BIT;
  function<T(T, T)> f;
  function<T(T)> inv;
  T E;
  dual_invertible_binary_indexed_tree(int N, function<T(T, T)> f, function<T(T)> inv, T E): N(N), BIT(N + 1, E), f(f), inv(inv), E(E){
  }
  void add(int i, T x){
    while (i > 0){
      BIT[i] = f(BIT[i], x);
      i -= i & -i;
    }
  }
  void add(int l, int r, T x){
    add(l, inv(x));
    add(r, x);
  }
  T operator [](int i){
    i++;
    T ans = E;
    while (i <= N){
      ans = f(ans, BIT[i]);
      i += i & -i;
    }
    return ans;
  }
};
int main(){
  int N;
  cin >> N;
  vector<int> A(N), T(N);
  for (int i = 0; i < N; i++){
    cin >> A[i] >> T[i];
  }
  int Q;
  cin >> Q;
  vector<int> D(Q), L(Q), R(Q);
  for (int i = 0; i < Q; i++){
    cin >> D[i] >> L[i] >> R[i];
    L[i]--;
  }
  vector<int> D2 = D;
  sort(D2.begin(), D2.end());
  D2.erase(unique(D2.begin(), D2.end()), D2.end());
  for (int i = 0; i < Q; i++){
    D[i] = lower_bound(D2.begin(), D2.end(), D[i]) - D2.begin();
  }
  int cnt = D2.size();
  vector<vector<int>> QL(N + 1), QR(N + 1);
  for (int i = 0; i < Q; i++){
    QL[L[i]].push_back(i);
    QR[R[i]].push_back(i);
  }
  vector<long long> ans(Q, 0);
  dual_invertible_binary_indexed_tree<int> BIT1(cnt, plus<int>(), negate<int>(), 0);
  dual_invertible_binary_indexed_tree<long long> BIT2(cnt, plus<long long>(), negate<long long>(), 0);
  for (int i = 0; i <= N; i++){
    for (int j : QL[i]){
      ans[j] -= (long long) BIT1[D[j]] * D2[D[j]] + BIT2[D[j]];
    }
    for (int j : QR[i]){
      ans[j] += (long long) BIT1[D[j]] * D2[D[j]] + BIT2[D[j]];
    }
    if (i < N){
      int p1 = lower_bound(D2.begin(), D2.end(), T[i]) - D2.begin();
      int p2 = lower_bound(D2.begin(), D2.end(), T[i] + A[i]) - D2.begin();
      BIT2.add(0, p1, A[i]);
      BIT1.add(p1, p2, -1);
      BIT2.add(p1, p2, A[i] + T[i] - 1);
    }
  }
  for (int i = 0; i < Q; i++){
    cout << ans[i] << endl;
  }
}
0