For the sigsegv problem, I'd guess that it's the result of i
index running past the end of the s[]
array under certain conditions in the inner loop here:
while(i<n){
p=s[i];
cnt=0;
while(s[i]==p){
cnt++;
i++; // <== I'm not convinced this will always remain less than n
// or less than 1000000 depending on the data set and
// what happens to be in memory after `s[]`
}
ans=ans+((unsigned long long)cnt*(cnt-1))>>1;
}