[simon@simon-laptop][16:29:32]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling ashutosh_6500-LOSTMAX.cpp
+ g++ -std=c++14 ashutosh_6500-LOSTMAX.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
ashutosh_6500-LOSTMAX.cpp: In function ‘int main()’:
ashutosh_6500-LOSTMAX.cpp:17:23: warning: conversion to ‘std::__debug::multiset<int>::value_type {aka int}’ from ‘long long int’ may alter its value [-Wconversion]
s.insert(x);
^
ashutosh_6500-LOSTMAX.cpp:19:40: warning: conversion to ‘std::__debug::multiset<int>::key_type {aka int}’ from ‘std::__cxx1998::multiset<int, std::less<int>, std::allocator<int> >::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
s.erase(s.lower_bound(s.size() -1));
~~~~~~~~~^~
ashutosh_6500-LOSTMAX.cpp:10:21: warning: unused variable ‘n’ [-Wunused-variable]
long long int t,n,i,ma,x;
^
ashutosh_6500-LOSTMAX.cpp:10:23: warning: unused variable ‘i’ [-Wunused-variable]
long long int t,n,i,ma,x;
^
ashutosh_6500-LOSTMAX.cpp:10:25: warning: unused variable ‘ma’ [-Wunused-variable]
long long int t,n,i,ma,x;
^~
+ set +x
Successful
[simon@simon-laptop][16:29:36]
[~/devel/hackerrank/otherpeoples]>echo "3
1 2 1
3 1 2 8
1 5 1 4 3 2" | ./a.out
/usr/include/c++/7/debug/multiset.h:328:
Error: attempt to erase from container with a past-the-end iterator.
Objects involved in the operation:
sequence "this" @ 0x0x7ffe3dfb4f60 {
type = std::__debug::multiset<int, std::less<int>, std::allocator<int> >;
}
iterator "__position" @ 0x0x7ffe3dfb4f00 {
type = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<int>, std::__debug::multiset<int, std::less<int>, std::allocator<int> > > (mutable iterator);
state = past-the-end;
references sequence with type 'std::__debug::multiset<int, std::less<int>, std::allocator<int> >' @ 0x0x7ffe3dfb4f60
}
Aborted (core dumped)
Here’s your solution, just copy pasted from your submission.
I guess I found the error.
You are trying to access invalid memory location. (Mentioned ‘here’ as a comment).
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,n,k,c=0;
cin >> n >> k;
string s[k+1]; int a[k+1]={0};
fflush(stdin);
for(i=1;i<=k;i++)
getline(cin,s[i]);
for(i=1;i<=k;i++){
if(s[i]== "CLOSEALL"){
a[k+1]={0}; //here
cout << 0 << endl;
}
else {
if(a[stoi(s[i].substr(6,7))]==0){
a[stoi(s[i].substr(6,7))]=1;
++c;
}
else{
a[stoi(s[i].substr(6,7))]=0;
--c;
}
if(c<0) c=0;
cout << c << endl;
}
}
return 0;
}
I tried to run it by commenting that part and I still get the error.
In that part I am just Initalizing. Here is the more elaborate error
“terminate called after throwing an instance of ‘std::out_of_range’
what(): basic_string::substr: __pos (which is 6) > this->size() (which is 0)”.