結果

問題 No.2338 Range AtCoder Query
ユーザー 👑 emthrmemthrm
提出日時 2023-06-02 23:20:49
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,776 bytes
コンパイル時間 3,450 ms
コンパイル使用メモリ 259,908 KB
実行使用メモリ 273,928 KB
最終ジャッジ日時 2023-08-28 05:52:51
合計ジャッジ時間 13,212 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 211 ms
273,824 KB
testcase_01 AC 212 ms
273,612 KB
testcase_02 AC 215 ms
273,616 KB
testcase_03 AC 211 ms
273,620 KB
testcase_04 AC 211 ms
273,728 KB
testcase_05 AC 212 ms
273,620 KB
testcase_06 AC 213 ms
273,660 KB
testcase_07 AC 214 ms
273,748 KB
testcase_08 AC 216 ms
273,876 KB
testcase_09 AC 213 ms
273,860 KB
testcase_10 AC 214 ms
273,928 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

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]{};
bool s[M]{};
array<deque<int>, M> al{}, ac{};
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].empty()) {
        ++atc;
      } else {
        penalty -= num[ac[prob].front()] - num[al[prob].front()];
      }
      ac[prob].emplace_front(idx);
    } else {
      if (!ac[prob].empty()) ++penalty;
    }
    al[prob].emplace_front(idx);
  } else {
    if (s[idx]) {
      if (ac[prob].empty()) {
        ++atc;
        penalty += al[prob].size();
      }
      ac[prob].emplace_back(idx);
    }
    al[prob].emplace_back(idx);
  }
  // cout << "[add] " << idx << ' ' << atc << ' ' << penalty << '\n';
}

void Mo::del(const int idx) const {
  const int prob = p[idx];
  if (idx + 1 == nl) {
    al[prob].pop_front();
    if (s[idx]) {
      ac[prob].pop_front();
      if (ac[prob].empty()) {
        --atc;
      } else {
        penalty += num[ac[prob].front()] - num[al[prob].front()];
      }
    } else {
      if (!ac[prob].empty()) --penalty;
    }
  } else {
    al[prob].pop_back();
    if (s[idx]) {
      ac[prob].pop_back();
      if (ac[prob].empty()) {
        --atc;
        penalty -= al[prob].size();
      }
    }
  }
  // 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;
  }
  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