結果

問題 No.2338 Range AtCoder Query
ユーザー KumaTachiRenKumaTachiRen
提出日時 2023-06-02 22:41:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,454 bytes
コンパイル時間 5,820 ms
コンパイル使用メモリ 274,304 KB
実行使用メモリ 250,436 KB
最終ジャッジ日時 2023-08-28 04:54:22
合計ジャッジ時間 16,025 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 240 ms
243,692 KB
testcase_01 AC 241 ms
243,636 KB
testcase_02 AC 242 ms
243,888 KB
testcase_03 AC 243 ms
243,632 KB
testcase_04 AC 242 ms
243,664 KB
testcase_05 AC 243 ms
243,668 KB
testcase_06 AC 243 ms
243,656 KB
testcase_07 AC 381 ms
243,460 KB
testcase_08 AC 246 ms
243,668 KB
testcase_09 AC 244 ms
243,648 KB
testcase_10 AC 379 ms
243,532 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>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

struct Fast
{
  Fast()
  {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (a); i--)
#define Yn(b) cout << ((b) ? "Yes" : "No") << "\n";
#define ll long long

template <typename T>
bool chmax(T &a, const T &b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}

struct Mo {
  int width;
  vector<int> left, right, order;

  Mo(int N, int Q) : order(Q) {
    width = max<int>(1, 1.0 * N / max<double>(1.0, sqrt(Q * 2.0 / 3.0)));
    iota(begin(order), end(order), 0);
  }

  void insert(int l, int r) { /* [l, r) */
    left.emplace_back(l);
    right.emplace_back(r);
  }

  template <typename AL, typename AR, typename DL, typename DR, typename REM>
  void run(const AL &add_left, const AR &add_right, const DL &delete_left,
           const DR &delete_right, const REM &rem) {
    assert(left.size() == order.size());
    sort(begin(order), end(order), [&](int a, int b) {
      int ablock = left[a] / width, bblock = left[b] / width;
      if (ablock != bblock) return ablock < bblock;
      if (ablock & 1) return right[a] < right[b];
      return right[a] > right[b];
    });
    int nl = 0, nr = 0;
    for (auto idx : order) {
      while (nl > left[idx]) add_left(--nl);
      while (nr < right[idx]) add_right(nr++);
      while (nl < left[idx]) delete_left(nl++);
      while (nr > right[idx]) delete_right(--nr);
      rem(idx);
    }
  }
};

const int sz = 200000;
int p[sz], ac[sz], point[sz], pena[sz], accnt[sz];
deque<int> wadeq[sz];

int main()
{
  int n, m, q;
  cin >> n >> m >> q;
  rep(i, 0, n)
  {
    string s = "";
    cin >> p[i] >> s;
    p[i]--;
    ac[i] = s == "AC";
  }

  Mo mo(n, q);
  rep(i, 0, q)
  {
    int l, r;
    cin >> l >> r;
    mo.insert(l - 1, r);
  }
  rep(i, 0, sz) wadeq[i] = {0};
  int pt = 0, pn = 0;
  auto add_left = [&](int i)
  {
    int x = p[i];
    if (ac[i])
    {
      if (accnt[x]++ == 0)
      {
        pt++;
      }
      else
      {
        pn -= wadeq[x].front();
      }
      wadeq[x].push_front(0);
    }
    else
    {
      if (accnt[x])
      {
        pn++;
      }
      wadeq[x].front()++;
    }
  };
  auto add_right = [&](int i)
  {
    int x = p[i];
    if (ac[i])
    {
      if (accnt[x]++ == 0)
      {
        pt++;
        pn += wadeq[x].front();
      }
      wadeq[x].push_back(0);
    }
    else
    {
      wadeq[x].back()++;
    }
  };
  auto erase_left = [&](int i)
  {
    int x = p[i];
    if (ac[i])
    {
      wadeq[x].pop_front();
      if (--accnt[x] == 0)
      {
        pt--;
      }
      else
      {
        pn += wadeq[x].front();
      }
    }
    else
    {
      if (accnt[x])
      {
        pn--;
      }
      wadeq[x].front()--;
    }
  };
  auto erase_right = [&](int i)
  {
    int x = p[i];
    if (ac[i])
    {
      wadeq[x].pop_back();
      if (--accnt[x] == 0)
      {
        pt--;
        pn -= wadeq[x].front();
      }
    }
    else
    {
      wadeq[x].back()--;
    }
  };
  auto out = [&](int i)
  {
    point[i] = pt;
    pena[i] = pn;
  };

  mo.run(add_left, add_right, erase_left, erase_right, out);
  rep(i, 0, q) cout << point[i] << " " << pena[i] << "\n";
}
0