Is this how we have to build segment tree using vectors

vectormerge(vectortmp1,vectortmp2){
int k=min(tmp1.size(),tmp2.size()),i=0,j=0;
vectorreTree;
while(i<k){
if(tmp1.size()<=tmp2.size()){
if(tmp1[i]<=tmp2[j]){
reTree.push_back(tmp1[i]);
i++;
}
else {
reTree.push_back(tmp2[j]);
j++;
}
}
else{
if(tmp2[i]<=tmp1[j]){
reTree.push_back(tmp2[i]);
i++;
}
else {
reTree.push_back(tmp1[j]);
j++;
}
}
}
if(tmp1.size()<=tmp2.size()){
while(j<tmp2.size())reTree.push_back(tmp2[j]);
}
else while(j<tmp1.size())reTree.push_back(tmp1[j]);

return reTree;

}
void buildtree(int tmp,int s,int e,vector<vector>tree,int idx ){
if(s==e){
tree[idx][0]=tmp[s];
return;
}
int mid=(s+e)/2;
buildtree(tmp,s,mid,tree,2
idx);
buildtree(tmp,mid+1,e,tree,2idx+1);
vector temp=merge(tree[2
idx],tree[2*idx+1]);
tree[idx]=temp;
return;

}

@Kiraa_545 please provide me your code in Coding Blocks IDE.

@Kiraa_545 here is sol to a problem GSS3 on SPOJ which uses segment tree and seg tree is created using vector.


You can try to get an idea from it.
If this resolves your doubt mark it as resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like