General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
100874254 Practice:
mradul098
375D - 27 C++14 (GCC 6-32) Wrong answer on test 13 826 ms 8732 KB 2020-12-11 07:39:39 2020-12-11 07:39:39
→ Source
#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=1;i<=n;i++)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second

#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))

#define PI 3.1415926535897932384626
#define maxn 100001
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;

const int mod = 1000000007;
const int bs=700;
int ft[2*maxn],s[maxn],t[maxn],col[maxn]; //note a globally declared aray has its default value set to 0
vi edges[maxn];
int timer=1;
int node_frequency[maxn],color_frequency[maxn],frequency_of_frequency[maxn],bucket[maxn];
int ans[maxn];

struct query
{
    int left;
    int right;
    int k;
    int idx;
};

int getBlock(int idx)
{
	return (idx + bs - 1) / bs;
}
 
 

bool comp(query a , query b)
{
	int x = a.left/bs;
	int y = b.left/bs;
	
	if(x != y)
	return x < y;
	
	if(x % 2)
	return a.right < b.right;
	else
	return a.right > b.right;
}

query Q[maxn];

void dfs(int node , int par)
{
	s[node] = timer;
	ft[timer] = node;
	timer++;
	
	for(int child : edges[node])
	if(child != par)
	dfs(child , node);
	
	t[node] = timer;
	ft[timer] = node;
	timer++;
}

void add(int idx)
{
	int node = ft[idx];
	int color_of_node = col[node];
	node_frequency[node]++;
	
	if(node_frequency[node] == 2)
	{
		color_frequency[color_of_node]++;
		frequency_of_frequency[color_frequency[color_of_node]]++;
		bucket[getBlock(color_frequency[color_of_node])]++;
		frequency_of_frequency[color_frequency[color_of_node]-1]--;
		bucket[getBlock(color_frequency[color_of_node]-1)]--;
	}
	
} 

void remove(int idx)
{
	int node = ft[idx];
	int color_of_node = col[node];
	node_frequency[node]--;
	
	if(node_frequency[node] == 1)
	{
		color_frequency[color_of_node]--;
		frequency_of_frequency[color_frequency[color_of_node]]++;
		bucket[getBlock(color_frequency[color_of_node])]++;
		frequency_of_frequency[color_frequency[color_of_node]+1]--;
		bucket[getBlock(color_frequency[color_of_node]-1)]--;
	}
	
}
 
int get_ans(int k,int n)
{
    int res = 0;
	int LB = getBlock(k);
	int RB = getBlock(n);
	
	if(LB == RB)
	{
		for(int i=k;i<=n;i++)
		res += frequency_of_frequency[i];
	}
	else
	{
		for(int i=k;i<=LB*bs;i++)
		res += frequency_of_frequency[i];
		
		for(int i=LB+1;i<RB;i++)
		res += bucket[i];
		
		for(int i=(RB-1)*bs+1;i<=n;i++)
		res += frequency_of_frequency[i];
	}
	
	return res;
}

int main()
{
    int n,q;
    cin>>n>>q;
    
    rep(i,n)
        cin>>col[i];
    
    rep(i,n-1)
        {
            int tx,ty;
            cin>>tx>>ty;
            edges[tx].pb(ty);
            edges[ty].pb(tx);
        }
    dfs(1,-1); //using dfs creating ft , s , t which is basically sying creation of flatten tree
    rep(i,q)
    {
        int num,k;
        cin>>num>>k;
        Q[i].left=s[num]; //starting point of the node a in ft array
        Q[i].right=t[num]; //ending point of the node a in ft array
        Q[i].k=k;
        Q[i].idx=i;
    }
    
    sort(Q+1, Q+1+q, comp);  //Q+1 kyunki indexing apan 1 se shuru kar rahe hai
    int ML=1 , MR=0;
    
    rep(i,q)
    {
        int left_of_ft = Q[i].left;
        int right_of_ft = Q[i].right;
        int k=Q[i].k;
        int idx = Q[i].idx;
        
        
		while(MR < right_of_ft)
		MR++ , add(MR);
		
		while(ML > left_of_ft)
		ML-- , add(ML);
		
		while(MR > right_of_ft)
		remove(MR) , MR--;
		
		while(ML < left_of_ft)
		remove(ML) , ML++;
    
        ans[idx]=get_ans(k,n);
    }
    rep(i,q)
        cout<<ans[i]<<endl;
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details