/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Accepted 5ms 5.02 MiB
#3 Accepted 89ms 5.27 MiB
#4 Accepted 57ms 5.172 MiB
#5 Accepted 81ms 5.215 MiB
#6 Accepted 160ms 5.566 MiB
#7 Accepted 228ms 5.527 MiB
#8 Accepted 578ms 6.988 MiB
#9 Accepted 466ms 6.957 MiB
#10 Accepted 417ms 7.547 MiB
#11 Time Exceeded ≥2101ms ≥11.324 MiB
#12 Time Exceeded ≥2099ms ≥20.336 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=2e5+10,MOD=998244353;
typedef long long ll;
#define debug(x) cout<<x<<endl
vector<int>edge[M];
int in[M];
int out[M];
int cnt=0;
int A[M];
int freq[M];      // freq[x] = how many times value x appears in the current subarray
bool used[M];     // used[x] indicates whether x <= currentK (so we count it if freq[x] > 0)
int distinctCount;
int curL = 0, curR = -1, curK = 0;   
vector<int>convert_A; 
void dfs(int x,int p){
    in[x]=out[x]=cnt++;
    convert_A.push_back(x);
    for(auto u:edge[x]){
    	if(u!=p){
    		dfs(u,x);
    	}
    }
    out[x]=cnt;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
    	int n;
    	cin>>n;
    	for(int i=0;i<n;i++)cin>>A[i];
    	for(int i=1;i<n;i++){
    		int x,y;
    		cin>>x>>y;
    		edge[x].push_back(y);
    		edge[y].push_back(x);
    	}
    	cnt=0;
    	dfs(1,-1);
    	int q;
    	cin>>q;
    	vector<int>ans(n+1,-1);
    	while(q--){
    		int x;
    		cin>>x;
    		int l=in[x];
    		int r=out[x]-1;
    		unordered_map<int,int>mp;
    		int k=r-l+1;
    		int c=0;
    		if(ans[x]!=-1){
    			cout<<ans[x]<<"\n";
    			continue;
    		}
    		for(int i=l;i<=r;i++){
    			int cur=A[convert_A[i]-1];
    			if(cur<=k){
    				mp[cur]++;
    				if(mp[cur]==1)c++;
    			}
    		}
    		ans[x]=k-c;
    		cout<<k-c<<"\n";
    	}
    	for(int i=0;i<=n+1;i++){
    		edge[i].clear();
    		in[i]=0;
    		out[i]=0;
    	}
    	convert_A.clear();
    	cnt=0;
    	
    	
    	
    	
    	
    	
    	}
    	
    	
    	
    	
      
    return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-31 19:14:44
Judged At
2024-12-31 19:14:44
Judged By
Score
20
Total Time
≥2101ms
Peak Memory
≥20.336 MiB