Sunday, 14 February 2016

Where Are My Genes (UVALive 3473, POJ 2867, Regionals 2005 >> Latin America - South America)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100000

int arr[50000+5], l[1000+5], r[1000+5];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, rev, q, x;
    while(cin>>n && n)
    {
        cin>>rev;
        //for(int i=1;i<=n;i++) arr[i]=i;
        for(int i=0;i<rev;i++)
        {
            cin>>l[i]>>r[i];
        }
        cin>>q;
        cout<<"Genome "<<kk++<<"\n";
        while(q--)
        {
            cin>>x;
            for(int i=0;i<rev;i++)
                if(l[i]<=x && x<=r[i])
                    x = l[i]+r[i]-x;
            cout<<x<<"\n";
        }

    }

return 0;
}

Wednesday, 10 February 2016

XOR Sum (UVALive 4682, Regionals 2009 >> Asia - Amritapuri)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100000

struct node{
    int lft, rgt;
    node()
    {
        lft=rgt=-1;
    }
};
node tree[3500000];
int nodeNum;

void Insrt(int x)
{
    int pos=0, bit;
    for(int i=31;i>=0;i--)
    {
        bit = (x & (1<<i));
        if(bit)
        {
            if(tree[pos].rgt==-1)
            {
                tree[pos].rgt=++nodeNum;
                tree[nodeNum]=node();
            }
            pos=tree[pos].rgt;
        }
        else
        {
           if(tree[pos].lft==-1)
            {
                tree[pos].lft=++nodeNum;
                tree[nodeNum]=node();
            }
            pos=tree[pos].lft;
        }
    }
return;
}

int Qry(int x)
{
    int pos=0, bit, res=0;
    for(int i=31;i>=0;i--)
    {
        bit = (x & (1<<i));
        if(bit)
        {
            if(tree[pos].lft!=-1)
            {
                res |= (1<<i);
                pos = tree[pos].lft;
            }
            else pos=tree[pos].rgt;
        }
        else
        {
           if(tree[pos].rgt!=-1)
            {
                res |= (1<<i);
                pos=tree[pos].rgt;
            }
            else pos=tree[pos].lft;
        }
    }
return res;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, x, cumxor, mx;
    cin>>tc;
    while(tc--)
    {
        cin>>n;
        nodeNum=mx=cumxor=0;

        tree[0] = node();
        Insrt(0);
        for(int i=0;i<n;i++)
        {
            cin>>x;
            cumxor=cumxor^x;
            mx=max(mx, Qry(cumxor));
            //cout<<Qry(cumxor)<<endl;
            Insrt(cumxor);
        }
        cout<<mx<<"\n";
    }

return 0;
}