QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#860881 | #9676. Ancestors | nalemy | 0 | 344ms | 44832kb | C++20 | 2.0kb | 2025-01-18 15:25:53 | 2025-01-18 15:26:05 |
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::vector<std::pair<int, int>>rg[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;
}
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++)
rg[t.size()-i].push_back({s[i], t[i]});
}
return s.push_back(u), std::move(s);
}
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);
}
}
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=1; i<s.size(); i++) {
for (auto u : p[i-1]) a[tt++] = {-1, i, u, pr[u], -1};
for (auto[u, v] : rg[i]) p[u].size() < p[v].size()
? mg(i, p[v], p[u]), p[u].swap(p[v]) : mg(i, p[u], p[v]);
}
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};
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: 0ms
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: 0
Wrong Answer
time: 2ms
memory: 12160kb
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:
467 72 672 143 391 602 463 35 713 681 291 435 585 208 521 489 348 425 45 361 329 297 265 233 201 169 137 105 73 41 9 0 0 0 0 0 0 0 0 59 64 0 0 241 0 0 86 290 93 183 476 614 547 122 602 650 695 680 0 430 297 730 3 128 110 203 213 3 92 0 4 303 0 74 0 51 544 172 514 0 0 526 645 40 264 0 444 190 0 0 219...
result:
wrong answer 1st numbers differ - expected: '452', found: '467'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 344ms
memory: 44832kb
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:
12072 1327 12370 2475 32809 37028 39185 31040 16761 16995 48336 42266 24077 1825 19174 7069 27509 30915 9298 17017 40463 31502 23214 8070 4021 36889 18667 6870 13467 25014 12480 17705 26332 12391 4251 2389 41200 14194 31780 26412 43116 38273 41276 23016 13035 44895 44784 19874 26233 30012 44340 4422...
result:
wrong answer 1st numbers differ - expected: '12045', found: '12072'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Runtime Error
Test #67:
score: 0
Runtime Error
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%