Can we reduce the complexity using segment tree here or any other means? if yes, How?
it's a code for the finding longest chain like the example below:

if
A(x1,y1) at index i in array
B(x2,y2) at index j in array

if( i<j and x1<=x2 and y1<=y2 and !(x1==x2 and y1==y2))
then A and B are in a chain of length 2 

let say
A={(0,0),(1,0),(1,1),(2,1),(2,0)}
then longest chain = {(0,0),(1,0),(1,1),(2,1)}
so answer here is 4 elements

I have used Dynamic programming here which is O(N^2) for my code

struct Pair
{
    int x;
    int y;
};



int main()
{

    int n;
    cin>>n;

    vector<Pair> arr;
    int L[1000000];

    Pair a;

    int i;int Maxchain=0;
    for(i=0;i<n;i++)
    {

        cin>>a.x>>a.y;
        arr.push_back(a);

        L[i]=1;
        for (int j = i-1; j >=0; j--)
        {

            if ((L[j]>=L[i])&&(arr[j].x <= arr[i].x) && (arr[j].y <= arr[i].y) && !(arr[j].x == arr[i].x && arr[j].y == arr[i].y))
                L[i] = L[j]+1;


        }

                Maxchain = L[i]>Maxchain ?L[i]:Maxchain ;

    }
    cout<<Maxchain;

    return 0;
}
share|improve this question

put on hold as off-topic by forsvarir, Quill, t3chb0t, denis, ferada Jan 14 at 23:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – forsvarir, Quill, t3chb0t, denis, ferada
If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.