QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#861086 | #9676. Ancestors | nalemy | 0 | 7ms | 12108kb | C++20 | 2.1kb | 2025-01-18 16:08:05 | 2025-01-18 16:08:18 |
Judging History
answer
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
const int N = 1e5;
int n, m, rt, tt, pr[N], ans[N], ft[N+1];
std::vector<int>g[N];
std::set<int>p[N];
struct el { int i, a, b, c, k; } a[N*20];
void ad(int i, int k) {
for (i+=2; i<=n; i+=i&-i) ft[i] += k;
}
int qr(int i) { int k = 0;
for (i+=2; i; i-=i&-i) k += ft[i];
return k;
}
void inline mg(int i, std::set<int>&s, std::set<int>&t) {
auto md = [&](int x, int y) {
a[tt++] = {-1, i, x, pr[x], -1};
a[tt++] = {-1, i, x, pr[x]=y, 1};
};
for (int x : t) {
auto it = s.lower_bound(x);
if (it != s.end()) md(*it, x);
if (it != s.begin()) md(x, *std::prev(it));
s.insert(it, x);
}
}
std::vector<int>dfs(int u) {
std::vector<int>s, t;
for (int v : g[u]) {
if (t = dfs(v), s.size() < t.size()) s.swap(t);
for (int i=0; i<t.size(); i++)
if (p[s[i]].size() < p[t[i]].size())
mg(t.size()-i, p[t[i]], p[s[i]]), s[i] = t[i];
else mg(t.size()-i, p[s[i]], p[t[i]]);
}
return s.push_back(u), std::move(s);
}
void sv(int l, int r) {
if (r - l == 1) return;
int m = l + r >> 1, i, j;
for (sv(l, m), sv(m, r), i=m, j=l; i<r; i++) {
for (; j<m && a[j].b<=a[i].b; j++)
if (!~a[j].i) ad(a[j].c, a[j].k);
if (~a[i].i) ans[a[i].i] += a[i].k*qr(a[i].c);
}
while (j-- > l) if (!~a[j].i) ad(a[j].c, -a[j].k);
std::inplace_merge(a+l, a+m, a+r, [](el x, el y){return x.b^y.b?x.b<y.b:x.i<y.i;});
}
int main() {
std::cin >> n >> m;
for (int i=0, x; i<n; a[tt++]={-1, 0, i, pr[i]=-1, 1}, p[i]={i}, i++)
std::cin >> x, x ? g[x-1].push_back(i) : void(rt = i);
auto s = dfs(rt);
for (int i=0; i<s.size(); i++) for (auto u : p[s[i]])
a[tt++] = {-1, s.size()-i, u, pr[u], -1};
for (int i=0, l, r, x; i<m; i++) {
std::cin >> l >> r >> x;
// a[tt++] = {i, x, l-2, l-2, -1}, a[tt++] = {i, x, r-1, l-2, 1};
for (int j=0; j<tt; j++) if (a[j].a<=x && a[j].b>=l-1 && a[j].b<=r-1 && a[j].c<=l-2)
ans[i] += a[j].k;
}
// std::sort(a, a+tt, [](el x, el y){return x.a^y.a?x.a<y.a:x.i<y.i;}), sv(0, tt);
for (int i=0; i<m; i++) std::cout << ans[i] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 1ms
memory: 11864kb
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: 10052kb
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: 0
Wrong Answer
time: 7ms
memory: 12108kb
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:
431 352 286 500 185 352 324 29 370 27 401 11 252 197 185 30 73 171 42 107 128 70 75 60 42 69 41 18 37 18 6 21 39 22 46 14 37 35 52 176 66 74 159 269 129 39 310 206 280 119 373 89 5 460 198 124 246 259 132 450 114 292 51 387 193 266 103 1 119 180 307 138 55 263 135 78 68 356 34 97 216 228 239 108 140...
result:
wrong answer 1st numbers differ - expected: '441', found: '431'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #30:
score: 0
Time Limit Exceeded
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:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Time Limit Exceeded
Test #67:
score: 0
Time Limit Exceeded
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:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%