Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7954057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:18:28+00:00 2026-06-04T03:18:28+00:00

I have almost solved this quadrant queries problem of Interviewstreet using segment trees with

  • 0

I have almost solved this quadrant queries problem of Interviewstreet using segment trees with lazy propagation but I’m still getting wrong answer so I need help in my code.

This is the question:

Quadrant Queries

There are N points in the plane. The ith point has coordinates (xi, yi). Perform the following queries:

  1. Reflect all points between point i and j both including along the X axis. This query is represented as X i j
  2. Reflect all points between point i and j both including along the Y axis. This query is represented as Y i j
  3. Count how many points between point i and j both including lie in each of the 4 quadrants. This query is represented as C i j

Input:

The first line contains N, the number of points. N lines follow.

The ith line contains xi and yi separated by a space.

The next line contains Q the number of queries. The next Q lines contain one query each, of one of the above forms.

All indices are 1 indexed.

Output:

Output one line for each query of the type C i j. The corresponding line contains 4 integers; the number of points having indices in the range [i..j] in the 1st,2nd,3rd and 4th quadrants respectively.

Constraints:

1 <= N <= 100000
1 <= Q <= 100000
You may assume that no point lies on the X or the Y axis.
All (xi,yi) will fit in a 32-bit signed integer
In all queries, 1 <=i <=j <=N

Sample Input:

4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
Y 1 2
C 1 3

Sample Output:

1 1 1 1
1 1 0 0
0 2 0 1

Explanation:

When a query says X i j, it means that take all the points between indices i and j both including and reflect those points along the X axis. The i and j here have nothing to do with the co-ordinates of the points. They are the indices. i refers to point i and j refers to point j

C 1 4 asks you to ‘Consider the set of points having index in {1,2,3,4}. Amongst those points, how many of them lie in the 1st, 2nd, 3rd and 4th quads respectively?’ The answer to this is clearly 1 1 1 1.

Next we reflect the points between indices ‘2 4’ along the X axis. So the new coordinates are :

1 1
-1 -1
-1 1
1 1

Now C 3 4 is ‘Consider the set of points having index in {3,4}. Amongst those points, how many of them lie in the 1st, 2nd, 3rd and 4th quads respectively?’ Point 3 lies in quadrant 2 and point 4 lies in quadrant 1. So the answer is 1 1 0 0.

Current Code

Here is my solution in c:

void query(int node, int b, int e, int i, int j, char ch)
{

      if(L[node][0]!=0 || L[node][1]!=0)
    {
      if(b!=e){
      L[2*node+1][0]=L[node][0];
      L[2*node+1][1]=L[node][1];
      L[2*node+2][0]=L[node][0];
      L[2*node+2][1]=L[node][1];
      }
      if(L[node][0]%2!=0)
      {
      tmp=Q[node][0];
      Q[node][0]=Q[node][3];
      Q[node][3]=tmp;

      tmp=Q[node][1];
      Q[node][1]=Q[node][2];
      Q[node][2]=tmp;
      }
      if(L[node][1]%2!=0)
      {
      tmp=Q[node][0];
      Q[node][0]=Q[node][1];
      Q[node][1]=tmp;

      tmp=Q[node][2];
      Q[node][2]=Q[node][3];
      Q[node][3]=tmp;
      }
      L[node][0]=0;
      L[node][1]=0;

    }

      if (i > e || j < b)
          return ;


      if (b >= i && e <= j)
      {
    if(ch == 'C'){
    ans[0]+=Q[node][0];
    ans[1]+=Q[node][1];
    ans[2]+=Q[node][2];
    ans[3]+=Q[node][3];
    }
    if(ch == 'X')
    {
      if(b!=e){
      L[2*node+1][0]++;
      L[2*node+2][0]++;
      }
      tmp=Q[node][0];
      Q[node][0]=Q[node][3];
      Q[node][3]=tmp;
      tmp=Q[node][1];
      Q[node][1]=Q[node][2];
      Q[node][2]=tmp;
    }
    if(ch == 'Y')
    {
      if(b!=e){
      L[2*node+1][1]++;
      L[2*node+2][1]++;
      }
      tmp=Q[node][0];
      Q[node][0]=Q[node][1];
      Q[node][1]=tmp;
      tmp=Q[node][2];
      Q[node][2]=Q[node][3];
      Q[node][3]=tmp;
    }
    return ;
      }


       query(2 * node +1, b, (b + e) / 2, i, j,ch);
      query(2 * node + 2, (b + e) / 2 + 1, e, i, j,ch);


    Q[node][0]=Q[2*node+1][0] + Q[2*node+2][0];
    Q[node][1]=Q[2*node+1][1] + Q[2*node+2][1];
    Q[node][2]=Q[2*node+1][2] + Q[2*node+2][2];
    Q[node][3]=Q[2*node+1][3] + Q[2*node+2][3];
    return ;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:18:30+00:00Added an answer on June 4, 2026 at 3:18 am

    If I understand your algorithm correctly, you are using the L array to keep track of whether a a range of points needs to be flipped or not, but deferring the actual flip until it becomes necessary.

    In this case, I think there might be a problem with the lines:

    void query(int node, int b, int e, int i, int j, char ch)
    {
      if(L[node][0]!=0 || L[node][1]!=0)
      {
        if(b!=e){
          L[2*node+1][0]=L[node][0];
          L[2*node+1][1]=L[node][1];
          L[2*node+2][0]=L[node][0];
          L[2*node+2][1]=L[node][1];
        }
    

    Suppose L[node][0] was 1, and L[2*node+1][0] was already 1. This means that some previous step wanted to flip the nodes at 2*node+1, and then this step also wants to flip these nodes. These flips should cancel out and L[2*node+1][0] should become zero.

    I think you should change these lines to use xor so that a double flip will cancel:

    void query(int node, int b, int e, int i, int j, char ch)
    {
      if(L[node][0]!=0 || L[node][1]!=0)
      {
        if(b!=e){
          L[2*node+1][0]^=L[node][0];
          L[2*node+1][1]^=L[node][1];
          L[2*node+2][0]^=L[node][0];
          L[2*node+2][1]^=L[node][1];
        }
    

    (Or perhaps I have misunderstood the approach!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I almost have this solved but need a little push. Here's what I have:
HI, I have almost solved this but have now got stuck! What I need
I though I had solved this problem, but it is back: Code generation for
Im new to Javascript and jquery and I almost have my problem solved. I
I have almost solved a huge problem with png files and transparency in IE
I have tried almost all the solutions from SO but no success :(. I
I have been trying to wrap my head around why this is happening but
I have started to solve PE problems a year ago, but within this year,
This question is a more special case of the problem described (and solved) in
UPDATE #2: I have solved almost all my issues bar the one major one.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.