QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#649863#8557. Goldberg MachinecaijianhongWA 429ms74464kbC++234.4kb2024-10-18 11:12:432024-10-18 11:12:43

Judging History

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

  • [2024-10-18 11:12:43]
  • 评测
  • 测评结果:WA
  • 用时:429ms
  • 内存:74464kb
  • [2024-10-18 11:12:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
namespace ds_bf {
  int c[200010];
  void add(int l, int r, int k) {
    if (l > r) return ;
    for (int i = l; i <= r; i++) c[i] += k;
  }
  LL sum(int n, int k) {
    LL res = 0;
    for (int i = 1; i <= n; i++) res += min(k, c[i]);
    return res;
  }
  int select(int n, LL k, int idx) {
    assert(idx > 0);
    for (int i = 1; i <= n; i++) if (c[i] <= k && --idx == 0) return i;
    return -1;
  }
};
namespace ds {
  constexpr int B = 1400, N = 1e5 + 10;
  struct block {
    int elem[B], tag, ptr[N], val[B];
    LL psm[B];
    block() {
      memset(elem, 0, sizeof elem);
      memset(val, 0, sizeof val);
      memset(psm, 0, sizeof psm);
      tag = 0;
      fill(ptr + 1, ptr + N, B);
    }
    void add_value(int v) { ++val[--ptr[v + 1]]; }
    void del_value(int v) { --val[ptr[v]++]; }
    void buildsum() {
      psm[0] = val[0];
      for (int i = 1; i < B; i++) psm[i] = psm[i - 1] + val[i];
    }
    void _add(int l, int r) {
      for (int i = l; i <= r; i++) add_value(elem[i]++);
      buildsum();
    }
    void _del(int l, int r) {
      for (int i = l; i <= r; i++) del_value(elem[i]--);
      buildsum();
    }
    void add(int l, int r, int k) { return k == 1 ? _add(l, r) : _del(l, r); }
    LL calc(int k) { // sum_x min(x, k) [WA]
      int pos = k >= tag ? ptr[k - tag] : 0;
      return (pos ? psm[pos - 1] + 1ll * pos * tag: 0) + 1ll * (B - pos) * k;
    }
    int count(int k) { // sum_x [x <= k]
      return k + 1 >= tag ? ptr[k - tag + 1] : 0;
    }
    int select(int k, int idx) { // idx-th x, x <= k
      for (int i = 0; i < B; i++) if (elem[i] <= k - tag && --idx == 0) return i;
      throw logic_error("ds::block::select: wrong argument");
    }
  } blk[N * 2 / B + 10];
  void add(int l, int r, int k) {
    debug("add(%d, %d, %d)\n", l, r, k);
    if (l % B && (l / B + 1) * B - 1 <= r) blk[l / B].add(l % B, B - 1, k), l = (l / B + 1) * B;
    while (l + B - 1 <= r) blk[l / B].tag += k, l += B;
    if (l <= r) blk[l / B].add(l % B, r % B, k);
  }
  LL sum(int n, int k) {
    LL res = 0;
    for (int i = 0; i * B <= n; i++) res += blk[i].calc(k);
    debug("sum(%d, %d) = %lld\n", n, k, res);
    return res;
  }
  int select(int n, int k, int idx) {
    ++idx;
    for (int i = 0; i * B <= n; i++) {
      if ((i + 1) * B <= n && blk[i].count(k) < idx) idx -= blk[i].count(k);
      else return blk[i].select(k, idx) + i * B;
    }
    throw logic_error("ds::select: wrong argument");
  }
};
int n, dfn[100010], rdel[100010], ptr[100010], fa[100010], cnt, siz[100010], rnk[200010], q;
vector<int> g[100010];
void dfs(int u, int _fa) {
  if (u != 1) {
    int &pos = rdel[u] = (int)(find(g[u].begin(), g[u].end(), _fa) - g[u].begin()), c = (int)g[u].size();
    pos = (pos + 1) % c, ptr[u] = (ptr[u] - pos + c) % c;
    rotate(g[u].begin(), g[u].begin() + pos, g[u].end());
  }
  dfn[u] = ++cnt, siz[u] = 1, fa[u] = _fa, rnk[cnt] = u;
  for (int v : g[u]) if (v != _fa) dfs(v, u), siz[u] += siz[v], rnk[++cnt] = u;
}
void update_t(int u, int k) {
  int l = dfn[u] + 1, r;
  if (u > 1 && ptr[u] + 1 == (int)g[u].size()) r = dfn[u] + siz[u] * 2 - 2;
  else r = dfn[g[u][ptr[u]]] - 1;
  r = min(r, cnt);
  if (l <= r) ds::add(l, r, k);
}
int main() {
#ifndef LOCAL
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  cin >> n;
  for (int i = 1, c; i <= n; i++) {
    cin >> c >> ptr[i];
    g[i].resize(c);
    for (int j = 0; j < c; j++) cin >> g[i][j];
  }
  dfs(1, 0), --cnt;
  for (int i = 1; i <= n; i++) update_t(i, +1);
  cin >> q;
  while (q--) {
    char op;
    cin >> op;
    if (op == 'C') {
      int x, y;
      cin >> x >> y;
      update_t(x, -1);
      int c = (int)g[x].size();
      ptr[x] = (y - rdel[x] + c) % c;
      update_t(x, +1);
    } else {
      LL k;
      cin >> k;
      ++k;
      int now = 0;
      auto calc = [&](int now) { return cnt * now - ds::sum(cnt, now); };
      for (int j = __lg(n); j >= 0; j--) if (calc(now + (1 << j)) < k) now += 1 << j;
      debug("k = %lld, calc(%d) = %lld\n", k, now, calc(now));
      if (now >= n) cout << rnk[(k - calc(now) - 1) % cnt + 1] << endl;
      else cout << rnk[ds::select(cnt, now, k - calc(now))] << endl;
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 71408kb

input:

7
2 0 2 3
3 0 1 4 5
3 0 1 6 7
1 0 2
1 0 2
1 0 3
1 0 3
5
Q 10
C 2 1
Q 10
C 3 2
Q 10

output:

1
4
1

result:

ok 3 number(s): "1 4 1"

Test #2:

score: 0
Accepted
time: 12ms
memory: 71176kb

input:

2
1 0 2
1 0 1
10000
Q 330436
Q 980610
Q 275
Q 22846
Q 85515
Q 339826
Q 647638
Q 689585
Q 153103
Q 934080
Q 633096
Q 431962
Q 692869
Q 914798
Q 436671
Q 832351
Q 78371
Q 328899
Q 727824
Q 351781
Q 819165
Q 267140
Q 722576
Q 133777
Q 414375
Q 205621
Q 160403
Q 445832
Q 628729
Q 532763
Q 934703
Q 80779...

output:

1
1
2
1
2
1
1
2
2
1
1
1
2
1
2
2
2
2
1
2
2
1
1
2
2
2
2
1
2
2
2
1
2
1
2
1
2
1
1
2
2
2
1
1
2
2
2
2
1
1
2
2
2
1
2
2
1
2
1
2
2
2
2
1
2
1
2
1
2
2
2
2
1
1
2
1
1
1
2
1
1
1
2
1
2
1
1
2
2
1
2
2
1
1
1
1
2
1
2
2
2
1
2
1
1
1
2
1
2
2
2
1
1
2
2
2
1
1
2
1
1
1
2
1
1
1
1
1
2
2
2
2
1
2
1
1
2
1
2
1
1
2
2
1
1
2
2
1
1
1
...

result:

ok 10000 numbers

Test #3:

score: 0
Accepted
time: 29ms
memory: 67504kb

input:

3
2 1 2 3
1 0 1
1 0 1
100000
C 2 0
C 2 0
Q 145832
C 3 0
Q 19924
Q 443533
C 1 0
C 1 0
C 3 0
C 2 0
C 3 0
Q 635031
Q 280859
Q 509745
C 1 1
Q 262631
Q 340848
C 2 0
C 3 0
C 2 0
C 2 0
Q 775790
Q 614349
C 2 0
Q 684238
Q 797643
C 1 1
C 1 1
C 3 0
C 2 0
C 1 0
Q 669611
Q 257133
Q 631101
Q 731654
C 2 0
Q 942620...

output:

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

result:

ok 50039 numbers

Test #4:

score: 0
Accepted
time: 38ms
memory: 67740kb

input:

100
3 1 2 7 3
5 2 12 82 1 45 5
6 4 50 4 83 9 1 6
4 2 16 22 59 3
4 3 11 2 10 21
5 2 20 27 3 14 60
4 0 53 1 13 8
6 5 56 39 15 96 7 68
4 0 80 3 18 31
6 2 48 30 5 65 24 36
2 1 5 23
3 0 2 81 33
3 2 7 37 17
3 1 69 73 6
4 2 78 26 8 19
2 0 4 54
2 1 13 100
5 4 61 57 9 28 32
2 1 29 15
2 0 66 6
2 0 5 35
4 0 25...

output:

15
10
8
5
11
71
13
12
2
7
49
17
16
23
20
45
20
3
39
10
36
59
90
76
8
96
65
14
59
24
30
69
55
56
40
4
69
39
22
53
4
57
45
77
7
2
21
42
53
48
19
5
2
29
85
5
3
82
69
39
67
30
61
39
31
85
39
3
39
49
84
5
93
49
17
75
60
40
70
88
42
18
3
63
54
22
39
39
39
9
1
7
22
17
36
2
48
24
61
95
31
16
10
96
77
14
13
...

result:

ok 50092 numbers

Test #5:

score: 0
Accepted
time: 270ms
memory: 70840kb

input:

10000
1 0 2
2 1 3 1
2 0 4 2
2 0 5 3
2 0 6 4
2 1 7 5
2 1 6 8
2 1 7 9
2 1 8 10
2 1 11 9
2 1 12 10
2 1 11 13
2 1 14 12
2 0 15 13
2 1 14 16
2 0 15 17
2 1 18 16
2 1 17 19
2 1 18 20
2 1 19 21
2 0 20 22
2 1 21 23
2 1 24 22
2 1 23 25
2 1 24 26
2 0 27 25
2 0 26 28
2 0 27 29
2 1 28 30
2 1 31 29
2 1 32 30
2 0 ...

output:

3695
1238
8636
8591
2895
2667
4000
3585
8805
6970
693
1129
9441
4828
1860
8041
7027
3334
826
3833
7100
145
3356
1195
9411
5003
1050
7925
5316
8514
5282
2349
9669
5638
1680
1593
568
2541
71
9748
2000
8123
6526
9624
5372
8597
600
8168
1137
7137
5992
6263
5121
3854
7322
2048
995
6513
4767
6323
3397
521...

result:

ok 49942 numbers

Test #6:

score: 0
Accepted
time: 104ms
memory: 69936kb

input:

30000
29999 8267 1430 15644 27896 12799 17006 2477 8579 14556 4736 23952 25221 4682 5439 25328 2601 11013 27331 13683 20234 23304 22969 10465 7534 10799 23752 11252 19644 9199 28411 6470 13255 27985 548 18397 14573 433 11968 24502 16871 15844 5273 28520 4849 14604 6927 8161 5450 2693 18229 10077 121...

output:

1
1
1
18571
1
1
1
1
20202
1
608
15018
1
26493
1
6195
1
3238
5442
27989
1
1
13461
1
27683
1
22468
1
10644
16048
5627
1
25230
1
1
11300
1
21716
1
1
1
1
4579
19435
10620
16302
8473
1
1
11022
1
1
1
1185
24354
1207
26583
5269
5527
1
23873
27349
1
9017
1
1
8809
11374
1
1
16028
1
1
1
1
2176
1
11776
2947
89...

result:

ok 49891 numbers

Test #7:

score: -100
Wrong Answer
time: 429ms
memory: 74464kb

input:

100000
7 3 2 8 9 20 79 239 71974
13 0 1065 275 3 138 4 1652 14 13 54296 1 22924 7635 74
10 8 7 5 100 3207 133 643 21907 107 2 63
18 14 2 55 366 1942 1576 11026 51606 12 216 139 352 6 11079 802 5046 981 6331 1636
8 2 135 32774 16 3143 935 386 12955 3
13 2 161 26 10499 99 35999 19850 17 28 12452 2172 ...

output:

2391
656
96869
94697
67537
6980
70105
12470
84610
16594
26081
7035
51551
58374
1678
21368
7816
51459
39226
3309
73519
20013
50099
1076
90799
98324
19243
39518
281
126
2631
10620
16923
14446
23531
6054
69845
44821
8826
40665
35410
75792
44916
74244
42564
24181
18502
85994
11359
36213
29951
16046
5776...

result:

wrong answer 1st numbers differ - expected: '18439', found: '2391'