QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#858956 | #9676. Ancestors | juruoA | 0 | 438ms | 57724kb | C++11 | 5.8kb | 2025-01-17 10:47:57 | 2025-01-17 10:47:57 |
Judging History
answer
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
using std::bitset;
using std::cout;
using std::deque;
using std::endl;
using std::greater;
using std::lower_bound;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::multimap;
using std::multiset;
using std::nth_element;
using std::pair;
using std::priority_queue;
using std::queue;
using std::reverse;
using std::set;
using std::sort;
using std::sqrt;
using std::stable_sort;
using std::string;
using std::swap;
using std::unique;
using std::upper_bound;
using std::vector;
typedef int li;
typedef long double lf;
inline li read(){
int ans = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
f = (ch == '-') ? -1 : 1;
ch = getchar();
}
while(ch <= '9' && ch >= '0'){
ans = ans * 10 + (ch ^ 48);
ch = getchar();
}
return ans * f;
}
li n, m, rt;
vector<li> a[100010];
li dep[100010], lg[100010], fa[100010], dfn[100010], lendfn, pos[100010];
namespace LCA{
li st[100010][23];
void dfs(li u, li f){
fa[dfn[++lendfn] = u] = f;
pos[u] = lendfn;
dep[u] = dep[f] + 1;
for(li i = 0; i < a[u].size(); i++){
li v = a[u][i];
if(v == f) continue;
dfs(v, u);
}
}
void pre(li rt){
lg[0] = -1;
for(li i = 1; i <= n; i++) lg[i] = lg[i >> 1] + 1;
dfs(rt, 0);
for(li i = 1; i <= n; i++) st[i][0] = dfn[i];
for(li j = 1; j <= lg[n]; j++){
for(li i = 1; i + (1 << j) - 1 <= n; i++){
if(dep[st[i][j - 1]] < dep[st[i + (1 << (j - 1))][j - 1]]) st[i][j] = st[i][j - 1];
else st[i][j] = st[i + (1 << (j - 1))][j - 1];
}
}
}
li Lca(li u, li v){
if(u == v) return u;
if(pos[u] > pos[v]) swap(u, v);
li l = pos[u] + 1, r = pos[v];
li k = lg[r - l + 1];
if(dep[st[l][k]] > dep[st[r - (1 << k) + 1][k]]) return fa[st[r - (1 << k) + 1][k]];
return fa[st[l][k]];
}
}
using LCA::Lca;
// const li size_mo = 100;
li size_mo;
struct Query{
li l, r, x, id;
bool operator < (const Query b) const{
if(b.l / size_mo == l / size_mo){
if((b.l / size_mo) & 1) return r < b.r;
return r > b.r;
}
return l < b.l;
}
} q[1000010];
li ans[1000010], P[100010];
// set<li> V[100010];
struct BLOCK{
li a[100010], all;
const li size = 320;
li belong[100010], sum[100010], L[100010], R[100010];
void modify(li x, li add){
all += add;
a[x + 1] -= add;
sum[belong[x + 1]] -= add;
}
li query(li x){
li ans = all;
// for(li i = 1; i <= x; i++) ans += a[i];
for(li i = 1; i < belong[x]; i++) ans += sum[i];
for(li i = L[belong[x]]; i <= x; i++) ans += a[i];
return ans;
}
void init(){
for(li i = 1; i <= n; i++) belong[i] = i / size + 1, L[i] = 1e9, R[i] = 0;
for(li i = 1; i <= n; i++){
L[belong[i]] = min(L[belong[i]], i);
R[belong[i]] = max(R[belong[i]], i);
}
}
} tree;
struct DATA{
vector<li> pos, a;
li all, size;
li find_front(li x){
for(li i = x - 1; i; i -= 2){
if(a[i]) return pos[i];
if(a[i - 1]) return pos[i - 1];
}
return -1;
}
li find_back(li x){
for(li i = x + 1; i <= size; i += 3){
if(a[i]) return pos[i];
if(a[i + 1]) return pos[i + 1];
if(a[i + 2]) return pos[i + 2];
// if(a[i + 3]) return pos[i + 3];
// if(a[i + 4]) return pos[i + 4];
}
return -1;
}
void insert(li x){
all++;
// cout << "ins " << x << endl;
a[x] = 1;
}
void erase(li x){
all--;
a[x] = 0;
}
} V[100010];
void add(li id){
// cout << "add " << id << "(" << P[id] << ", " << dep[id] << ")" << endl;
li maxn = dep[id] - 1;
if(V[dep[id]].all){
li it = V[dep[id]].find_front(P[id]);
// cout << it << " ";
if(it != -1){
li lca = Lca(id, it);
maxn = min(maxn, dep[id] - dep[lca] - 1);
}
it = V[dep[id]].find_back(P[id]);
// cout << it << endl;
if(it != -1){
li lca = Lca(id, it);
maxn = min(maxn, dep[id] - dep[lca] - 1);
}
}
// cout << dep[id] << " ins " << P[id] << endl;
V[dep[id]].insert(P[id]);
tree.modify(maxn, 1);
}
void del(li id){
// cout << "del " << id << endl;
li maxn = dep[id] - 1;
V[dep[id]].erase(P[id]);
if(V[dep[id]].all){
li it = V[dep[id]].find_front(P[id]);
if(it != -1){
li lca = Lca(id, it);
maxn = min(maxn, dep[id] - dep[lca] - 1);
}
it = V[dep[id]].find_back(P[id]);
if(it != -1){
li lca = Lca(id, it);
maxn = min(maxn, dep[id] - dep[lca] - 1);
}
}
tree.modify(maxn, -1);
}
int main(){
// freopen("wonderful.ans", "r", stdin);
// freopen("www.ww", "w", stdout);
n = read(), m = read();
size_mo = n / sqrt(m) * 1.7;
// size_mo = sqrt(n);
for(li i = 1; i <= n; i++){
a[fa[i] = read()].push_back(i);
if(fa[i] == 0) rt = i;
}
LCA::pre(rt);
for(li i = 1; i <= m; i++){
q[i].l = read(), q[i].r = read(), q[i].id = i, q[i].x = read();
}
sort(q + 1, q + 1 + m);
for(li yy = 1; yy <= n; yy++){
li i = dfn[yy];
V[dep[i]].size++;
};
for(li yy = 1; yy <= n; yy++){
li i = dfn[yy];
V[dep[i]].a.resize(V[dep[i]].size + 5);
V[dep[i]].pos.resize(V[dep[i]].size + 5);
V[dep[i]].size = 0;
};
for(li yy = 1; yy <= n; yy++){
li i = dfn[yy];
V[dep[i]].pos[P[i] = ++V[dep[i]].size] = i;
// cout << i << " " << P[i] << " " << V[dep[i]].pos[P[i]] << endl;
}
tree.init();
li l = q[1].l, r = q[1].l - 1;
for(li i = 1; i <= m; i++){
while(l > q[i].l) add(--l);
while(r < q[i].r) add(++r);
while(l < q[i].l) del(l++);
while(r > q[i].r) del(r--);
ans[q[i].id] = tree.query(q[i].x);
}
for(li i = 1; i <= m; i++){
printf("%d\n", ans[i]);
}
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 0ms
memory: 22016kb
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: 0ms
memory: 22136kb
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: 19948kb
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: 0
Wrong Answer
time: 3ms
memory: 22080kb
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:
349 341 86 104 386 16 171 25 100 142 336 156 292 315 91 106 226 103 216 31 34 36 77 166 109 18 75 73 49 7 -6 2 41 62 12 126 122 50 125 152 100 123 26 0 106 34 173 52 184 89 268 270 152 77 39 323 155 52 2 286 148 201 51 145 27 14 94 75 140 231 73 191 328 188 36 -3 99 132 -9 18 150 70 -4 6 170 199 311...
result:
wrong answer 1st numbers differ - expected: '347', found: '349'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 17
Accepted
time: 201ms
memory: 38844kb
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: 327ms
memory: 33200kb
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: 438ms
memory: 31788kb
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:
14887 16270 8887 7182 15256 397 4267 7326 18639 383 16833 4360 7131 8958 4969 12539 10133 17882 16252 12355 16412 18840 14603 11204 1244 19105 3088 18276 7512 16290 9387 10045 5377 7511 8729 17164 18874 6159 1333 16848 16651 6023 2810 18123 9219 15664 18597 11872 10017 19369 6869 8123 19599 15477 43...
result:
wrong answer 1st numbers differ - expected: '14838', found: '14887'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 17
Accepted
time: 288ms
memory: 57724kb
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:
ok 1000000 numbers
Test #68:
score: 17
Accepted
time: 286ms
memory: 54640kb
input:
100000 1000000 82160 95864 48267 17482 19568 35077 14202 20440 4649 64145 148 2227 6969 39096 36508 20991 67700 90300 69215 57284 18492 9246 9629 7538 7845 30368 55600 48445 18542 41242 45052 25380 20894 91677 77040 73134 15572 21966 25343 14501 16227 23870 39207 50024 30787 11148 16884 63700 33205 ...
output:
44469 16565 2546 424 43423 23219 20745 3045 35596 11249 3763 322 27634 52470 20391 28546 14538 26343 9739 15234 20199 4332 1072 43633 3512 1432 47918 11347 4284 9054 6824 9773 39448 19949 18592 16534 43419 3303 11308 51071 1613 30619 8351 15408 6155 28590 13050 10085 2851 15353 33854 11870 5231 3742...
result:
ok 1000000 numbers
Test #69:
score: 0
Wrong Answer
time: 283ms
memory: 51684kb
input:
100000 1000000 25194 62545 57529 6618 90347 4663 56805 81144 16504 79307 43199 91078 67805 33396 46754 26525 75274 39333 20904 40286 76299 83579 79770 37838 8666 33691 40228 71445 16473 53091 68958 32037 72079 38640 89820 67042 68888 34997 65595 87669 425 95432 78811 30382 34738 51750 96653 87265 96...
output:
8483 8805 11404 18293 36068 18273 12910 15439 20610 3347 1471 13609 3954 8914 18719 6353 6811 15843 15270 27225 8776 2972 25 9854 18495 3513 18079 3695 11640 6560 6933 26252 14335 5139 9448 11571 12467 2416 2158 15835 34067 14974 30802 5131 21443 5006 2406 14357 13263 5561 8574 20829 32580 22129 151...
result:
wrong answer 1st numbers differ - expected: '8417', found: '8483'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%