QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#864378#9676. AncestorsGodzilla0 354ms65816kbC++234.8kb2025-01-20 15:31:462025-01-20 15:31:46

Judging History

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

  • [2025-01-20 15:31:46]
  • 评测
  • 测评结果:0
  • 用时:354ms
  • 内存:65816kb
  • [2025-01-20 15:31:46]
  • 提交

answer

#include <bits/stdc++.h>
#define file_in(x) (freopen(#x".in", "r", stdin))
#define file_out(x) (freopen(#x".out", "w", stdout))
#define ll long long
#define ull unsigned long long
#define vi vector
#define pr pair <int, int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ls(p) (p << 1)
#define rs(p) ((p << 1) | 1)

using namespace std;

char _c; bool _f; template <class T> void IN(T & x) {
  _f = x = 0; while (_c = getchar(), !isdigit(_c)) {if (_c == '-') _f = 1;}
  while (isdigit(_c)) {x = x * 10 + _c - '0', _c = getchar();} if (_f) x = -x;
}
template <class T> void _write(T x) {
  if (x < 0) return putchar('-'), _write(-x), void();
  if (x > 9) _write(x / 10);
  putchar('0' + x % 10);
}
template <class T> void write_s(T x) {_write(x), putchar(' ');}
template <class T> void write(T x) {_write(x), putchar('\n');}
template <class first, class... rest> void write(first fir, rest... res) {
  write_s(fir), write(res...);
}
#define debug(...) (_debug(#__VA_ARGS__, __VA_ARGS__))
template <class T> void _debug(const char * format, T x) {
  cerr << format << " = " << x << endl;
}
template <class first, class... rest>
void _debug(const char * format, first fir, rest... res) {
  while (*format != ',') cerr << *format++;
  cerr << " = " << fir << ',', _debug(format + 1, res...);
}

bool START;

const int N = 1e5 + 5, M = 1e6 + 5, B = 317;

int n, m, rt, FA[N], dep[N], p[N][2], dfn[N], sz[N], idx, mxd, ans[M];
int lg[M];
int dfn2[N << 1], st[20][N << 1], idx2;
int pre[N], nxt[N];
int tot;
bool exi[N];
vi <int> G[N], d[N];

int a[N], s[N];

struct qry {
  int l, r, x, id;
  bool operator < (const qry & o) const {
    if (l / B != o.l / B) return l < o.l;
    return r > o.r;
  }
} q[M];

struct node {
  int x, y, z;
} stk[N];

bool END;

void add_e(int x, int y) {G[x].pb(y);}

void dfs(int x, int fa) {
  dep[x] = dep[fa] + 1, dfn[x] = ++idx, sz[x] = 1, d[dep[x]].pb(x);
  //  debug(x, dep[x]);
  mxd = max(mxd, dep[x]);
  dfn2[++idx2] = x, p[x][0] = idx2;
  for (auto y : G[x]) if (y ^ fa) dfs(y, x), sz[x] += sz[y];
  dfn2[++idx2] = x, p[x][1] = idx2;
}

int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  if (dfn[y] >= dfn[x] && dfn[y] < dfn[x] + sz[x]) return x;
  int l = p[x][0], r = p[y][0]; if (l > r) swap(l, r);
  int k = lg[r - l + 1];
  //  debug(x, y, l, r, k, st[k][l], st[k][r - (1 << k) + 1]);
  x = st[k][l], y = st[k][r - (1 << k) + 1];
  if (dep[x] < dep[y]) return FA[x];
  return FA[y];
}

int calc(int x, int y) {
  if (!y) return 0;
  if (!x) return dep[y] - 1;
  int lc = lca(x, y);
  //  debug(x, y, lc);
  return dep[y] - dep[lc] - 1;
}

void add(int x, int k) {if (x > 0) a[x] += k, s[x / B] += k;}

void del(int x, bool tp) {
  int l = pre[x], r = nxt[x];
  add(calc(l, x), -1), add(calc(x, r), -1), nxt[l] = r, pre[r] = l, add(calc(l, r), 1);
  if (tp) stk[++tot] = (node) {l, x, r};
}

void con(int x, int y) {
  nxt[x] = y, pre[y] = x;
  if (y) add(calc(x, y), 1);
}

void init(int l, int r) {

  for (int i = 1; i <= n; ++i) pre[i] = nxt[i] = a[i] = 0;
  for (int i = 1; i <= n / B + 1; ++i) s[i] = 0;

  for (int i = 1; i <= mxd; ++i) {
    int las = 0;
    for (auto x : d[i]) if (x >= l && x <= r) con(x, las), las = x;
    if (las) con(0, las), exi[i] = 1;
    //      else exi[i] = 0;
  }
}

void rb() {
  while (tot) {
    int x = stk[tot].x, y = stk[tot].y, z = stk[tot].z;
    add(calc(x, z), -1), con(x, y), con(y, z);
    tot--;
  }
}

int ask(int x) {
  int tmp = x / B, lim = mxd / B, sm = 0;
  for (int i = tmp + 1; i <= lim; ++i) sm += s[i];
  for (int i = x; i < (tmp + 1) * B; ++i) sm += a[i];
  return sm;
}

signed main() {
  cerr << (&END - &START) / 1024.0 / 1024.0 << endl;
  for (int i = 2; i < M; ++i) lg[i] = lg[i >> 1] + 1;
  IN(n), IN(m);
  for (int i = 1, x; i <= n; ++i) {
    IN(x), FA[i] = x; if (x) add_e(x, i); else rt = i;
  }
  dfs(rt, 0);
  for (int i = 1; i <= (n << 1); ++i) st[0][i] = dfn2[i];
  for (int i = 1; i < 20; ++i)
    for (int j = 1; j + (1 << i) - 1 <= (n << 1); ++j) {
      int x = st[i - 1][j], y = st[i - 1][j + (1 << (i - 1))];
      if (dep[x] < dep[y]) st[i][j] = x; else st[i][j] = y;
    }
  for (int i = 1; i <= m; ++i) IN(q[i].l), IN(q[i].r), IN(q[i].x), q[i].id = i;
  sort(q + 1, q + 1 + m);

  //  cerr << "$" << endl;

  for (int buf = 0, i = 1; buf * B <= n && i <= m; buf++) {
    int L = max(1, buf * B), R = min(n, (buf + 1) * B - 1);
    if (q[i].l > R) continue;
    //    debug(buf);
    init(L, n);
    int l = L, r = n;
    while (i <= m && q[i].l >= L && q[i].l <= R) {
      while (r > q[i].r) del(r, 0), r--;
      while (l < q[i].l) del(l, 1), l++;
      ans[q[i].id] = ask(q[i].x);
      rb(), i++;
    }
  }
  for (int i = 1; i <= m; ++i) write(ans[i]);
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 0ms
memory: 14692kb

input:

7 5
3 1 0 5 3 5 1
1 3 1
5 7 2
1 5 1
4 7 1
4 7 2

output:

2
1
3
3
1

result:

ok 5 number(s): "2 1 3 3 1"

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 24160kb

input:

1000 1000
686 337 192 336 405 0 108 485 350 762 258 780 179 939 25 657 571 662 119 786 604 224 935 494 685 575 369 178 249 740 954 204 598 592 68 771 498 86 55 38 298 704 239 292 993 286 16 813 719 187 14 476 792 49 944 52 227 720 310 470 900 243 663 950 627 300 728 189 45 610 673 548 873 95 48 841 ...

output:

467
67
672
143
391
602
463
35
608
588
197
292
576
145
643
343
234
464
28
284
166
246
210
141
274
279
94
236
197
29
9
15
57
10
9
16
87
162
19
217
232
24
178
334
103
139
293
400
299
351
529
632
592
296
640
678
715
708
52
465
322
731
83
93
291
356
405
61
40
63
17
217
76
147
25
142
584
189
418
7
55
547
...

result:

wrong answer 1st numbers differ - expected: '452', found: '467'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 151ms
memory: 40800kb

input:

50000 200000
42574 43129 47328 17982 40521 6668 12729 32377 201 11940 8599 11734 18349 41045 26854 22540 9897 33419 7463 1243 47272 27135 49050 49111 22435 42539 39924 20272 5843 9308 45963 3283 31185 13692 38952 20583 15885 24802 4773 953 49907 28689 36942 23550 19449 8970 33340 31665 5407 46023 18...

output:

12072
1321
12294
2444
32518
36534
38684
30520
16477
16703
47153
41194
23401
1770
18598
6831
26583
29765
8905
16295
38569
30061
22070
7660
3820
34754
17642
6469
12646
23461
11657
16511
24521
11503
3950
2214
37806
13036
29116
24114
39160
34788
37342
20876
11766
41253
44497
17822
23479
26717
43266
4139...

result:

wrong answer 1st numbers differ - expected: '12045', found: '12072'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 354ms
memory: 65816kb

input:

100000 1000000
6457 23693 90928 23592 90440 75018 16865 3342 83718 16731 95103 31510 38719 27886 29093 41955 6596 46409 51839 10527 91993 61074 14405 34833 53674 42363 11490 43757 46191 6058 59164 96938 57858 40178 97523 84164 21582 72243 11267 47368 97058 6637 95208 60092 53943 16441 28363 64965 52...

output:

52956
18767
1319
13405
11021
455
50595
81481
6813
3640
58937
10991
70
4713
36
9517
39731
1166
67346
74637
2667
45182
4914
6774
1625
4198
52270
30435
60137
48654
29768
2815
6846
73091
21944
49693
9923
46795
29787
6866
2435
20773
2959
34666
4377
2428
4582
7527
38292
7253
3586
63817
28075
43828
20215
1...

result:

wrong answer 514th numbers differ - expected: '65', found: '2067001658'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%