title: Quadrant Queries
body: <p>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.</p>
  
  <p>This is the question:</p>
  
  <p>Quadrant Queries</p>
  
  <p>There are N points in the plane. The ith point has coordinates (xi, yi). Perform the following queries:</p>
  
  <ol>
  <li>Reflect all points between point i and j both including along the X axis. This query is represented as <code>X i j</code></li>
  <li>Reflect all points between point i and j both including along the Y axis. This query is represented as <code>Y i j</code></li>
  <li>Count how many points between point i and j both including lie in each of the 4 quadrants. This query is represented as <code>C i j</code></li>
  </ol>
  
  <h2>Input:</h2>
  
  <p>The first line contains N, the number of points. N lines follow.</p>
  
  <p>The ith line contains xi and yi separated by a space.</p>
  
  <p>The next line contains Q the number of queries. The next Q lines contain one query each, of one of the above forms.</p>
  
  <p>All indices are 1 indexed.</p>
  
  <h2>Output:</h2>
  
  <p>Output one line for each query of the type <code>C i j</code>. 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.</p>
  
  <h2>Constraints:</h2>
  
  <pre><code>1 &lt;= N &lt;= 100000
  1 &lt;= Q &lt;= 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 &lt;=i &lt;=j &lt;=N
  </code></pre>
  
  <h2>Sample Input:</h2>
  
  <pre><code>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
  </code></pre>
  
  <h2>Sample Output:</h2>
  
  <pre><code>1 1 1 1
  1 1 0 0
  0 2 0 1
  </code></pre>
  
  <h2>Explanation:</h2>
  
  <p>When a query says <code>X i j</code>, 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</p>
  
  <p><code>C 1 4</code> 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.</p>
  
  <p>Next we reflect the points between indices '2 4' along the X axis. So the new coordinates are :</p>
  
  <pre><code>1 1
  -1 -1
  -1 1
  1 1
  </code></pre>
  
  <p>Now <code>C 3 4</code> 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.</p>
  
  <h2>Current Code</h2>
  
  <p>Here is my solution in c:</p>
  
  <pre><code>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 &gt; e || j &lt; b)
            return ;
  
  
        if (b &gt;= i &amp;&amp; e &lt;= 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 ;
  }
  </code></pre>