0

The following code is running perfectly well with my g++ compiler. But on submitting it on an online judge, gives SIGABRT error. I read that stl elements can generate this error if they try to access too much memory. I cannot see any such use of memory. Is it the count? I have implemented my own count, but it still gives the same error.

#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define M 100
void range_change(bitset<M>& x, int lower, int upper, bool change){
    if (change){
     for (unsigned i = lower; i <= upper; ++i)
              x.set(i);
     }
    else{
    for (unsigned i = lower; i <= upper; ++i)
              x.reset(i);
     }
    }
int count_Set(bitset<M> & x){
    int count = 0;
    for(int i = 0; i< 100; i++){
        if(1 & x[i]) count++;
    }
    return count;
}

bitset<100> houses;

int main(){
    fastio;
    int t;
    cin>> t;
    int m, x, y;
    int reach{0};
    int lower{0}, upper{0};
    // int t = 1;
    while(t--){

        houses.set();
        // cout<< houses;
        cin >> m >> x >> y;
        reach = x*y;
        vector<int> cat(m, 0);
        for(auto& u: cat){
            cin>> u;

            lower = u - reach -1;
            if(lower < 0) lower = 0;
            upper = u + reach-1;
            if(upper > 100) upper = 99;
            range_change(houses, lower, upper, false);
        }
        // cout<< houses << "\n";
         cout<< houses.count() << "\n";
       // cout<< count_Set(houses) << "\n";

 }
    return 0;
}
sarp
  • 9
  • 6
  • Please read [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl May 02 '20 at 16:06
  • "`#pragma GCC optimize ("-O2")`" - don't put compiler options in source files. Use a proper build system. – Jesper Juhl May 02 '20 at 16:08
  • @JesperJuhl I understand the downsides of using but this code is only meant for competition. I admit that it's a bad habit. thanks! – sarp May 02 '20 at 17:05

1 Answers1

2

Just skimming the source code, this is what I see:

#define M 100
...
bitset<100> houses;

Seems like you want to use M instead of 100 for safety.

Then here:

if(upper > 100) upper = 99;

This is going to break at upper = 100 Suggest:

if(upper >= M) upper = M - 1;
273K
  • 29,503
  • 10
  • 41
  • 64
rand'Chris
  • 788
  • 4
  • 17
  • @rand'Chris thanks, that worked, I will be careful next time. what's wrong with bitset<100>, why using M is more safe? – sarp May 02 '20 at 16:59
  • You're using `M` to specify the size of something. Anything that manipulates this data will need to know its size. So you can either use `M` or `100` today. However, if you need 102 bits in the future, you have to update both `M` and all the `100`s. If you just use `M`, and your logic is "M size" and "M size -1", and "for i = 1 to M size" you only need to change M in one place. So it applies to all comparisons and loops where you have `100` where what you really mean is "size M". – rand'Chris May 02 '20 at 17:10
  • Also, if this solved the problem, please accept it as a solution. Thanks! – rand'Chris May 02 '20 at 17:17