WA in Game from IOI'13

Here is my code…

#include "bits/stdc++.h" using namespace std;

#define ll unsigned long long

ll r, c;

long long gcd2(long long X, long long Y) {
    long long tmp;
    while (X != Y && Y != 0) {
        tmp = X;
        X = Y;
        Y = tmp % Y;
    }
    return X; }

ll segtrees[100][100000];

void build() {
    for(ll i = 0; i<r; i++)
    for(ll j = 0; j<100000; j++)
    segtrees[i][j]=0; }

void update(ll rw, ll col, ll val, ll idx, ll hd, ll tl) {
    //cout << "U " << idx << " " << hd << " " << tl << "\n";
    if(hd==tl && tl==col)
    {
        segtrees[rw][idx] = val;
        return;
    }
    ll mid = (hd+tl)/2;
    if(col>=hd && col<=mid)
    update(rw, col, val, idx*2, hd, mid);
    else if(col>mid && col<=tl)
    update(rw, col, val, idx*2+1, mid+1, tl);
    segtrees[rw][idx]=gcd2(segtrees[rw][idx*2], segtrees[rw][idx*2+1]); }

ll query(ll rw, ll i, ll j, ll idx, ll hd, ll tl) {
    //cout << "Q " << idx << " " << hd << " " << tl << "\n";
    ll mid = (hd+tl)/2;
    if(hd>=i && hd<=j && tl>=i && tl<=j)
    return segtrees[rw][idx];
    else if((hd>=i && hd<=j && (tl>=j || tl <=i)) || (tl>=i && tl<=j && (hd>=j || hd<=i)))
    return gcd2(query(rw, i, j, idx*2, hd, mid), query(rw, i, j, idx*2+1, mid+1, tl));
    else
    {
    //cout << "POOP!\n";
    return 0;
    } }

void calculate(ll p, ll q, ll u, ll v) {
     ll ans = 0;
    for(int i = p; i<=u; i++)
    ans = gcd2(ans, query(i, q, v, 1, 0, c-1));
    cout << ans << endl; }

int main() {
    ll n;
    ll p, q, u, v, k;
    cin >> r >> c >> n;
    build();
    while(n--)
    {
        int choice;
        cin >> choice;
        choice--;
        cin >> p >> q;
        if(choice)
        {
           cin >> u >> v;
           calculate(p, q, u, v);
        }
        else
        {
            cin >> k;
            update(p, q, k, 1, 0, c-1);
        }
    }

}

I’m trying to score a 37. My program maintains N segment trees for the N rows. Why do I get a WA verdict?

Thanks,
Arpan

//