QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#857974 | #9676. Ancestors | 369Pai | 0 | 16ms | 24304kb | C++14 | 4.8kb | 2025-01-16 11:35:53 | 2025-01-16 11:35:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int LG = 18 , N = 1e5 + 5 , M = 1e6 + 5;
int n , m , rt , d , fa[N] , dep[N] , lst[N] , vis[N] , dsu[N];
int num , tot , dfn[N] , id[N] , ans[M];
vector<int>g[N] , vc[N] , tr[N]; set<int>st[N];
struct QU{int tp , l , r , x , k;}qu[M * 5];
struct ST
{
int st[LG][N];
int Min(int u , int v){return dep[u] < dep[v] ? u : v;}
void init()
{
for(int i = 1 ; i <= n ; i++)
st[0][dfn[i]] = i;
for(int i = 1 ; i < LG ; i++)
for(int j = 1 ; j + (1 << i) - 1 <= n ; j++)
st[i][j] = Min(st[i - 1][j] , st[i - 1][j + (1 << (i - 1))]);
}
int ask(int l , int r)
{
int t = __lg(r - l + 1);
return Min(st[t][l] , st[t][r - (1 << t) + 1]);
}
}stb;
int LCA(int u , int v)
{
if(u == v)return u;
if(dfn[u] > dfn[v])swap(u , v);
return fa[stb.ask(dfn[u] + 1 , dfn[v])];
}
void Dfs(int u)
{
dep[u] = dep[fa[u]] + 1;
dfn[u] = ++num , id[num] = u;
for(int v : g[u])
{
if(v == fa[u])continue ;
Dfs(v);
}
}
int Find(int x){return x == dsu[x] ? x : dsu[x] = Find(dsu[x]);}
void Dfs2(int u)
{
// cerr << " dfs " << u << "\n";
for(int v : tr[u])
{
Dfs2(v);
int fu = Find(u) , fv = Find(v);
auto &su = st[fu] , &sv = st[fv];
if(su.size() < sv.size())swap(su , sv);
auto add = [&](int z)
{
// cerr << "\t" << z << ',' << lst[z] << " [" << vis[z] << ',' << d - dep[u] << ")\n";
qu[++tot] = {1 , lst[z] , z , vis[z] , 1};
qu[++tot] = {1 , lst[z] , z , d - dep[u] , -1};
vis[z] = d - dep[u];
};
for(int z : sv)
{
auto it = su.lower_bound(z);
// cerr << " ins " << z << "\n";
if(it != su.begin())
{
auto pr = prev(it);
if(Find(*pr) != Find(v))
{
add(z); lst[z] = *pr;
}
}
if(it != su.end())
{
auto nx = sv.upper_bound(z);
if(nx == sv.end() || *nx > *it)
{
int t = *it;
add(t); lst[t] = z;
}
}
su.insert(z);
}
sv.clear() , dsu[fv] = fu;
}
// cerr << " " << u << ' ' << Find(u) << ": {";
// for(int z : st[Find(u)])cerr << z << ",";
// cerr << "} ; ";
// for(int i = 1 ; i <= n ; i++)
// cerr << Find(i) << " \n"[i == n];
}
struct BIT
{
int c[N];
void add(int x , int v)
{
for(int i = x ; i <= n ; i += i & -i)
c[i] += v;
}
int ask(int x)
{
int sum = 0;
for(int i = x; i ; i -= i & -i)
sum += c[i];
return sum;
}
}bit;
void Calc(int l , int r)
{
if(l == r)return ;
int mid = (l + r) >> 1;
Calc(l , mid);
Calc(mid + 1 , r);
for(int i = l , j = mid + 1 ; i <= mid + 1 ; i++)
{
for(; j <= r && (i > mid || qu[j].l > qu[i].l + 1) ; j++)
{
if(!qu[j].tp)ans[qu[j].k] += bit.ask(qu[j].r) - bit.ask(qu[j].l - 1);
}
if(i <= mid)
{
if(qu[i].tp)bit.add(qu[i].r , qu[i].k);
}
}
for(int i = l ; i <= mid ; i++)
if(qu[i].tp)bit.add(qu[i].r , -qu[i].k);
inplace_merge(qu + l , qu + mid + 1 , qu + r + 1 , [&](const QU &a , const QU &b){return a.l == b.l ? a.tp < b.tp : a.l > b.l;});
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
cin >> n >> m;
for(int i = 1 ; i <= n ; i++)
{
cin >> fa[i];
if(!fa[i])rt = i;
else g[fa[i]].push_back(i);
}
for(int i = 1 ; i <= m ; i++)
{
int l , r , x; cin >> l >> r >> x;
qu[i] = {0 , l , r , x , i};
// ans[i] = r - l + 1;
}
Dfs(rt);
stb.init();
tot = m;
for(int i = 1 ; i <= n ; i++)
vc[dep[i]].push_back(i);
auto cmp = [&](int u , int v){return dfn[u] < dfn[v];};
for(d = 1 ; d <= n ; d++)
{
vector<int>p = vc[d];
if(p.empty())continue ;
for(int u : p)
{
dsu[u] = u , vis[u] = lst[u] = 0;
st[u].insert(u);
}
sort(p.begin() , p.end() , cmp);
int sz = p.size();
for(int i = 0 ; i + 1 < sz ; i++)
p.push_back(LCA(p[i] , p[i + 1]));
sort(p.begin() , p.end() , cmp);
unique(p.begin() , p.end());
// cerr << "dep = " << d << ":\n";
sz = p.size();
for(int i = 1 ; i < sz ; i++)
tr[LCA(p[i - 1] , p[i])].push_back(p[i]);
for(int u : p)dsu[u] = u;
Dfs2(p[0]);
for(int z : vc[d])
{
// cerr << "\t" << z << ',' << lst[z] << " [" << vis[z] << ',' << d << ")\n";
qu[++tot] = {1 , lst[z] , z , vis[z] , 1};
qu[++tot] = {1 , lst[z] , z , d , -1};
vis[z] = d;
}
for(int u : p)tr[u].clear() , st[u].clear();
}
sort(qu + 1 , qu + tot + 1 , [&](const QU &a , const QU &b){return a.x == b.x ? a.tp > b.tp : a.x < b.x;});
// for(int i = 1 ; i <= tot ; i++)
// cerr << qu[i].tp << ":" << qu[i].l << ',' << qu[i].r << ' ' << qu[i].x << " ; " << qu[i].k << "\n";
// Calc(1 , tot);
for(int i = 1 ; i <= tot ; i++)
{
if(!qu[i].tp)for(int j = 1 ; j < i ; j++)
if(qu[j].tp && qu[j].l < qu[i].l && qu[i].l <= qu[j].r && qu[j].r <= qu[i].r)
ans[qu[i].k] += qu[j].k;
}
for(int i = 1 ; i <= m ; i++)
cout << ans[i] << "\n";
return 0;
}
详细
Subtask #1:
score: 0
Memory Limit Exceeded
Test #1:
score: 11
Accepted
time: 0ms
memory: 24300kb
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: 24304kb
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: 16ms
memory: 24200kb
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
Memory Limit Exceeded
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: 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%