ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#213860 | #582. t1 | STASISZHY | 100 | 690ms | 26376kb | C++11 | 2.4kb | 2024-11-13 22:18:56 | 2024-11-13 23:10:41 |
answer
// Problem: A. t1
// Contest: undefined - NOIP2024训练赛 04
// URL: http://www.noi.ac/contest/1156/problem/582
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
//#define int long long
#define fi first
#define se second
#define PII pair<int, int>
using namespace std;
const int N = 2e5 + 10, M = 1e6 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m, q, ti, cnt;
int s[N], dp[N], sum[N], dfl[N], dfr[N], id[N], fa[N];
vector<int> e[N], ans;
void dfs(int now, int f)
{
dfl[now] = ++ ti, id[ti] = now;
for(auto i : e[now])
{
if(i == f) continue;
dfs(i, now);
}
dfr[now] = ti;
}
struct Tree
{
int l, r;
PII mn;
}tr[N << 2];
inline PII MN(PII a, PII b)
{
if(a.fi < b.fi) return a;
else return b;
}
inline void pushup(int x) {tr[x].mn = MN(tr[2 * x].mn, tr[2 * x + 1].mn);}
void build(int x, int l, int r)
{
tr[x].l = l, tr[x].r = r;
if(l == r)
{
tr[x].mn = {s[id[l]], id[l]};
return;
}
int mid = l + r >> 1;
build(2 * x, l, mid);
build(2 * x + 1, mid + 1, r);
pushup(x);
}
PII query(int x, int l, int r)
{
if(tr[x].l >= l && tr[x].r <= r) return tr[x].mn;
int mid = tr[x].l + tr[x].r >> 1; PII res = {INF + 5, 0};
if(l <= mid) res = MN(res, query(2 * x, l, r));
if(r > mid) res = MN(res, query(2 * x + 1, l, r));
return res;
}
void change(int x, int p)
{
if(tr[x].l == tr[x].r)
{
tr[x].mn.fi = INF;
return;
}
int mid = tr[x].l + tr[x].r >> 1;
if(p <= mid) change(2 * x, p);
else change(2 * x + 1, p);
pushup(x);
}
void solve(int now)
{
// if(++ cnt >= 30) exit(0);
// cout << "now = " << now << '\n';
int l = dfl[now] + 1, r = dfr[now];
if(l > r)
{
ans.push_back(now); change(1, dfl[now]);
return;
}
while(1)
{
PII x = query(1, l, r);
// cout << "sum = " << x.fi << " to = " << x.se << '\n';
if(x.fi >= INF)
{
ans.push_back(now); change(1, dfl[now]);
break;
}
else solve(x.se);
}
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 2; i <= n; i ++)
{
int x; cin >> x; fa[i] = x;
e[i].push_back(x), e[x].push_back(i);
}
for(int i = 1; i <= n; i ++) cin >> s[i];
dfs(1, 0); build(1, 1, n);
// cout << dfl[3] << ' ' << dfr[3] << '\n';
// cout << fa[3] << '\n';
solve(1);
for(auto i : ans) cout << i << ' ';
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 18484kb
input:
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...
output:
52 75 61 96 94 85 99 65 92 64 100 53 79 67 84 89 69 62 88 58 87 68 51 76 56 70 74 55 91 72 59 82 71 ...
result:
ok 100 numbers
Test #2:
score: 10
Accepted
time: 12ms
memory: 18484kb
input:
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...
output:
97 98 56 64 88 71 85 54 94 81 69 74 77 82 66 76 100 95 78 57 93 86 80 83 63 90 68 99 53 52 73 91 96 ...
result:
ok 100 numbers
Test #3:
score: 10
Accepted
time: 10ms
memory: 18484kb
input:
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...
output:
68 100 83 82 66 57 73 90 74 89 92 84 78 52 99 65 75 54 71 87 85 77 61 93 72 63 64 58 60 69 91 96 67 ...
result:
ok 100 numbers
Test #4:
score: 10
Accepted
time: 12ms
memory: 18556kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...
output:
720 630 795 982 858 778 764 843 958 636 655 718 999 640 819 553 685 815 801 611 580 660 848 722 508 ...
result:
ok 1000 numbers
Test #5:
score: 10
Accepted
time: 0ms
memory: 18560kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...
output:
993 967 945 661 760 801 836 832 798 725 609 708 916 659 785 955 912 802 778 739 850 529 680 664 847 ...
result:
ok 1000 numbers
Test #6:
score: 10
Accepted
time: 0ms
memory: 18560kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...
output:
865 829 713 826 804 598 835 729 919 613 859 604 586 677 926 808 871 883 512 573 946 688 540 980 501 ...
result:
ok 1000 numbers
Test #7:
score: 10
Accepted
time: 164ms
memory: 26376kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
output:
60966 84526 92926 81979 52715 89039 62563 60694 55151 70404 50693 82115 87107 88733 92525 69003 5623...
result:
ok 100000 numbers
Test #8:
score: 10
Accepted
time: 163ms
memory: 26372kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
output:
73271 97790 79288 53984 60747 85301 62325 94837 68122 92850 54755 72761 86308 93120 85924 58122 5785...
result:
ok 100000 numbers
Test #9:
score: 10
Accepted
time: 163ms
memory: 26372kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
output:
96945 71333 97384 73952 87860 78638 54641 53438 84679 65487 87180 73489 72817 69821 95923 70797 7798...
result:
ok 100000 numbers
Test #10:
score: 10
Accepted
time: 166ms
memory: 26376kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
output:
86618 75549 54929 84432 89351 73329 74867 54756 55393 96686 62390 82968 79815 69008 65957 58416 6517...
result:
ok 100000 numbers
Extra Test:
score: 0
Extra Test Passed