Need help on ACTIV - Activities. Getting SIGSEGV

https://www.spoj.com/problems/ACTIV/

My code produces SIGSEGV runtime error. Can someone help me find the error?
@ssjgz @galencolin @everule1

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define all(x) begin(x), end(x)

const int MOD = 1e8;

int bsearch(vector<pii> &t, int st, int end, int x) {
	int ind = -1;
	while(st <= end) {
		int mid = (st + end) / 2;
		if(t[mid].ss <= x) {
			ind = mid;
			st = mid + 1;
		}
		else {
			end = mid - 1;
		}
	}
	return ind;
}

int main() {
	int n;
	while(cin >> n) {
		if(n == -1) {
			break;
		}
		vector<pii> t;
		for(int i = 0; i < n; ++i) {
			int x, y;
			cin >> x >> y;
			t.pb({x, y});
		}
		
		sort(all(t), [](pii x, pii y) {
			if( x.ss < y.ss) {
				return true;
			}
			return (x.ss == y.ss && x.ff <= y.ff);
		});
		
		vector<ll> dp(n, 1);
		dp[1] = 1;
		for(int i = 1; i < n; ++i) {
			int ind = bsearch(t, 0, i -1, t[i].ff);
			if(ind != -1) {
				dp[i] = (dp[i] * dp[ind] + dp[i]) % MOD;
			}
			dp[i] += dp[i - 1];
			dp[i] %= MOD;
		}
		string fin = to_string(dp[n - 1]);
		fin = string(8 - fin.size(), '0') + fin;
		cout << fin << "\n";
	}
	return 0;
}

ind initial value could be any thing right?

const int MOD = 1e8;

are you sure ?

Yeah, it is according to the question

‘ind’ just has the index of the interval obtained from the binary search

Is something wrong on my bsearch function?

The first thing that leaps out is that the comparator you pass to sort is wrong:

[simon@simon-laptop][11:30:34]
[~/devel/hackerrank/otherpeoples]>echo "5
1 3
3 5
5 7
2 4
4 6
3
500000000 1000000000
1 500000000
1 500000000
1
999999999 1000000000
-1" | ./prad8109-ACTIV
/usr/include/c++/7/bits/stl_algo.h:4866:
Error: comparison doesn't meet irreflexive requirements, assert(!(a < a)).

Objects involved in the operation:
    instance "functor" @ 0x0x7ffe5d5d1729 {
      type = main::{lambda(std::pair<int, int>, std::pair<int, int>)#1};
    }
    iterator::value_type "ordered type" {
      type = std::pair<int, int>;
    }
Aborted (core dumped)
1 Like

But it produced correct output for the sample test case in ideone.
Can you please say why the comparator is wrong?

There’s Undefined Behaviour for you :man_shrugging: :

Exactly as it says in the error message - your comparator is (at least) not irreflexive.

1 Like

Tysm for the help!

1 Like

Found the reason for the error.
https://codeforces.com/blog/entry/60972