結果

問題 No.2338 Range AtCoder Query
ユーザー 👑 emthrmemthrm
提出日時 2023-06-02 23:37:50
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 795 ms / 4,000 ms
コード長 3,690 bytes
コンパイル時間 3,443 ms
コンパイル使用メモリ 257,708 KB
実行使用メモリ 14,420 KB
最終ジャッジ日時 2023-08-28 06:19:39
合計ジャッジ時間 21,565 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,428 KB
testcase_01 AC 2 ms
5,444 KB
testcase_02 AC 2 ms
5,416 KB
testcase_03 AC 2 ms
5,440 KB
testcase_04 AC 2 ms
5,472 KB
testcase_05 AC 2 ms
5,456 KB
testcase_06 AC 3 ms
5,568 KB
testcase_07 AC 3 ms
5,492 KB
testcase_08 AC 3 ms
5,496 KB
testcase_09 AC 3 ms
5,760 KB
testcase_10 AC 3 ms
5,524 KB
testcase_11 AC 674 ms
11,220 KB
testcase_12 AC 615 ms
11,252 KB
testcase_13 AC 693 ms
11,248 KB
testcase_14 AC 763 ms
10,412 KB
testcase_15 AC 758 ms
11,936 KB
testcase_16 AC 795 ms
14,152 KB
testcase_17 AC 792 ms
14,152 KB
testcase_18 AC 789 ms
14,228 KB
testcase_19 AC 792 ms
14,192 KB
testcase_20 AC 786 ms
14,184 KB
testcase_21 AC 793 ms
14,208 KB
testcase_22 AC 790 ms
14,420 KB
testcase_23 AC 791 ms
14,200 KB
testcase_24 AC 791 ms
14,204 KB
testcase_25 AC 794 ms
14,156 KB
testcase_26 AC 49 ms
8,072 KB
testcase_27 AC 40 ms
8,076 KB
testcase_28 AC 41 ms
8,048 KB
testcase_29 AC 460 ms
14,192 KB
testcase_30 AC 774 ms
14,196 KB
testcase_31 AC 438 ms
14,208 KB
testcase_32 AC 413 ms
13,552 KB
testcase_33 AC 162 ms
14,380 KB
testcase_34 AC 163 ms
14,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct Mo {
  explicit Mo(const std::vector<int>& ls, const std::vector<int>& rs)
      : n(ls.size()), ptr(0), nl(0), nr(0), ls(ls), rs(rs) {
    const int width = std::round(std::sqrt(n));
    order.resize(n);
    std::iota(order.begin(), order.end(), 0);
    std::sort(order.begin(), order.end(),
              [&ls, &rs, width](const int a, const int b) -> bool {
                  if (ls[a] / width != ls[b] / width) return ls[a] < ls[b];
                  return (ls[a] / width) & 1 ? rs[a] < rs[b] : rs[a] > rs[b];
              });
  }

  int process() {
    if (ptr == n) [[unlikely]] return -1;
    const int id = order[ptr++];
    while (ls[id] < nl) add(--nl);
    while (nr < rs[id]) add(nr++);
    while (nl < ls[id]) del(nl++);
    while (rs[id] < nr) del(--nr);
    return id;
  }

  void add(const int idx) const;

  void del(const int idx) const;

  int nl, nr;
 private:
  const int n;
  int ptr;
  std::vector<int> ls, rs, order;
};

constexpr int N = 200000, M = 200000;
int p[M]{}, num[N]{}, nxt[N]{};
bool s[M]{};
int al[M]{}, ac[M]{};
int atc = 0;
ll penalty = 0;

void Mo::add(const int idx) const {
  const int prob = p[idx];
  if (idx == nl) {
    if (s[idx]) {
      if (ac[prob] == 0) {
        ++atc;
      } else {
        penalty -= num[nxt[idx]] - num[idx] - 1;
      }
      ++ac[prob];
    } else {
      if (ac[prob] > 0) ++penalty;
    }
  } else {
    if (s[idx]) {
      if (ac[prob] == 0) {
        ++atc;
        penalty += al[prob];
      }
      ++ac[prob];
    }
  }
  ++al[prob];
  // cout << "[add] " << idx << ' ' << atc << ' ' << penalty << '\n';
}

void Mo::del(const int idx) const {
  const int prob = p[idx];
  --al[prob];
  if (idx + 1 == nl) {
    if (s[idx]) {
      --ac[prob];
      if (ac[prob] == 0) {
        --atc;
      } else {
        penalty += num[nxt[idx]] - num[idx] - 1;
      }
    } else {
      if (ac[prob] > 0) --penalty;
    }
  } else {
    if (s[idx] && --ac[prob] == 0) {
      --atc;
      penalty -= al[prob];
    }
  }
  // cout << "[del] " << idx << ' ' << atc << ' ' << penalty << '\n';
}

int main() {
  int n, m, q; cin >> n >> m >> q;
  REP(i, n) {
    string ac_or_wa; cin >> p[i] >> ac_or_wa; --p[i];
    s[i] = (ac_or_wa == "AC");
  }
  vector<int> last(m, -1);
  REP(i, n) {
    if (last[p[i]] != -1) num[i] = num[last[p[i]]];
    ++num[i];
    last[p[i]] = i;
  }
  ranges::fill(last, -1);
  fill(nxt, nxt + n, -1);
  REP(i, n) {
    if (s[i]) {
      if (last[p[i]] != -1) nxt[last[p[i]]] = i;
      last[p[i]] = i;
    }
  }
  vector<int> l(q), r(q); REP(i, q) cin >> l[i] >> r[i], --l[i];
  Mo mo(l, r);
  vector<int> ans1(q);
  vector<ll> ans2(q);
  REP(_, q) {
    const int index = mo.process();
    ans1[index] = atc;
    ans2[index] = penalty;
  }
  REP(i, q) cout << ans1[i] << ' ' << ans2[i] << '\n';
  return 0;
}
0