QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#304244#4857. Puzzle: Hearthstoneyzy1AC ✓321ms16360kbC++175.0kb2024-01-13 16:58:502024-01-13 16:58:51

Judging History

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

  • [2024-01-13 16:58:51]
  • 评测
  • 测评结果:AC
  • 用时:321ms
  • 内存:16360kb
  • [2024-01-13 16:58:50]
  • 提交

answer

#include <bits/stdc++.h>

#if defined(LOCAL)
#define DBG_MACRO_NO_WARNING
#include <dbg.hpp>
#else
#define dbg(x...) (0)
#endif

using namespace std;

using ll = long long;

// #define int ll
#define rep(i, f, t) for (int i = (f), ed##i = (t); i <= ed##i; ++i)
#define re(i, t) rep (i, 1, t)
#define per(i, t, f) for (int i = (t), ed##i = (f); i >= ed##i; --i)
#define ste(i, f, t, s) for (int i = (f), ed##i = (t); i <= ed##i; i += s)
#define nxt(i, f, g) for (int i = g.h[f]; i; i = g.e[i].n)
#define umod(x) ((x) >= mo && ((x) -= mo))
#define dmod(x) ((x) < 0 && ((x) += mo))
#define y1 y1__
#define fio(x) (freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout))
#define lb(x) ((x) & -(x))

template <class T, class E>
__attribute__((always_inline)) inline void up(T &x, E &&y) {
  if (x < y) x = y;
}
template <class T, class E>
__attribute__((always_inline)) inline void down(T &x, E &&y) {
  if (y < x) x = y;
}

const int N = 1e6 + 9;
int n, m, pos[N];

struct Seg {
  struct T {
    int sum, mn, zcnt, fcnt;
  } d[N << 2];

  static T Mer(T x, T y) {
    T res;
    res.sum = x.sum + y.sum;
    res.mn = min(x.mn, x.sum + y.mn);
    res.zcnt = x.zcnt + y.zcnt;
    res.fcnt = x.fcnt + y.fcnt;
    return res;
  }

  inline void Up(int u) { d[u] = Mer(d[u << 1], d[u << 1 | 1]); }

  inline void Cha(int p, int x, int u, int l, int r) {
    if (l == r) {
      if (x == 1)
        d[u].zcnt = 1, d[u].fcnt = 0;
      else if (x == -1)
        d[u].fcnt = 1, d[u].zcnt = 0;
      else
        d[u].zcnt = d[u].fcnt = 0;
      d[u].sum = d[u].mn = x;
      return;
    }
    int mid = (l + r) >> 1;
    if (p <= mid)
      Cha(p, x, u << 1, l, mid);
    else
      Cha(p, x, u << 1 | 1, mid + 1, r);
    Up(u);
  }

  inline T Ask(int L, int R, int u, int l, int r) {
    if (L <= l && r <= R) return d[u];
    int mid = (l + r) >> 1;
    if (R <= mid) return Ask(L, R, u << 1, l, mid);
    if (mid + 1 <= L) return Ask(L, R, u << 1 | 1, mid + 1, r);
    return Mer(Ask(L, R, u << 1, l, mid), Ask(L, R, u << 1 | 1, mid + 1, r));
  }
} seg;

inline bool Add(int id) {
  if (seg.d[1].sum <= 0) return 0;
  seg.Cha(n + id, -1, 1, 1, n + m);
  return 1;
}

inline bool Ck1(int l, int r) {
  auto res1 = seg.Ask(l, r, 1, 1, n + m);
  return res1.fcnt;
}

inline int ErFen1(int L, int r) {
  if (!Ck1(L, r)) return -1;
  int l = L;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (Ck1(L, mid))
      r = mid;
    else
      l = mid + 1;
  }
  int res = Ck1(L, l) ? l : r;
  return res;
}

inline bool Test(int id, int x, int y) {
  if (!y) {
    auto res1 = pos[x] == 1 ? Seg::T{0, 0, 0, 0} : seg.Ask(1, pos[x] - 1, 1, 1, n + m);
    int res2 = seg.Ask(pos[x] + 1, n + m, 1, 1, n + m).mn;
    int res = min(res1.mn, res1.sum + res2);
    if (res < 0) return 0;
  } else {
    auto add = ErFen1(pos[x], n + id - 1);
    // dbg(add);
    if (add == -1) return 0;
    seg.Cha(add, 0, 1, 1, n + m);
  }
  seg.Cha(pos[x], 0, 1, 1, n + m);
  seg.Cha(n + id, 1, 1, 1, n + m);
  pos[x] = n + id;
  return 1;
}

// 查询 [1,x]~[1,r] 是否存在一个 sum=0 的
inline bool Ck2(int x, int r) {
  int res1 = x == 1 ? 0 : seg.Ask(1, x - 1, 1, 1, n + m).sum;
  int res2 = seg.Ask(x, r, 1, 1, n + m).mn;
  // dbg(res1, res2);
  return res1 + res2 == 0;
}

inline int ErFen2(int R) {
  if (!Ck2(1, R)) return -1;
  int l = 1, r = R;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (Ck2(mid, R))
      l = mid;
    else
      r = mid - 1;
  }
  int res = Ck2(r, R) ? r : l;
  return res;
}

inline int ErFen1R(int R) {
  if (!Ck1(1, R)) return -1;
  int l = 1, r = R;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (Ck1(mid, R))
      l = mid;
    else
      r = mid - 1;
  }
  int res = Ck1(r, R) ? r : l;
  return res;
}

inline void Out(int id) {
  int cntyes = 0;
  // dbg(seg.Ask(1, 4, 1, 1, n + m).mn);
  // dbg(seg.Ask(1, 3, 1, 1, n + m).sum);
  // dbg(seg.Ask(3, 3, 1, 1, n + m).sum);
  // dbg(Ck2(4, 4));
  int lstsam = ErFen2(n + id);
  // dbg(lstsam);
  if (lstsam == -1)
    cntyes = 0;
  else
    cntyes = seg.Ask(1, lstsam, 1, 1, n + m).fcnt;
  int cntno = 0;
  int lstadd = ErFen1R(n + id);
  // dbg(lstadd);
  if (lstadd == -1)
    cntno = seg.Ask(1, n + id, 1, 1, n + m).zcnt;
  else
    cntno = seg.Ask(lstadd, n + id, 1, 1, n + m).zcnt;
  cout << cntyes << ' ' << cntno << '\n';
}

inline void Work() {
  cin >> n >> m;
  memset(seg.d, 0, sizeof(*seg.d) * ((n + m) << 2));
  re (i, n) seg.Cha(i, 1, 1, 1, n + m), pos[i] = i;
  re (id, m) {
    string op;
    cin >> op;
    if (op == "add") {
      if (!Add(id))
        cout << "bug\n";
      else
        Out(id);
    } else {
      int x, y;
      cin >> x >> y;
      if (!Test(id, x, y))
        cout << "bug\n";
      else
        Out(id);
    }
    // cerr << "";
  }
}

signed main() {
  // fio("card");
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) Work();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
1 8
test 1 0
test 1 1
add
test 1 0
test 1 1
add
test 1 1
test 1 0
2 10
add
add
add
test 1 1
test 1 1
add
add
add
test 2 1
test 2 1

output:

0 1
bug
1 0
bug
0 1
1 0
0 1
0 1
0 0
2 0
bug
1 1
bug
2 0
bug
bug
1 1
bug

result:

ok 18 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3464kb

input:

1
4 7
add
add
test 3 0
test 4 0
add
test 1 1
test 3 1

output:

0 0
0 0
0 1
2 2
2 0
1 1
1 3

result:

ok 7 lines

Test #3:

score: 0
Accepted
time: 26ms
memory: 3588kb

input:

100000
1 1
add
1 1
add
1 1
test 1 0
1 1
add
1 1
test 1 1
1 1
test 1 0
1 1
test 1 0
1 1
add
1 1
test 1 1
1 1
add
1 1
add
1 1
add
1 1
test 1 0
1 1
add
1 1
test 1 1
1 1
test 1 1
1 1
test 1 1
1 1
test 1 1
1 1
test 1 1
1 1
add
1 1
add
1 1
test 1 0
1 1
add
1 1
add
1 1
test 1 0
1 1
add
1 1
add
1 1
test 1 0...

output:

1 0
1 0
0 1
1 0
bug
0 1
0 1
1 0
bug
1 0
1 0
1 0
0 1
1 0
bug
bug
bug
bug
bug
1 0
1 0
0 1
1 0
1 0
0 1
1 0
1 0
0 1
1 0
0 1
1 0
1 0
0 1
1 0
1 0
1 0
bug
1 0
bug
1 0
0 1
bug
bug
0 1
1 0
bug
bug
bug
0 1
1 0
0 1
1 0
1 0
1 0
1 0
1 0
1 0
1 0
bug
1 0
1 0
1 0
bug
bug
0 1
1 0
0 1
bug
0 1
1 0
0 1
1 0
0 1
0 1
0 1
...

result:

ok 100000 lines

Test #4:

score: 0
Accepted
time: 50ms
memory: 3552kb

input:

10000
32 8
add
add
test 8 1
test 17 1
add
add
add
test 21 0
41 20
test 30 1
add
test 1 0
test 1 1
test 7 1
test 40 0
test 16 1
add
add
add
add
test 17 0
test 12 0
add
add
test 20 1
test 3 1
add
test 15 0
test 7 0
1 5
add
add
add
add
add
6 46
add
test 2 1
test 3 1
test 1 0
add
add
add
add
add
add
tes...

output:

0 0
0 0
0 1
0 32
0 0
0 0
0 0
0 1
bug
0 0
0 1
bug
0 41
0 41
bug
0 0
0 0
0 0
0 0
0 1
0 2
0 0
0 0
0 1
0 2
0 0
0 1
0 2
1 0
bug
bug
bug
bug
0 0
0 6
bug
0 6
0 0
0 0
0 0
0 0
0 0
6 0
5 1
4 2
bug
4 2
4 2
4 0
6 0
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
5 1
4 2
4 0
bug
6 0
bug
bug
bug
bug
bug
bug
bug
5 1
6...

result:

ok 100000 lines

Test #5:

score: 0
Accepted
time: 119ms
memory: 3532kb

input:

1000
53 136
test 11 1
add
test 35 0
test 9 1
add
add
add
test 41 0
add
add
add
add
test 25 0
test 27 0
test 41 0
add
test 4 0
add
test 44 1
add
add
test 44 1
add
add
test 43 1
add
test 2 0
add
add
add
add
add
add
test 52 1
add
test 6 1
add
test 28 0
test 17 0
test 12 0
test 34 1
add
add
add
test 14 ...

output:

bug
0 0
0 1
0 53
0 0
0 0
0 0
0 1
0 0
0 0
0 0
0 0
0 1
0 2
0 3
0 0
0 1
0 0
0 1
0 0
0 0
0 1
0 0
0 0
0 1
0 0
0 1
0 0
0 0
0 0
0 0
0 0
0 0
0 1
0 0
0 1
0 0
0 1
0 2
0 3
0 4
0 0
0 0
0 0
0 1
0 2
bug
0 3
0 0
0 0
0 0
0 1
0 0
0 0
0 0
0 0
0 1
0 2
0 0
0 0
0 1
0 2
0 3
0 0
0 0
0 1
0 2
0 0
0 0
0 0
0 0
0 1
0 0
0 1
0 0...

result:

ok 100000 lines

Test #6:

score: 0
Accepted
time: 183ms
memory: 3876kb

input:

100
109 357
test 79 0
test 79 1
add
test 44 1
test 20 1
test 51 1
add
test 92 1
test 5 1
test 30 0
add
add
test 102 0
test 11 0
test 13 0
test 95 1
test 41 0
test 3 1
test 8 1
add
test 2 0
test 64 0
add
test 54 0
test 47 0
test 98 1
test 79 1
test 92 0
test 8 0
add
test 73 1
test 36 0
add
test 35 0
...

output:

0 109
bug
0 0
0 109
bug
bug
0 0
0 109
bug
0 109
0 0
0 0
0 1
0 2
0 3
0 4
0 5
0 109
bug
0 0
0 1
0 2
0 0
0 1
0 2
0 3
0 109
0 109
0 109
0 0
0 109
0 109
0 0
0 1
0 0
0 1
0 0
0 0
0 0
0 0
0 1
0 2
0 0
0 0
0 1
0 0
0 0
0 0
0 1
0 0
0 0
0 1
0 0
0 0
0 0
0 1
0 0
0 1
0 0
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 3
0 0
0 0
0 0
...

result:

ok 100000 lines

Test #7:

score: 0
Accepted
time: 254ms
memory: 5944kb

input:

10
25552 12446
add
add
test 22946 1
test 4399 0
test 10460 0
add
test 15897 0
add
test 16867 0
add
test 1999 1
test 24798 0
add
add
add
test 8221 1
test 25345 0
test 19211 1
add
add
test 14730 1
add
test 25216 0
add
add
add
add
test 8793 0
test 18342 0
test 9516 1
add
add
add
test 6745 1
test 2365 1...

output:

0 0
0 0
0 1
0 2
0 3
0 0
0 1
0 0
0 1
0 0
0 1
0 2
0 0
0 0
0 0
0 1
0 2
0 3
0 0
0 0
0 1
0 0
0 1
0 0
0 0
0 0
0 0
0 1
0 2
0 3
0 0
0 0
0 0
0 1
0 2
0 0
0 0
0 1
0 0
0 0
0 1
0 2
0 3
0 0
0 1
0 0
0 1
0 0
0 1
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 3
0 4
0 0
0 1
0 2
0 3
0 0
0 0
0 1
0 2
0 0
0 1
0 0
0 1
0 2
0 0
0 1
0 0
0 0
...

result:

ok 100000 lines

Test #8:

score: 0
Accepted
time: 313ms
memory: 16360kb

input:

1
100000 100000
add
add
test 71404 1
test 62095 0
add
add
add
test 29685 0
add
test 99916 0
add
add
add
add
add
add
add
add
add
add
add
test 43433 0
test 88048 1
add
test 70691 0
add
add
test 88525 0
add
test 74492 1
test 9542 1
test 88355 1
add
test 41894 0
test 2337 0
test 52673 0
add
test 98139 1...

output:

0 0
0 0
0 1
0 2
0 0
0 0
0 0
0 1
0 0
0 1
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 1
0 2
0 0
0 1
0 0
0 0
0 1
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 0
0 0
0 1
0 2
0 3
0 4
0 5
0 0
0 1
0 0
0 0
0 1
0 0
0 1
0 0
0 1
0 2
0 3
0 4
0 5
0 0
0 1
0 2
0 3
0 4
0 5
0 0
0 0
0 0
0 0
0 0
...

result:

ok 100000 lines

Test #9:

score: 0
Accepted
time: 321ms
memory: 10448kb

input:

1
10000 100000
test 1088 1
add
add
add
test 4148 1
test 3699 0
add
test 4394 1
test 6465 1
test 6479 0
test 4746 1
add
add
test 6936 0
test 4300 0
test 8779 1
test 2311 0
add
test 6913 0
test 5255 0
add
test 3355 0
add
add
add
test 7119 1
test 8449 0
test 3464 0
test 1716 0
add
test 7997 0
test 9195...

output:

bug
0 0
0 0
0 0
0 1
0 2
0 0
0 1
0 2
0 3
0 10000
0 0
0 0
0 1
0 2
0 3
0 4
0 0
0 1
0 2
0 0
0 1
0 0
0 0
0 0
0 1
0 2
0 3
0 4
0 0
0 1
0 2
0 0
0 1
0 2
0 0
0 0
0 1
0 0
0 0
0 1
0 2
0 3
0 4
0 0
0 0
0 0
0 0
0 1
0 0
0 1
0 2
0 0
0 1
0 0
0 0
0 0
0 0
0 0
0 1
0 2
0 3
0 0
0 0
0 0
0 0
0 0
0 1
0 0
0 1
0 0
0 1
0 2
0 3
...

result:

ok 100000 lines

Test #10:

score: 0
Accepted
time: 269ms
memory: 9904kb

input:

1
1000 100000
add
test 724 0
add
test 437 1
add
add
test 695 1
test 643 1
add
add
add
add
test 927 1
test 422 0
add
test 763 1
add
add
add
add
test 196 0
test 825 0
add
test 775 1
test 118 1
add
test 861 0
add
add
test 276 1
test 899 0
add
add
add
test 172 0
test 99 0
test 846 0
add
add
add
test 940...

output:

0 0
0 1
0 0
0 1
0 0
0 0
0 1
0 2
0 0
0 0
0 0
0 0
0 1
0 2
0 0
0 1
0 0
0 0
0 0
0 0
0 1
0 2
0 0
0 1
0 2
0 0
0 1
0 0
0 0
0 1
0 2
0 0
0 0
0 0
0 1
0 2
0 3
0 0
0 0
0 0
0 1
0 0
0 0
0 0
0 1
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 0
0 1
0 2
0 3
0 0
0 1
0 0
0 0
0 1
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 3
0 0
0 0
0 0
0 1
0 2
0 0
...

result:

ok 100000 lines

Test #11:

score: 0
Accepted
time: 237ms
memory: 9804kb

input:

1
100 100000
test 57 1
test 66 0
add
test 34 1
add
add
add
test 18 0
test 35 0
test 16 1
test 84 1
add
test 97 1
add
test 60 1
add
add
test 40 0
test 86 1
add
test 24 0
add
add
add
add
test 84 0
test 61 0
add
test 94 0
test 80 0
test 58 1
add
add
test 49 1
test 45 0
test 32 1
add
test 69 0
test 76 0...

output:

bug
0 100
0 0
0 100
0 0
0 0
0 0
0 1
0 2
0 3
0 4
0 0
0 1
0 0
0 1
0 0
0 0
0 1
0 2
0 0
0 1
0 0
0 0
0 0
0 0
0 1
0 2
0 0
0 1
0 2
0 3
0 0
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 0
0 1
0 0
0 1
0 0
0 0
0 1
0 0
0 1
0 2
0 0
0 1
0 2
0 0
0 0
0 0
0 1
0 2
0 3
0 0
0 1
0 2
0 0
0 0
0 1
0 0
0 0
0 0
0 1
0 2
0 0
0 1
0 2
0 3
0 0
...

result:

ok 100000 lines

Test #12:

score: 0
Accepted
time: 224ms
memory: 9752kb

input:

1
10 100000
add
add
test 10 0
test 5 1
add
add
add
add
add
add
add
add
test 6 1
test 10 1
test 5 1
test 8 0
add
add
add
add
add
test 1 0
test 9 0
test 5 0
test 6 1
test 5 1
test 5 1
add
test 2 0
add
test 8 1
add
test 9 0
test 6 1
add
test 10 1
add
test 10 0
test 9 1
add
test 7 1
test 1 1
add
add
add...

output:

0 0
0 0
0 1
0 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 1
0 2
0 3
6 4
6 0
6 0
6 0
10 0
bug
bug
bug
bug
9 1
8 2
bug
8 0
bug
10 0
9 1
10 0
bug
9 1
10 0
9 1
10 0
bug
9 1
10 0
9 1
8 2
8 0
10 0
bug
9 1
10 0
bug
bug
bug
9 1
8 2
8 0
10 0
9 1
8 2
bug
8 0
10 0
9 1
10 0
bug
bug
bug
bug
bug
bug
9 1
bug
10 0
bug
bug
...

result:

ok 100000 lines