QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#859496 | #9676. Ancestors | lfxxx | 0 | 1733ms | 40032kb | C++17 | 3.3kb | 2025-01-17 19:50:04 | 2025-01-17 19:50:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
bool be;
constexpr int N = 1e5 + 5, Q = 1e6 + 5;
int n, m, b, rt, fa[N], dfn[N], dep[N], cnt, st[N][17], p[N], in[N], pre[N], nxt[N], ans[Q];
vector<int>e[N];
struct Query {
int l, r, x, id;
}q[Q];
struct ds {
int a[N], sum[350], in[N], b;
void init()
{
b = sqrt(n);
for (int i = 1; i <= n; ++i) in[i] = (i + b - 1) / b;
}
void add(int x, int v)
{
a[x] += v;
sum[in[x]] += v;
}
int query(int x)
{
int ans = 0;
for (int i = 1; i < in[x]; ++i) ans += sum[i];
for (int i = (in[x] - 1) * b + 1; i <= x; ++i) ans += a[i];
return ans;
}
}d;
void dfs(int u)
{
dfn[u] = ++cnt;
st[cnt][0] = fa[u];
dep[u] = dep[fa[u]] + 1;
for (int v : e[u]) {
dfs(v);
}
}
int get(int u, int v)
{
return dfn[u] < dfn[v] ? u : v;
}
int LCA(int u, int v)
{
if (u == v) return u;
if (dfn[u] > dfn[v]) swap(u, v);
int k = __lg(dfn[v] - dfn[u]);
return get(st[dfn[u] + 1][k], st[dfn[v] - (1 << k) + 1][k]);
}
void work(int x, int y, int z)
{
if (y == 0) return;
if (x == 0 || dep[x] != dep[y]) d.add(dep[y], z);
else d.add(dep[x] - dep[LCA(x, y)], z);
}
void del(int x)
{
work(pre[x], x, -1);
work(x, nxt[x], -1);
work(pre[x], nxt[x], 1);
pre[nxt[x]] = pre[x];
nxt[pre[x]] = nxt[x];
}
void add(int x)
{
work(pre[x], x, 1);
work(x, nxt[x], 1);
work(pre[x], nxt[x], -1);
pre[nxt[x]] = x;
nxt[pre[x]] = x;
}
bool en;
int main()
{
#ifdef IAKIOI
cerr << (&be - &en) / 1024.0 / 1024 << " MB\n----------------------------" << endl;
freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> fa[i];
if (fa[i] == 0) rt = i;
else e[fa[i]].emplace_back(i);
}
dfs(rt);
for (int j = 1; j <= __lg(n); ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
st[i][j] = get(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
d.init();
b = max(1, (int)(n / sqrt(m)));
for (int i = 1; i <= n; ++i) {
in[i] = (i + b - 1) / b;
}
iota(p + 1, p + 1 + n, 1);
sort(p + 1, p + 1 + n, [](int x, int y) {
if (dep[x] != dep[y]) return dep[x] < dep[y];
return dfn[x] < dfn[y];
});
for (int i = 1; i <= n; ++i) {
pre[p[i]] = p[i - 1];
nxt[p[i]] = p[i + 1];
}
for (int i = 1; i <= m; ++i) {
cin >> q[i].l >> q[i].r >> q[i].x;
q[i].id = i;
}
sort(q + 1, q + 1 + m, [](Query a, Query b) {
if (in[a.l] != in[b.l]) return a.l < b.l;
return a.r > b.r;
});
int l = 0, r = -1;
for (int i = 1; i <= m; ++i) {
if (in[l] != in[q[i].l]) {
while (r >= l) del(r--);
pre[0] = nxt[0] = 0;
for (int j = l; j <= n; ++j) pre[i] = nxt[i] = 0;
l = (in[q[i].l] - 1) * b + 1;
r = n;
int lst = 0;
for (int j = 1; j <= n; ++j) {
if (l <= p[j] && p[j] <= r) {
work(lst, p[j], 1);
pre[p[j]] = lst;
nxt[lst] = p[j];
lst = p[j];
}
}
}
if (m > 400000) continue;
while (q[i].r < r) del(r--);
int pl = l;
while (q[i].l > l) del(l++);
ans[q[i].id] = r - l + 1 - d.query(q[i].x);
while (pl < l) add(--l);
}
for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
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: 15984kb
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: 11
Accepted
time: 1ms
memory: 12096kb
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:
452 67 611 126 329 486 354 25 559 585 184 265 576 116 489 289 147 287 13 282 151 192 146 141 148 131 43 72 69 29 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 2 69 110 286 172 0 40 31 16 105 75 45 8 94 540 115 357 7 11 431 581 37 ...
result:
ok 1000 numbers
Test #3:
score: 11
Accepted
time: 1ms
memory: 13924kb
input:
1000 1000 594 766 788 546 408 364 152 525 963 359 339 746 747 58 628 60 144 646 222 863 418 1000 494 84 225 21 202 146 81 879 239 526 662 816 483 140 7 834 978 26 370 619 12 112 824 319 855 852 877 32 708 236 296 791 40 102 238 930 734 609 740 309 982 837 272 451 825 977 717 597 761 90 305 224 216 8...
output:
441 362 300 535 197 383 360 29 428 31 473 9 310 242 232 37 96 227 50 146 180 93 108 83 58 106 61 26 61 32 7 25 54 25 59 17 48 42 68 237 83 95 206 339 161 47 377 250 335 136 436 98 6 515 216 132 263 270 137 463 117 292 61 468 237 326 149 0 126 228 353 168 76 332 169 98 75 418 49 133 284 281 282 134 1...
result:
ok 1000 numbers
Test #4:
score: 11
Accepted
time: 2ms
memory: 16152kb
input:
1000 1000 330 124 926 768 265 445 803 501 173 371 897 686 88 668 787 5 440 818 604 119 918 821 30 400 391 262 299 334 279 58 45 101 814 582 467 182 538 549 455 761 121 769 480 639 605 906 883 341 138 862 684 42 855 535 778 154 407 439 967 780 128 565 622 739 910 69 647 612 345 407 929 352 296 869 31...
output:
347 340 86 105 386 17 172 27 104 146 341 160 298 321 97 112 232 110 222 37 40 42 84 173 114 25 82 81 56 14 1 2 41 62 12 126 122 50 125 152 100 123 26 0 106 34 173 52 184 89 268 270 152 77 39 322 154 50 0 284 146 199 54 149 27 18 101 84 140 232 73 199 334 195 35 1 107 138 1 25 158 78 4 10 178 202 315...
result:
ok 1000 numbers
Test #5:
score: 11
Accepted
time: 3ms
memory: 16020kb
input:
1000 1000 461 631 908 871 9 818 589 859 917 846 415 498 899 405 954 857 192 327 912 204 453 253 923 705 211 775 882 41 956 634 761 588 676 694 428 729 575 747 216 979 10 472 471 422 580 944 295 121 62 603 376 520 720 864 792 22 891 773 77 520 801 680 590 70 617 807 784 136 566 699 167 726 75 701 780...
output:
95 177 33 70 54 125 39 35 0 263 17 65 55 46 144 73 55 152 58 145 194 103 68 101 34 32 83 60 43 24 0 1 23 46 51 114 57 27 116 38 86 70 53 52 147 129 136 46 41 14 137 154 161 159 205 6 149 157 21 24 7 50 81 9 15 53 13 134 225 36 32 3 104 44 73 139 35 24 191 94 61 128 399 29 103 2 31 34 23 116 9 91 158...
result:
ok 1000 numbers
Test #6:
score: 11
Accepted
time: 1ms
memory: 14088kb
input:
1000 1000 287 431 325 595 467 406 982 985 603 656 520 452 564 548 370 451 331 407 538 749 304 421 813 467 941 545 807 796 484 617 276 908 461 162 323 903 455 796 466 897 972 859 359 684 644 348 245 579 149 18 702 537 415 64 906 382 210 478 145 369 128 336 574 86 847 475 671 849 135 238 920 421 512 9...
output:
117 162 29 84 31 73 55 52 148 119 26 88 88 186 44 96 41 39 276 8 27 81 60 38 58 13 5 17 17 32 8 14 26 24 23 10 58 52 140 6 63 14 66 99 185 207 51 163 61 13 1 183 252 70 87 42 45 127 108 23 92 70 4 35 136 143 7 34 7 53 31 57 39 11 24 132 13 0 23 39 37 13 16 60 25 5 10 130 0 47 5 1 103 54 45 24 31 176...
result:
ok 1000 numbers
Test #7:
score: 0
Wrong Answer
time: 2ms
memory: 11860kb
input:
1000 1000 744 630 563 578 427 227 792 774 559 165 201 450 227 905 966 321 29 908 183 636 603 275 148 739 196 551 884 685 487 276 139 646 352 52 96 246 212 295 502 968 1000 938 121 100 326 309 567 582 744 290 170 312 464 262 768 179 921 741 4 431 107 465 240 973 526 369 172 744 580 420 134 566 875 85...
output:
65 71 39 51 42 31 29 29 3 25 170 6 3 38 58 97 9 11 65 3 27 29 136 24 108 67 83 54 38 13 0 11 58 22 33 17 6 5 50 96 75 13 60 32 239 165 20 49 16 69 73 280 98 29 341 67 25 21 46 69 101 65 12 134 81 3 61 10 5 4 42 46 23 88 39 153 96 55 14 31 1 3 48 57 42 4 24 95 41 68 13 24 20 0 9 28 190 53 35 79 68 10...
result:
wrong answer 82nd numbers differ - expected: '0', found: '3'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 1733ms
memory: 21096kb
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:
12045 1321 12292 2444 32443 36534 38575 30505 16464 16648 47153 41144 23401 1762 18567 6831 26486 29716 8905 16295 38569 29990 22061 7638 3820 34754 17582 6466 12632 23384 11657 16493 24434 11503 3945 2205 37806 13036 29052 24114 39158 34702 37326 20815 11766 41253 44355 17807 23452 26717 43107 4133...
result:
wrong answer 116806th numbers differ - expected: '0', found: '530'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 0
Wrong Answer
time: 261ms
memory: 40032kb
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:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 1st numbers differ - expected: '52956', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%