Can anyone give a hand about the code?

Revision en1, by OtterZ, 2025-10-20 10:34:14

I was using the code to solve 258E - Little Elephant and Tree:

#include<cstdio>
#include<set>
#include<vector>
using namespace std;
int n,dfs[100009],dfscnt,ot[100009],ots[100009];
vector<int>e[100009];
void tree_srh(int nk,int l){
	dfs[nk]=++dfscnt;
	for(int i=0;i<e[nk].size();i++){
		if(e[nk][i]==l)continue; 
		tree_srh(e[nk][i],nk);
	}
	ot[nk]=ots[dfs[nk]]=dfscnt;
} 
vector<int>seg_tree[(1<<21)];
int m,a,b;
multiset<int>s;
int ans[100009];
void add(int k,int l,int r,const int lq,const int rq,const int n_val){
//printf("%d %d %d %d %d\n",k,l,r,lq,rq);
	if(l>rq||r<lq)return;
	if(l>=lq&&r<=rq){
		seg_tree[k].push_back(n_val); 
		return;
	}
	int m=(l+r)>>1;
	add((k<<1),l,m,lq,rq,n_val);
	add((k<<1)|1,m+1,r,lq,rq,n_val);
} 
int tamp(){
	int u=1,ats=0;
	while(u<=n){
		multiset<int>::iterator it=s.lower_bound(u);
		if(it==s.end())break;
		ats+=ots[*it]-*it+1;
		u=ots[*it]+1;
	}
	return ats;
}
void solve(int k,int l,int r){
	//printf("%d %d %d\n",k,l,r); 
	for(int i=0;i<seg_tree[k].size();i++)s.insert(seg_tree[k][i]);
	if(l==r){
		ans[l]=tamp();
		if(!s.empty())ans[l]--;
	} 
	else{
		int m=(l+r)>>1;
		solve((k<<1),l,m);
		solve((k<<1)|1,m+1,r);
	}
	for(int i=0;i<seg_tree[k].size();i++)s.erase(s.find(seg_tree[k][i]));
}
int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<n;i++){
		scanf("%d %d",&a,&b);
		e[a].push_back(b);
		e[b].push_back(a);
	}
	tree_srh(1,0);
	/*for(int i=1;i<=n;i++){
		printf("%d ",dfs[i]);
	}
	putchar('\n');*/
	for(int i=1;i<=m;i++){
		scanf("%d %d",&a,&b);
		add(1,1,n,dfs[a],ot[a],dfs[a]);
		add(1,1,n,dfs[a],ot[a],dfs[b]);
		add(1,1,n,dfs[b],ot[b],dfs[a]);
		add(1,1,n,dfs[b],ot[b],dfs[b]);
	}
	solve(1,1,n);
	for(int i=1;i<=n;i++){
		printf("%d ",ans[dfs[i]]);
	}
	return 0;
}

The code passed the task quickly but I couldn't prove it was at the right time complexy.Could anyone tell me how to prove the time complexy of the code that is enough to pass the task or hack it? waiting for your answer.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English OtterZ 2025-10-20 10:34:14 2059 Initial revision (published)