Can anyone help me with 3-Dimensional Binary Indexed Tree solution of http://www.spoj.pl/problems/COMBAT/?
<p>I'm trying to solve this problem <a href="http://www.spoj.pl/problems/COMBAT/" rel="nofollow">http://www.spoj.pl/problems/COMBAT/</a> using 3-D Binary Indexed Tree but not to able to get it Accepted. I'm getting Time Limit Exceeded for this problem, my logic is:</p>

<p>I'm storing the frequency (no of times a point(x,y,z) comes in a range of missile) using update function and then retrieving the actual frequency at point(x,y,z) using cum function and checking it if it is even then it is "Enemy", otherwise "Friend".</p>

<p>I'm really stuck with this problem and out of ideas to optimize it. Please help me with this code.</p>

<p>Sorry but I'm new to forums and I don't know how to post my code here but I have posted my code here <a href="http://apps.topcoder.com/forums/?module=Thread&amp;threadID=711119&amp;start=0&amp;mc=1" rel="nofollow">http://apps.topcoder.com/forums/?module=Thread&amp;threadID=711119&amp;start=0&amp;mc=1</a>, you can check it and please tell me how to post a code in stack overflow forums.</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;cstdio&gt;
#include&lt;algorithm&gt;

using namespace std;

int a[202][202][202]={0};

void update(int x1,int y1,int z1,int x2,int y2,int z2){
    while(x1&lt;=x2){
        int y=y1;
        while(y&lt;=y2){
            int z=z1;
            while(z&lt;=z2){
                a[x1][y][z]=a[x1][y][z]+1;
                z=z+(z &amp; -z);
            }
            y=y+(y &amp; -y);
        }
        x1=x1+(x1 &amp; -x1);
    }
}

int cum(int x,int y,int z){
    int result=0;
    while(x&gt;0){
        while(y&gt;0){
            while(z&gt;0){
                result=result+a[x][y][z];
                z=z-(z &amp; -z);   
            }
            y=y-(y &amp; -y);
        }
        x=x-(x &amp; -x);
    }
    return result;
}
/*
bool min_ele(int a,int b,int c,int p,int q,int r){
    return (a&lt;=p)&amp;&amp;(b&lt;=q)&amp;&amp;(c&lt;=r);
}*/
int main()
{
    int t;
    cin&gt;&gt;t;
    for(int w=0;w&lt;t;w++){
        int x1,y1,z1,x2,y2,z2;
        scanf("%d%d%d%d%d%d",&amp;x1,&amp;y1,&amp;z1,&amp;x2,&amp;y2,&amp;z2);
        int q;
        cin&gt;&gt;q;
        for(int i=0;i&lt;q;i++){
            char c;
            cin&gt;&gt;c;
            if(c=='U'){
                int x,y,z,r;
                scanf("%d%d%d%d",&amp;x,&amp;y,&amp;z,&amp;r);
                int m=x-r;
                int n=y-r;
                int o=z-r;
                if((x1&gt;m)&amp;&amp;(y1&gt;n)&amp;&amp;(z1&gt;o)){
                    m=x1;
                    n=y1;   
                    o=z1;
                }
                //if(y1&gt;n)
                //  n=y1;
                //if(z1&gt;o)
                //  o=z1;
                int p=x+r;
                int q=y+r;
                int rr=z+r;
                if((p&gt;x2)&amp;&amp;(q&gt;y2)&amp;&amp;(rr&gt;z2)){
                    p=x2;
                    q=y2;
                    rr=z2;  
                }
                //if(q&gt;y2)
                //  q=y2;
                //if(rr&gt;z2)
                //  rr=z2;  
                update(m-999,n-999,o-999,p-999,q-999,rr-999);
            }
            else{
                int x,y,z;
                scanf("%d%d%d",&amp;x,&amp;y,&amp;z);
            //  cout&lt;&lt;cum(x-999,y-999,z-999)&lt;&lt;"\n";
            //  cout&lt;&lt;cum(x-1-999,y-1-999,z-1-999)&lt;&lt;"\n";
                int check=cum(x-999,y-999,z-999)-cum(x-1-999,y-1-999,z-1-999);
                if(check % 2 == 0){
                    cout&lt;&lt;"Enemy\n";    
                }
                else
                    cout&lt;&lt;"Friend\n";
            }
        }
    }

    return 0;
}
</code></pre>
