Why sigsegv on Milking Cows problem (USACO Training Pages)

Hello, I don't understand why USACO gives me sigsegv on my code, and I cannot reproduce it on my own computer.

#include <bits/stdc++.h>

using namespace std;

int main() {
	int n; cin>>n;
	vector<bool> cows(1000000, false);
	int maxn = 0;
	while(n--) {
		int a, b; cin>>a>>b;
		maxn=max(maxn, a); maxn=max(maxn, b);
		for(int i=a; i<=b; i++) cows[i]=true;
	}
	cows.resize(maxn+2);
	int yesmilk = 0, nomilk=0;
	int _y=0, _n=0; //temporary 
	bool passed=false;
	for(int i=1; i<=maxn; i++) {
		if(!passed) {
			if(cows[i]) passed=true;
		}
		if (cows[i]&&passed) {
			_y++; //increase longest time (temporary)
			nomilk=max(nomilk, _n); //merge nomilk
			_n=0; //reset
		} else if (!cows[i]&&passed){
			_n++;
			yesmilk=max(yesmilk, _y);
			_y=0;
		}
	}
	nomilk=max(nomilk, _n); 
	yesmilk=max(yesmilk, _y);
	if(nomilk==0) cout<<yesmilk-1<<" "<<0<<endl;
	else cout<<yesmilk-1<<" "<<nomilk+1<<endl;
}

Posted Feb 17, 2017, 2:50 AM UTC

10
Loading comments…