I have written this merge sort program in c++ but I am getting "Segmentation fault (core dumped)" error after running the code. Even though there is no compilation error. Can you please tell me what's the mistake I am doing? While taking input in the array it is showing that error. If I change it to push_back, the input is fine but later in merge function, it is showing the same error.
//merging 2 sorted subarrays.
#include <iostream>
#include <vector>
using namespace std;
void merge(vector <int> &a,vector <int> &b,vector <int> &c)
{
    int i=0,j=0,k=0,bL=b.size(),cL=c.size();
    while(i<bL && j<cL)
    {
        if(b[i]<c[j])
        {
            a[k]=b[i];
            i++;k++;
        }
        else
        {
            a[k]=c[j];
            j++;k++;
        }
    }
    while(i<bL)
    {
        a[k]=b[i];
        i++;k++;
    }
    while(j<cL)
    {
        a[k]=c[j];
        j++;k++;
    }
    cout<<"array a inside merge is: "<<endl;
    for(int p=0;p<a.size();p++)
    {
        cout<<a[p]<<endl;
    }
}
void mergeSort(vector <int> &a)
{
    vector <int> l, r;
    int mid;
    if(a.size()<2) return;
    mid = a.size()/2;
    for(int i=0;i<mid;i++)
    {
        l[i]=a[i];
    }
    for(int i=mid;i<a.size();i++)
    {
        r[i-mid]=a[i];
    }
    mergeSort(l);
    mergeSort(r);
    merge(a, l, r);
}
int main()
{
    int n;
    vector <int> a;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    mergeSort(a);
    for(int p=0;p<n;p++)
    {
        cout<<a[p]<<endl;
    }
    return 0;
}
 
     
     
    