I seem to have run in a weird problem. I have a multiset consisting of long longs, but when I try to insert number 5*1e8, it crashes. For more info, when I try to insert the number the multiset is empty ( but I don't see how that could affect anything ). the code is down below.
I added some string outputs, to pinpoint where the program crashes. This is a solution to problem 1418D on codeforces ( https://codeforces.com/contest/1418/problem/D ) and my program crashes on the second testcase. the program stops when it hits insert which comes after lfdsa string and iterators, multiset size is outputed.
Any help is appreciated and sorry for my bad english.
The test case is :
5 8
5 1 2 4 3
0 1
0 2
0 3
0 4
0 5
1 1000000000
1 1
1 500000000
Code
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int main()
{
multiset<ll> dif,arr;
ll n,q;
cin>>n>>q;
ll x[n+1];
for(ll k=1;k<=n;k++)
{
cin>>x[k];
arr.insert(x[k]);
}
sort(x+1,x+1+n);
for(ll k=2;k<=n;k++)
{
dif.insert(x[k]-x[k-1]);
}
multiset<ll>::iterator st,nd,LargestDif,it;
st=arr.begin();
nd=arr.end();
LargestDif=dif.end();
if(n!=1) LargestDif--;
nd--;
if(n>2) cout<<*nd-*st-*LargestDif<<endl;
else cout<<"0"<<endl;
for(ll k=1;k<=q;k++)
{
ll type,cord;
cin>>type>>cord;
if(arr.size()<3 && type==0)
{
cout<<0<<endl;
arr.erase(arr.find(cord));
continue;
}
if(arr.size()<2 && type==1)
{
cout<<0<<endl;
arr.insert(cord);
continue;
}
cout<<"jfads"<<endl;
if(type==1)
{
arr.insert(cord);
it=arr.find(cord);
multiset<ll>::iterator it1,it2;
it1=it;
it2=it;
it2++;
cout<<"ghfd"<<endl;
if(it==arr.begin())
{
dif.insert(*it2-*it);
}
else
{
cout<<"lfds"<<endl;
it1--;
if(it2==arr.end())
{
cout<<"klfds"<<endl;
dif.insert(*it-*it1);
}
else
{
cout<<"lfdsa"<<endl;
dif.erase(dif.find(*it2-*it1));
cout<<*it<<" "<<*it2<<" "<<*it2-*it<<" "<<dif.size()<<endl;
ll o=*it2-*it;
dif.insert(o);
cout<<"qwere"<<endl;
dif.insert(*it-*it1);
}
cout<<";fdasd"<<endl;
}
}
else
{
it=arr.find(cord);
multiset<ll>::iterator it1,it2;
it1=it;
it2=it;
it2++;
if(it==arr.begin())
{
dif.erase(dif.find(*it2-*it));
}
else
{
it1--;
if(it2==arr.end())
{
dif.erase(dif.find(*it-*it1));
}
else
{
dif.erase(dif.find(*it2-*it));
dif.erase(dif.find(*it-*it1));
dif.insert(*it2-*it1);
}
}
cout<<"sanlfas"<<endl;
arr.erase(arr.find(cord));
}
cout<<"dsafdsf"<<endl;
st=arr.begin();
nd=arr.end();
cout<<"bcd"<<endl;
nd--;
cout<<"ghsfd "<<dif.size()<<endl;
LargestDif=dif.end();
LargestDif--;
cout<<"oj;dfas"<<endl;
cout<<*nd-*st-*LargestDif<<endl;
}
}