I am getting SIGSEGV Error. Seems like incrementing the iterator is causing the problem. Here is my code

#include<bits/stdc++.h>
#define ll long long
#define oset tree < int , null_type , less , rb_tree_tag , tree_order_statistics_node_update >
using namespace std;
vector v;
int ub(int a,int low,int high)
{
int x=INT_MAX;
while(low<=high)
{
int mid=(low+high)/2;
if(v[mid]>a)
{
x=min(x,mid);
high=mid-1;
}
else
{
low=mid+1;
}
}
return x;
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;ll a,prev=INT_MIN,count=0;
cin>>N;
for(int i=0;i<N;i++)
{
cin>>a;
if(a>prev)
{
v.push_back(a);
prev=a;
}
else
{
int index=ub(a,0,v.size()-1);
count+=(i-index);
auto itr=v.begin();
for(int k=0;k<index;k++)itr++;
v.insert(itr, a);
}
}
cout<<count;
return 0;
}

but i am getting runtime error from line 5,
vector v;
also care about your code’s formatting (click here for reference)

2 Likes