QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#625600#7627. PhonyDBsoleilTL 1ms5896kbC++233.6kb2024-10-09 20:03:202024-10-09 20:03:21

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 20:03:21]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5896kb
  • [2024-10-09 20:03:20]
  • 提交

answer


#include <bits/stdc++.h>
using namespace std;
static constexpr int64_t inf = 0x3f3f3f3f3f3f3f3f;
uint64_t randu64(void) {
  static const auto FIXED = chrono::steady_clock::now().time_since_epoch().count();
  static mt19937_64 gen(FIXED); return gen();
} // randu64
namespace treap {
  static constexpr int MaxN = 5e5 + 5;
  struct node {
    int ls, rs, size;
    uint64_t fix;
    int64_t val;
    node() = default;
    node(int64_t v) : ls(0), rs(0), size(1), fix(randu64()), val(v) { }
  } tr[MaxN];
  int tn, root;
  int newnode(int64_t v) { return tr[++tn] = node(v), tn; }
  void pushup(int p) {
    tr[p].size = tr[tr[p].ls].size + tr[tr[p].rs].size + 1;
  } // treap::pushup
  int join(int l, int r) {
    if (!l || !r) return l | r;
    if (tr[l].fix < tr[r].fix) {
      tr[l].rs = join(tr[l].rs, r);
      return pushup(l), l;
    } else {
      tr[r].ls = join(l, tr[r].ls);
      return pushup(r), r;
    }
  } // treap::join
  void split_v(int p, int64_t v, int &l, int &r) {
    if (!p) return l = r = 0, void();
    if (tr[p].val <= v) l = p, split_v(tr[p].rs, v, tr[l].rs, r), pushup(l);
    else r = p, split_v(tr[p].rs, v, l, tr[r].rs), pushup(r);
  } // treap::split_v
  void insert(int64_t v) {
    int A, B; split_v(root, v, A, B);
    root = join(A, join(newnode(v), B));
  } // treap::insert
  int64_t find_k(int k) {
    int p = root;
    while (p) {
      if (tr[tr[p].rs].size + 1 == k) return tr[p].val;
      if (tr[tr[p].rs].size + 1 < k)
        k -= tr[tr[p].rs].size - 1, p = tr[p].ls;
      else p = tr[p].rs;
    } return -inf;
  } // treap::find_k
  int rankv(int64_t v) {
    int p = root, y = 0;
    while (p) {
      if (tr[p].val <= v) p = tr[p].rs;
      else y += tr[tr[p].rs].size + 1, p = tr[p].ls;
    } return y;
  } // treap::rankv
} // namespace treap
using treap::insert, treap::find_k, treap::rankv;
static constexpr int Maxn = 5e5 + 5;
int n, qn, tn, cur;
int64_t K, b[Maxn], add;
int64_t modK(int64_t x) { return (x %= K) < 0 ? x + K : x; }
int64_t floorK(int64_t x) { return x - modK(x); }
int main(void) {
  cin.tie(nullptr)->sync_with_stdio(false);
  cin >> n >> qn >> K; tn = cur = 0;
  for (int i = 1; i <= n; ++i) cin >> b[i];
  sort(b + 1, b + n + 1, greater<int64_t>());
  int tn = 0; add = floorK(b[1]);
  while (tn <= n && floorK(b[tn + 1]) == add) insert(modK(b[++tn]));
  while (qn--) {
    string op; int64_t x;
    cin >> op >> x;
    if (op.front() == 'C') {
      while (x) {
        if (tn < n && floorK(b[tn + 1]) == add - K) {
          int y = rankv(modK(b[tn + 1])) - cur;
          if (y != 0) {
            if (x <= y) cur += x, x = 0;
            else cur += y, x -= y;
          }
          if (x != 0) {
            insert(modK(b[++tn])), ++cur;
          }
        } else {
          if (cur != 0) {
            if (tn - cur < x) x -= tn - cur, cur = 0, add -= K;
            else cur += x, x = 0;
          } else {
            if (x < tn) {
              cur = x, x = 0;
            } else {
              if (tn < n) {
                int64_t p = floorK(b[tn + 1]);
                if ((add - p) / K * tn <= x)
                  x -= (add - p) / K * tn, add = p;
                else add -= x / tn * K, x %= tn;
              } else {
                add -= x / tn * K, x %= tn;
              }
            }
          }
        }
      }
    } else {
      int64_t ans = inf;
      if (x <= tn - cur) ans = find_k(cur + x) + add;
      else if (x <= tn) ans = find_k(x - (tn - cur)) + add - K;
      else ans = b[x];
      cout << ans << endl;
    }
  }
  return 0;
} // main

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5896kb

input:

3 5 5
7 3 9
A 3
C 1
A 2
C 2
A 3

output:

3
4
-1

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

input:

5 8 8
294 928 293 392 719
A 4
C 200
A 5
C 10
A 2
C 120
A 1
A 3

output:

294

result: