/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 4.777 MiB
#2 Accepted 3ms 4.688 MiB
#3 Accepted 54ms 6.613 MiB
#4 Accepted 52ms 4.832 MiB
#5 Accepted 65ms 4.703 MiB
#6 Accepted 83ms 4.77 MiB
#7 Accepted 88ms 5.383 MiB
#8 Accepted 139ms 7.785 MiB
#9 Accepted 127ms 6.156 MiB
#10 Accepted 134ms 6.473 MiB
#11 Accepted 64ms 10.043 MiB
#12 Accepted 219ms 18.285 MiB
#13 Accepted 217ms 18.387 MiB
#14 Accepted 218ms 18.359 MiB
#15 Accepted 218ms 17.992 MiB
#16 Accepted 220ms 18.266 MiB
#17 Accepted 84ms 18.191 MiB
#18 Accepted 84ms 18.562 MiB
#19 Accepted 863ms 14.426 MiB
#20 Accepted 833ms 14.254 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll, ll>                   PII;
const int mx = 1e5 + 5;
int K , Ans[mx], arr[mx],freq[mx] , F[mx] ;
int cnt=0;
vector<int>edge[mx];
int out[mx],in[mx];
vector<int>convert_A; 
int bit[mx];
int n,q;
int qquery(int id){
	int sum=0;
	while(id>0){
		sum+=bit[id];
		id-=id&(-id);
	}
	return sum;
	
}
void update(int id,int val){
	while(id<=n){
		bit[id]+=val;
		id+=id&(-id);
	}
}
struct Query
{
    int index, l, r ;
    bool operator < (const Query &other)const
    {
        int block_own = l/ K ;
        int block_other = other.l / K ;
        return(block_other == block_own) ? r < other.r : block_own < block_other ;
    }

} query[mx];
void add(int index)
{
    freq[arr[convert_A[index]-1]]++ ;
    if(freq[arr[convert_A[index]-1]]==1)update(arr[convert_A[index]-1],1);
    
}
void remove (int index)
{
   freq[arr[convert_A[index]-1]]-- ;
   if(freq[arr[convert_A[index]-1]]==0)update(arr[convert_A[index]-1],-1);
}
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;
}
void solve()
{
      cin >> n ;
    for(int i = 0 ; i <= n ; i++) freq[i] = 0 ;
    for(int i = 0 ; i < n ; i++) cin >> arr[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);
    K = sqrt(n) ;
    cin >> q ;
    for(int i = 0 ; i < q ; i++){
    	int x;
    	 cin >> x;
    	 query[i].l=in[x], query[i].r=out[x]-1 , query[i].index = i;
    	 }
    sort(query , query + q) ;
    int l = 0, r = -1 ;
    for(int i = 0 ; i < q ; i++)
    {
        while(r < query[i].r) add(++r ) ;
        while(l < query[i].l) remove(l++) ;
        while(r > query[i].r) remove(r-- );
        while(l > query[i].l) add(--l) ;
       // int cnt = 0 ;
        int dis = query[i].r - query[i].l + 1 ;
        
        Ans[query[i].index] = dis-qquery(dis);
    }
    convert_A.clear();
    for(int i=1;i<=n;i++)edge[i].clear(),bit[i]=0;
    for(int i = 0 ; i < q ; i++) cout<< Ans[i] << "\n" ;
}
int main()
{
     __Heart__
    // READ("input7.txt") ;
   // WRITE("output7.txt") ;
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-01 10:16:09
Judged At
2025-01-01 12:41:26
Judged By
Score
100
Total Time
863ms
Peak Memory
18.562 MiB