Google codejam question 3 , cant understand where my soln went wrong

This is my solution for 3rd question in google codejam

I cant figure out the mistake and it seems to me the solution is right but i am not getting the answer.

#include<iostream>

#include
#include<bits/stdc++.h>
using namespace std;

void binary_search(int i,int n,int *d1,int *d2,int *s,int e ,string str )
{
int l=0;int r=n-1;
while (l <= r) {
int m = l + (r - l) / 2;
if (s[m] == d1[i]&&e[m]==d2[i])
{
string temp=str[i];
str[i]=str[m];
str[m]=temp;
}
if (s[m] < d1[i])
l = m + 1;
else
r = m - 1;
}

 }

int main()
{
int t;
cin>>t;
for(int m=1;m<=t;m++)
{
int n;
cin>>n;
int s[n];
int e[n];
int d1[n];
int d2[n];
for(int i=0;i<n;i++)
{
cin>>s[i];
cin>>e[i];

   }
   for(int i=0;i<n;i++)
   {
       d1[i]=s[i];
       d2[i]=e[i];
   }
   
   int temp;
   //sorting inorder of increasing starting time
   for(int i=0;i<n-1;i++)
   {
       for(int j=0;j<n-i-1;j++)
       {
           if(s[j]>s[j+1])
           {
               temp=s[j+1];
               s[j+1]=s[j];
               s[j]=temp;
               temp=e[j];
               e[j]=e[j+1];
               e[j+1]=temp;
           } 
       }
   }
   
   
   string str[n]; 
cout<<"Case #"<<m<<":"<<" ";    

   
   
   int flag=0;
 stack<int> c;
 stack<int> j;
 j.push(e[1]);//assigning 1st work to jamie
  str[1]='J';
 c.push(e[0]);//assigning 2nd work to c;
  str[0]='C';
for(int i=2;i<n;i++)
{
   
     if(s[i]>=c.top())
    {
        str[i]='C';
        c.pop();
        c.push(e[i]);
        continue;
    }
    
     else if(s[i]>=j.top())
    {
       str[i]='J';
       j.pop();
       j.push(e[i]);
       continue;
       
    }
    
    else
    {
        flag=2;
        break;
    }
}
for(int i=0;i<n;i++)
{
    if(d1[i]!=s[i]&&d2[i]!=e[i])
    {
        binary_search(i,n,d1,d2,s,e,str);
       /* for(int j=0;j<n;j++)
        {
            if(d1[i]==s[j]&&d2[i]==e[j])
            {
               
            }
        }*/
    }
    
}


if(flag==2)
{
    cout<<"IMPOSSIBLE"<<endl;
}
else
{
    for(int i=0;i<n;i++)
cout<<*(str+i);
cout<<endl;

}




}
return 0;

}

@1706157


This is the correct solution for this question
It is a simple sorting based greedy question

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.