QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#876662 | #9676. Ancestors | rlc202204 | 0 | 952ms | 329016kb | C++14 | 4.5kb | 2025-01-31 10:47:05 | 2025-01-31 10:47:07 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define mp(x, y) make_pair(x, y)
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int N = 1e5 + 5;
const int M = 1e6 + 5;
int n, m, rt;
vector<int> e[N];
int fa[N] = {0}, d[N] = {0};
vector<int> res[N];
void dfs(int x, int dth) {
res[d[x] = dth].push_back(x);
for (auto i: e[x])
dfs(i, dth + 1);
}
int tot = 0;
struct Node {
int op, x, y, z, f, id;
Node (int _op = 0, int _x = 0, int _y = 0, int _z = 0, int _f = 0, int _id = 0) :
op(_op), x(_x), y(_y), z(_z), f(_f), id(_id) {}
} a[M * 6], b[M * 6];
int p[N] = {0};//虚树父亲
set<int> g[N];//虚树节点
void build(int t) {
for (auto i: res[t]) {
if (e[i].size() >= 2 || i == rt) {
for (auto j: e[i])
p[j] = i, g[i].insert(j);
}
else {
if (e[i].size())
p[e[i][0]] = p[i], g[p[i]].insert(e[i][0]);
g[p[i]].erase(i);
}
}
}
int sz[N] = {0}, chd[N] = {0};
int getsz(int x) {
sz[x] = g[x].empty(), chd[x] = -1;
for (auto i: g[x]) {
sz[x] += getsz(i);
if (chd[x] == -1 || sz[chd[x]] < sz[i])
chd[x] = i;
}
return chd[x];
}
piii c[M * 2];
int len = 0;
void upd(int x, int t, int cl) {
if (chd[x] == -1)
c[++len] = mp(t, mp(x, cl));
for (auto i: g[x])
upd(i, t, cl);
}
int srh(int x, int nw) {
if (chd[x] == -1)
return x;
for (auto i: g[x])
if (i != chd[x])
srh(i, nw);
int col = srh(chd[x], nw);
for (auto i: g[x])
if (i != chd[x])
upd(i, nw - d[x], col);
return col;
}
set<int> pos[N];
int arr[N] = {0};
void initc() {
c[++len] = mp(0, mp(rt, 0));
sort(c + 1, c + len + 1);
for (int i = 1; i <= n; i++) {
arr[i] = i, pos[i].insert(0), pos[i].insert(i);
a[++tot] = Node(0, 0, 0, i, 1);
}
for (int i = 1; i <= len; i++) {
int t = c[i].first, x = c[i].second.first, y = c[i].second.second;
// printf("at time %d: mdf %d to %d\n", t, x, y);
auto cur = pos[arr[x]].lower_bound(x);
cur--;
int prx = *cur;
a[++tot] = Node(0, t, *cur, x, -1);
cur++, cur++;
if (cur != pos[arr[x]].end()) {
a[++tot] = Node(0, t, x, *cur, -1);
a[++tot] = Node(0, t, prx, *cur, 1);
}
pos[arr[x]].erase(x);
arr[x] = y;
if (arr[x]) {
pos[arr[x]].insert(x);
cur = pos[arr[x]].lower_bound(x);
cur--;
prx = *cur;
a[++tot] = Node(0, t, prx, x, 1);
cur++, cur++;
if (cur != pos[arr[x]].end()) {
a[++tot] = Node(0, t, prx, *cur, -1);
a[++tot] = Node(0, t, x, *cur, 1);
}
}
}
// for (int i = 1; i <= tot; i++)
// printf("(%d, %d, %d, v:%d)\n", a[i].x, a[i].y, a[i].z, a[i].f);
}
bool cmp(Node u, Node v) {
if (u.x != v.x)
return u.x < v.x;
if (u.y != v.y)
return u.y < v.y;
if (u.z != v.z)
return u.z < v.z;
return u.op < v.op;
}
int t[N] = {0};
int lowbit(int x) {
return x & -x;
}
void mdf(int x, int v) {
while (x <= n)
t[x] += v, x += lowbit(x);
}
int qry(int x) {
int ans = 0;
while (x)
ans += t[x], x -= lowbit(x);
return ans;
}
void clr(int x) {
while (x <= n)
t[x] = 0, x += lowbit(x);
}
int ans[M] = {0};
void slv(int l, int r) {
if (l + 1 == r)
return;
int mid = (l + r) / 2;
slv(l, mid), slv(mid, r);
for (int i = mid, j = l; i < r; i++) {
while (j < mid && a[j].y < a[i].y) {
if (a[j].op == 0)
mdf(a[j].z, a[j].f);
j++;
}
if (a[i].op == 1)
ans[a[i].id] += a[i].f * qry(a[i].z);
}
for (int i = l; i < mid; i++)
if (a[i].op == 0)
clr(a[i].z);
for (int i = l, j = mid, cur = l; i < mid || j < r; )
if (j == r || (i < mid && a[i].y < a[j].y))
b[cur++] = a[i++];
else
b[cur++] = a[j++];
for (int i = l; i < r; i++)
a[i] = b[i];
}
void show(int x) {
for (auto i: g[x])
printf("%d -> %d\n", x, i);
for (auto i: g[x])
show(i);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &fa[i]);
if (!fa[i])
rt = i;
else
e[fa[i]].push_back(i);//, cout << fa[i] << " -> " << i << endl;
}
dfs(rt, 1);
for (int i = 1; res[i + 1].size(); i++) {
// printf("d:%d\n", i);
build(i);
getsz(rt);
srh(rt, i + 1);
upd(rt, i + 1, 0);
// show(rt);
}
initc();
for (int i = 1, l, r, k; i <= m; i++) {
scanf("%d%d%d", &l, &r, &k);
a[++tot] = Node(1, k, l, l - 1, -1, i);
a[++tot] = Node(1, k, l, r, 1, i);
}
sort(a + 1, a + tot + 1, cmp);
slv(1, tot + 1);
for (int i = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 11
Accepted
time: 15ms
memory: 305812kb
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: 13ms
memory: 306200kb
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: 20ms
memory: 305788kb
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
Runtime Error
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:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #30:
score: 17
Accepted
time: 273ms
memory: 316676kb
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: 0
Time Limit Exceeded
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:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Time Limit Exceeded
Test #67:
score: 17
Accepted
time: 952ms
memory: 329016kb
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: 0
Time Limit Exceeded
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:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%