Question Link: https://www.hackerrank.com/contests/hack-the-interview-vi-asia-pacific/challenges/the-cup-game
This was my code… it passed 10/14 test cases only and gave segmentation fault in the rest. Why is that?
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll i,n,m,s,q,temp;
cin >> n >> m >> s >> q;
ll ar[n]={0};
for(i=1; i<=m; i++)
{
ll k;
cin >> k;
k--;
ar[k]=1;
}
while(s--)
{
ll s1,s2;
cin >> s1 >> s2;
s1--;
s2--;
temp=ar[s1];
ar[s1]=ar[s2];
ar[s2]=temp;
}
ll ar2[n]={0};
ll sum=0;
for(i=0; i<n; i++)
{
sum += ar[i];
ar2[i]=sum;
}
while(q--)
{
ll q1,q2;
cin >> q1 >> q2;
q1--;
q2--;
if(q1==0)
cout << ar2[q2] << " ";
else
cout << (ar2[q2]-ar2[q1-1]) << " ";
}
return 0;
}