You are not logged in. Please login at www.codechef.com to post your questions!

×

Flip Coins . Getting Wrong Answer Please Help

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(node*2, 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[node*2] + tree[node*2+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[node*2] = !lazy[node*2]; // Mark child as lazy
            lazy[node*2+1] = !lazy[node*2+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(node*2, 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\n",query_tree(1,0,N-1,A,B)); //put_number(query_tree(1,0,N-1,A,B));

    }
}
return 0;

}

asked 06 Sep '14, 01:38

agam_goyal's gravatar image

2★agam_goyal
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,727

question asked: 06 Sep '14, 01:38

question was seen: 840 times

last updated: 06 Sep '14, 01:38