QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#864373#9676. AncestorsGodzilla0 346ms65668kbC++234.8kb2025-01-20 15:24:322025-01-20 15:24:34

Judging History

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

  • [2025-01-20 15:24:34]
  • 评测
  • 测评结果:0
  • 用时:346ms
  • 内存:65668kb
  • [2025-01-20 15:24:32]
  • 提交

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 = 316;

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)
    if (l == 1 || exi[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: 2ms
memory: 16548kb

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: 17316kb

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
589
197
293
576
145
644
344
235
465
28
286
168
248
212
141
276
281
94
238
199
37
5
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
85
95
292
356
405
62
40
63
17
218
76
148
25
143
584
190
419
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: 134ms
memory: 38704kb

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
2452
32443
36534
38686
30522
16479
16705
47153
41144
23490
1767
18598
6831
26585
29716
8920
16295
38576
30065
22076
7638
3828
34754
17597
6490
12632
23414
11690
16493
24471
11548
3945
2206
37978
13036
29125
24126
39158
34801
37326
20880
11771
41299
44513
17807
23452
26761
43283
4133...

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: 346ms
memory: 65668kb

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: '-437291787'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%