Flip Coins . Getting Wrong Answer Please Help

segment-tree

#1

#include<stdio.h>
#define MAX (100001)
#define true 1
#define false 0
#define pc(x) putchar_unlocked(x)
#define gc() getchar_unlocked()
int arr[100001] = {0};
int tree[200002]={0};
int lazy[200002]={0};

/**

  • Increment elements within range [i, j] with value value
    */
    void update_tree(int node, int a, int b, int i, int j, int value) {

    if(i>b || j<a || a>b) //out of range
    return ;

    if(a == b) { // Leaf node
    if(!lazy[node])
    {
    tree[node] += value;
    tree[node]%=2;
    }
    else
    {
    lazy[node]=false;
    }

    }

    if(i<=a && j>=b) //completely lies in range
    {
    lazy[node] = !lazy[node];
    return ;
    }

    update_tree(node2, a, (a+b)/2, i, j, value); // Updating left child
    update_tree(1+node
    2, 1+(a+b)/2, b, i, j, value); // Updating right child

    tree[node] = tree[node2] + tree[node2+1]; // Updating root with max value
    }

/**

  • Query tree to get max element value within range [i, j]
    */
    int query_tree(int node, int a, int b, int i, int j) {

    if(a > b || a > j || b < i) return 0; // Out of range

    if(a >= i && b <= j) // Current segment is totally within range [i, j]
    {
    if(lazy[node])
    {
    tree[node] = b-a + 1 - tree[node];
    lazy[node]=!lazy[node];
    if(a != b)
    {
    lazy[node2] = !lazy[node2]; // Mark child as lazy
    lazy[node2+1] = !lazy[node2+1]; // Mark child as lazy
    }
    }
    return tree[node];
    }
    if(lazy[node]) { // This node needs to be updated
    tree[node] = b-a + 1 - tree[node]; // Update it

     if(a != b) {
     	lazy[node*2] = !lazy[node*2]; // Mark child as lazy
     	lazy[node*2+1] = !lazy[node*2+1]; // Mark child as lazy
     }
    
     lazy[node] = false; // Reset it
    

    }

    int q1 = query_tree(node2, a, (a+b)/2, i, j); // Query left child
    int q2 = query_tree(1+node
    2, 1+(a+b)/2, b, i, j); // Query right child

    int res = q1 + q2; // Return final result

    return res;
    }

int main() {
int N,Q,T,A,B;
scanf("%d%d",&N,&Q);
//N = get_number();
//Q = get_number();
// build_tree(1, 0, N-1);
while(Q–)
{
scanf("%d%d%d",&T,&A,&B);
//T = get_number();
//A = get_number();
//B = get_number();
if(!T)
{
update_tree(1,0,N-1,A,B,1);
}
else
{
printf("%d
",query_tree(1,0,N-1,A,B));
//put_number(query_tree(1,0,N-1,A,B));

    }
}
return 0;

}