UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213858#582. t1erican100463ms26756kbC++112.7kb2024-11-13 22:05:372024-11-13 23:10:30

answer

// Problem: A. t1
// Contest: undefined - NOIP2024训练赛 04
// URL: http://noi.ac/contest/1156/problem/582
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Challenger: Erica N
// ----
// 
#include<bits/stdc++.h>

using namespace std;
#define rd read()
#define ull unsigned long long
#define int long long 
#define pb push_back
#define itn int
#define ps second 
#define pf first


#define rd read()
int read(){
  int xx = 0, ff = 1;char ch = getchar();
  while (ch < '0' || ch > '9'){
    if (ch == '-')ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')xx = xx * 10 + (ch - '0'), ch = getchar();
  return xx * ff;
}
#define zerol = 1
#ifdef zerol
#define cdbg(x...) do { cerr << #x << " -> "; err(x); } while (0)
void err() {cerr << endl;}
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
	for (auto v: a) cerr << v << ' ';err(x...);
}
template<typename T, typename... A>
void err(T a, A... x) {
	cerr << a << ' ';err(x...);
}
#else
#define dbg(...)
#endif
const int N=2e5+5;
const ull P=137;
const int INF=1e18+7;
/*

策略


*/	



#define pii pair<int,int>
int faa[N];
vector<int> e[N];
int n;
priority_queue<pii> pq;
int dfn[N],_dfn[N],rdfn[N];
int w[N];
int tim;

void add(int a,int b){
	e[a].pb(b);
}

namespace SGT{
	
	pair<int,int> t[N<<2];
	
	void pushup(int x){
		t[x]=t[x<<1].pf<t[x<<1|1].pf?t[x<<1]:t[x<<1|1];
	}
	void build(itn x,int l,int r){
		if(l==r){
			t[x]={w[_dfn[l]],_dfn[l]};
			return ;
		}
		int mid=l+r>>1;
		build(x<<1,l,mid);
		build(x<<1|1,mid+1,r);
		pushup(x);
	}
	
	pii query(int x,int l,int r,int pl,int pr){
		if(pr<pl)return make_pair(INF,0);
		if(pl<=l&&pr>=r){
			return t[x];
		}
		int mid=l+r>>1;
		pii res={INF,0};
		pii t;
		if(pl<=mid)t=query(x<<1,l,mid,pl,pr),res=t.pf<res.pf?t:res;
		if(pr>mid)t=query(x<<1|1,mid+1,r,pl,pr),res=t.pf<res.pf?t:res;
		return res;
	}
	
	void change(int x,int l,int r,int p){
		if(l==r){
			t[x]={INF,0};
			return ;
		}
		int mid=l+r>>1;
		if(p<=mid)change(x<<1,l,mid,p);
		else change(x<<1|1,mid+1,r,p);
		pushup(x);
	}
}using namespace SGT;



void dfs(int x,int fa){
	dfn[x]=++tim;
	_dfn[tim]=x;
	for(auto v:e[x]){
		if(v==fa)continue;
		dfs(v,x);
	}
	
	rdfn[x]=tim;
}


int cnt;
void dfs2(int x){
	while(1){
		int v=query(1,1,n,dfn[x]+1,rdfn[x]).ps;
		cnt++;
		if(v==0){
			cout<<x<<' ';
			change(1,1,n,dfn[x]);
			return ;
		}
		dfs2(v);
	}
	dfs2(faa[x]);
}


signed main(){
	 n=rd;
	for(int i=2;i<=n;i++){
		faa[i]=rd;
		add(faa[i],i);
	}
	
	
	for(int i=1;i<=n;i++){
		w[i]=rd;
	}
	
	dfs(1,0);
	
	
	build(1,1,n);
	
	dfs2(1);
	
	// cdbg(cnt);
	
	
}


详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 4ms
memory: 18424kb

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: 4ms
memory: 18424kb

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: 4ms
memory: 18420kb

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: 3ms
memory: 18500kb

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: 7ms
memory: 18500kb

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: 5ms
memory: 18500kb

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: 76ms
memory: 26748kb

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: 82ms
memory: 26756kb

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: 129ms
memory: 26752kb

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: 149ms
memory: 26748kb

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