Please read the new rule regarding the restriction on the use of AI tools. ×

Finding the mistake in sorting vector of objects

Revision en1, by VandanRogheliya, 2020-07-15 11:49:08

I was solving D-Query question from SPOJ.

This was my original solution using an offline approach. Strangely for queries, more than 16 this program gave a segmentation fault

I modified the program according to one editorial to this and got AC

Note: Both programs only differ in sorting comp_ends function:

In 1st approach

int comp_ends(const Info& a, const Info& b) {
  if (a.type == b.type) {
    if (a.type == Q) return a.r <= b.r;
    return a.i <= b.i;
  } else {
    if (a.type == Q) return a.r < b.i;
    return a.i <= b.r;
  }
}

In 2nd approach

int comp_ends(const Info& a, const Info& b) {
  if (a.end < b.end) return true;
  else if (a.end == b.end) return a.type == I;
  else return false;
}

It will be very helpful if someone pointed out my mistake.

Thanks!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English VandanRogheliya 2020-07-15 11:49:08 998 Initial revision (published)