QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#858305 | #9676. Ancestors | c# | 0 | 231ms | 85056kb | C++23 | 6.0kb | 2025-01-16 15:52:57 | 2025-01-16 15:52:58 |
Judging History
answer
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\33[32m[" << __LINE__ << "]\33[m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= f;
}
const int N = 1e5 + 10, M = 1e6 + 10, K = N * 100;
int n, m, fa[N], rt, ans[N], s[N], sz[N], son[N];
vector <int> v[N];
struct Q {
int l, r, x, id;
} q[M], tmpq[M];
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(pair <int, int> x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(((uint64_t) x.first << 32 | x.second) + FIXED_RANDOM);
}
};
void dfs(int x) {
sz[x] = 1;
for (int i: v[x]) {
dfs(i);
sz[x] += sz[i];
if (sz[i] > sz[son[x]]) son[x] = i;
}
}
vector <set <int>> vw[N];
vector <pair <pair <int, int>, int>> tt[N];
int k;
struct O {
int l, r, x, v;
} o[K], tmpo[K];
void ins(int x, int l, int r, int v) {
if (l <= r) tt[x].emplace_back(make_pair(l, r), v);
}
void dfs2(int x) {
if (son[x]) {
dfs2(son[x]);
swap(vw[x], vw[son[x]]);
for (int i: v[x]) if (i != son[x]) {
dfs2(i);
while (vw[x].size() < vw[i].size()) vw[x].insert(vw[x].begin(), set <int> {});
F(j, 0, SZ(vw[i])) {
int jj = SZ(vw[x]) - (SZ(vw[i]) - j), pos = vw[i].size() - j;
// if (x == 5) {
// debug << i << ' ' << vw[i][j].size() << endl;
// }
int lst = 0;
for (int k: vw[i][j]) {
auto it = vw[x][jj].insert(k).first, iit = next(it);
int l = it == vw[x][jj].begin() ? 1 : * prev(it) + 1, r = iit == vw[x][jj].end() ? n : * iit - 1;
// if () l = * prev(it) + 1;
// else l = 1;
// if (iit == vw[x][jj].end()) r = n;
// else r = * iit - 1;
ins(pos, l, r, - 1);
ins(pos, l, k - 1, 1);
ins(pos, k + 1, r, 1);
ins(pos, lst + 1, k - 1, - 1);
lst = k;
}
ins(pos, lst + 1, n, - 1);
}
vector <set <int>> ().swap(vw[i]);
}
}
s[vw[x].size()]++;
vw[x].push_back(set <int> {x});
ins(1, 1, x - 1, 1);
ins(1, x + 1, n, 1);
}
int info[N], tim[N], cur;
void mdf(int x, int y) {
for (; x <= n; x += x & - x)
if (tim[x] != cur) tim[x] = cur, info[x] = y;
else info[x] += y;
}
int qry(int x) {
int s = 0;
for (; x; x ^= x & - x)
(tim[x] == cur) && (s += info[x]);
return s;
}
void cdq(int l1, int r1, int l2, int r2, int l3, int r3) {
if (l1 > r1 || l2 > r2 || l3 == r3) {
if (l1 <= r1) {
sort(q + l1, q + r1 + 1, [&] (Q x, Q y) {
return x.r < y.r;
});
}
if (l2 <= r2) {
sort(o + l2, o + r2 + 1, [&] (O x, O y) {
return x.r < y.r;
});
}
return;
}
int mid = (l3 + r3) >> 1;
F(i, l1, r1) tmpq[i] = q[i];
// F(i, l2, r2) tmpo[i] = o[i];
int s1 = l1 - 1;
F(i, l1, r1)
if (tmpq[i].x <= mid) q[++s1] = tmpq[i];
int mid1 = s1;
F(i, l1, r1)
if (tmpq[i].x > mid) q[++s1] = tmpq[i];
// int s2 = l2 - 1;
int mid2 = l2 - 1;
F(i, l2, r2)
if (o[i].x <= mid) mid2++;
// int mid2 = s2;
// F(i, l2, r2)
// if (tmpo[i].x > mid) o[++s2] = tmpo[i];
cdq(l1, mid1, l2, mid2, l3, mid);
cdq(mid1 + 1, r1, mid2 + 1, r2, mid + 1, r3);
cur++;
int pos = mid2;
DF(i, r1, mid1 + 1) {
while (pos >= l2 && q[i].r <= o[pos].r) mdf(o[pos].l, o[pos].v), pos--;
// debug << qry(q[i].l) << " " << q[i].id << " " << pos << " " << mid2 << " " << l2 << endl;
// if (q[i].id == 2) {
// F(j, pos + 1, mid2) {
// debug << o[j].l << " " << o[j].v << endl;
// }
// debug << q[i].x << " " << mid + 1 << " " << r3 << " " << q[i].l << " " << qry(q[i].l) << endl;
// }
ans[q[i].id] -= qry(q[i].l);
}
{
F(i, l1, r1) tmpq[i] = q[i];
int p1 = l1, p2 = mid1 + 1, pp = l1;
while (p1 <= mid1 || p2 <= r1) {
if (p1 <= mid1 && (p2 > r1 || tmpq[p1].r < tmpq[p2].r)) q[pp++] = tmpq[p1++];
else q[pp++] = tmpq[p2++];
}
}
{
F(i, l2, r2) tmpo[i] = o[i];
int p1 = l2, p2 = mid2 + 1, pp = l2;
while (p1 <= mid2 || p2 <= r2) {
if (p1 <= mid2 && (p2 > r2 || tmpo[p1].r < tmpo[p2].r)) o[pp++] = tmpo[p1++];
else o[pp++] = tmpo[p2++];
}
}
}
signed main() {
read(n), read(m);
F(i, 1, n)
if (read(fa[i])) v[fa[i]].push_back(i);
else rt = i;
dfs(rt), dfs2(rt);
F(j, 0, SZ(vw[rt])) {
int pos = vw[rt].size() - j;
// if (x == 5) {
// debug << i << ' ' << vw[i][j].size() << endl;
// }
int lst = 0;
for (int k: vw[rt][j]) {
ins(pos, lst + 1, k - 1, - 1);
lst = k;
}
ins(pos, lst + 1, n, - 1);
}
F(i, 1, n) {
unordered_map <pair <int, int>, int, custom_hash> id;
for (auto [a, b]: tt[i]) id[a] += b;
// tt[i].clear();
vector <pair <pair <int, int>, int>> ().swap(tt[i]);
for (auto [a, b]: id)
if (b) o[++k] = {a.first, a.second, i - 1, b};//, debug << i << " " << a.first << ' ' << a.second << ' ' << b << endl;
}
DF(i, n, 1) s[i] += s[i + 1];
F(i, 1, m) read(q[i].l), read(q[i].r), ans[i] = s[read(q[i].x)], q[i].id = i;
// F(i, 1, m) debug << ans[i] << '\n';
cdq(1, m, 1, k, 0, n);
F(i, 1, m) cout << ans[i] << '\n';
return 0;
}
/* why?
*/
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 1ms
memory: 9840kb
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: 3ms
memory: 12492kb
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: 4ms
memory: 12432kb
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: 1ms
memory: 12340kb
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: 0
Wrong Answer
time: 4ms
memory: 10316kb
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:
96 178 34 71 55 126 40 36 1 264 18 66 56 47 145 74 56 153 59 146 195 104 69 102 35 33 84 61 44 25 1 2 24 47 52 115 58 28 117 39 87 71 54 53 148 130 137 47 42 15 138 155 162 160 206 7 150 158 22 25 8 51 82 10 16 54 14 135 226 37 33 4 105 45 74 140 36 25 192 95 62 129 399 30 104 3 32 35 24 117 10 92 1...
result:
wrong answer 1st numbers differ - expected: '95', found: '96'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 17
Accepted
time: 209ms
memory: 55284kb
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:
ok 200000 numbers
Test #31:
score: 17
Accepted
time: 201ms
memory: 47796kb
input:
50000 200000 13611 7267 47234 19765 39458 30493 19935 34465 46369 11995 35207 5752 13718 47418 12913 13725 27694 47726 25253 30406 27587 25790 21091 12008 13534 8804 26484 34270 33810 49102 25288 37607 11863 23340 22747 40502 4461 24803 31317 25617 46077 6504 46299 34533 6829 17841 2812 35413 16394 ...
output:
14606 766 20003 9866 13393 15046 12175 16064 14351 32598 8598 4328 20759 18242 14279 16634 19676 27649 22510 18209 10566 6103 7184 10324 11352 25597 17392 26556 19067 28802 18806 8556 16176 19224 25066 21928 3542 3540 2793 21786 15621 831 18062 6304 5590 24262 14451 14273 10795 9579 10524 6779 16154...
result:
ok 200000 numbers
Test #32:
score: 0
Wrong Answer
time: 213ms
memory: 45340kb
input:
50000 200000 7808 37218 25681 42081 13175 20056 26651 36373 24916 22399 6718 4909 8913 8722 43481 17050 49628 37628 9367 22930 27058 40654 5776 8531 26430 24332 39623 5383 17932 44070 29404 47256 19162 25499 2140 3864 43779 46852 10360 40725 38332 16051 16623 9761 9775 35907 31158 36636 39585 10652 ...
output:
14840 16219 8836 7131 15205 342 4212 7271 18581 325 16775 4300 7071 8899 4910 12479 10074 17825 16196 12299 16357 18785 14543 11144 1184 19044 3027 18215 7452 16227 9325 9984 5315 7450 8669 17103 18813 6098 1272 16784 16590 5962 2745 18058 9154 15600 18525 11800 9947 19294 6795 8051 19529 15407 4259...
result:
wrong answer 1st numbers differ - expected: '14838', found: '14840'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 0
Wrong Answer
time: 231ms
memory: 85056kb
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 -4982 50595 81481 6813 3640 58937 10991 -380 4713 -6051 9517 39731 1166 67346 74637 2667 45182 4298 6774 1625 4198 52270 30435 60137 48654 29768 2815 6846 73091 21944 49693 9923 46795 29787 6725 2435 20773 2959 34666 4377 322 4166 7527 38292 7253 3586 63817 28075 43828 2...
result:
wrong answer 6th numbers differ - expected: '455', found: '-4982'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%