UVa – 12532 – Interval Product

This problem is a straightforward implimentation of segment tree.
#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define pii                 pair<int,int>
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define pii                 pair<int,int>
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define for0(i,n)          for(int i=0;i<n;i++)
#define for1(i,n)          for(int i=1;i<=n;i++)
#define forrev(i,n)        for(int i=n-1; i>=0; i--)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define forba(i,b,a)       for(int i=b;i>=a;i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d: ",z)
#define PI                  acos(-1) //3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<row && ty>=0 && ty<col
#define intlim              2147483648
#define mx                  100005
#define inf                 100000008

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

int input[mx],tree[3*mx];

void det(int x,int node)
{
    if(x>0) tree[node]=1;
    else if(x<0) tree[node]=-1;
    else tree[node]=0;
}

void build(int node,int low,int high)
{
    if(low==high)
    {
        det(input[low],node); return;
    }
    int mid=(low+high)/2;
    int left=2*node;
    int right=left+1;
    build(left,low,mid);
    build(right,mid+1,high);
    tree[node]=tree[left]*tree[right];
}

void update(int node,int low,int high, int index,int val)
{
    if(low>index || high<index) return;
    else if(low>=index && high<=index)
    {
        det(val,node); return;
    }
    int mid=(low+high)/2;
    int left=2*node;
    int right=left+1;
    update(left,low,mid,index,val);
    update(right,mid+1,high,index,val);
    tree[node]=tree[left]*tree[right];
}

int query(int node,int low,int high, int qlow,int qhigh)
{
    if(low>qhigh || high<qlow) return 1;
    else if(low>=qlow && high<=qhigh) return tree[node];
    int mid=(low+high)/2;
    int left=2*node;
    int right=left+1;
    int l=query(left,low,mid,qlow,qhigh);
    int r=query(right,mid+1,high,qlow,qhigh);
    return l*r;
}


int main()
{
    //CIN;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,k;
    while(sff(n,k)==2)
    {
        for1(i,n) sf(input[i]);
        build(1,1,n);
        string s,ss;
        for1(i,k)
        {
            int qlow,qhigh,index,val;
            cin>>s;
            if(s=="C")
            {
                sff(index,val);
                update(1,1,n,index,val);
            }
            else
            {
                sff(qlow,qhigh);
                int ans=query(1,1,n,qlow,qhigh);
                if(ans==1) ss+='+';
                else if(ans==-1) ss+='-';
                else ss+='0';
            }
        }
        cout<<ss<<endl;
        ss.clear();
        ms(tree,0);
    }

    return 0;
}

UVa – 11235 – Frequent values

Solution:

This problem can be efficiently solved by the Segment tree. 

Since it is stated that, the input will be strictly in increasing order, a natural technique can be applied.

For this we require 3 arrays :
1. array input stores the input numbers,
2. array count stores the frequency of each input number,
3. and array start stores the index of the input list where a particular input number appeared first.

For example,
input = { -1, -1, 1, 1, 1, 1, 3, 10, 10, 10}
count = { 2, 2, 4, 4, 4, 1, 3, 3, 3 }
start = { 1, 1, 3, 3, 3, 7, 8, 8, 8 }

At first, a segment tree is constructed where each node will store the value of the maximum count (from the count array) of its respective range [ a,b ].
Let, the query range be [ i,j ].
Now 2 cases can occur The value at index i and j are –
1. same  i.e input[ i ] = input[ j ].
2. different  i.e  input[ i ] ≠ input[ j ].

#Case 1:
Solving this case is the easiest. Since input[ i ] = input[ j ] , all the numbers in the range [ i,j ] are same ( since the numbers are non-descending ). So the answer for this case 1 is j – i + 1.

#Case 2:
In this case, there exists an index x where input[ i ] = input[ x ] and input[ i ] ≠ input[ x + 1 ]. Let, k = x + 1. So, the value of k = start [ i ] + count [ i ] .
So, the frequency of the value input[ i ] in the range [ i,k ] is cnt1 = k – i .
The frequency of input[ j ] in the range [ i,j ] is cnt2 = j –start [ j ] + 1 .

The maximum frequency of the values in range [ i,j ] may also exists in the range [ k, start[ j ] – 1 ]. This can be found by querying the segment tree for the maximum value in the range [ k, start[ j ] – 1 ]. Let the maximum count returned by the query be cnt3.

Therefore the answer for case 2 is max ( cnt1, cnt2 , cnt3 ).

 

#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define pii                 pair<int,int>
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define pii                 pair<int,int>
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define for0(i,n)          for(int i=0;i<n;i++)
#define for1(i,n)          for(int i=1;i<=n;i++)
#define forrev(i,n)        for(int i=n-1; i>=0; i--)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define forba(i,b,a)       for(int i=b;i>=a;i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d: ",z)
#define PI                  acos(-1) //3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<row && ty>=0 && ty<col
#define intlim              2147483648
#define mx                  100005
#define inf                 100000008

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

int input[mx],tree[3*mx],cnt[mx],start[mx];
map<int,int> mp;

void build (int node,int low,int high)
{
    if(low==high)
    {
        tree[node]=cnt[low];
        return;
    }
    int mid=(low+high)/2;
    int left=2*node;
    int right=2*node+1;
    build(left,low,mid);
    build(right,mid+1,high);
    tree[node]=max(tree[left],tree[right]);
}

int query(int node,int low,int high,int qlow,int qhigh)
{
    if(low>qhigh || high<qlow) return -inf;
    else if(low>=qlow && high<=qhigh)
    {
        return tree[node];
    }
    int mid=(low+high)/2;
    int left=2*node;
    int right=2*node+1;
    int l=query(left,low,mid,qlow,qhigh);
    int r=query(right,mid+1,high,qlow,qhigh);
    return max(l,r);
}

int main()
{
    //CIN;
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int n,q;
    while(sf(n)==1 && n!=0)
    {
        sf(q);
        for1(i,n)
        {
            sf(input[i]);
            mp[input[i]]++;
        }
        int y=-inf,k;
        for1(i,n)
        {
            int x=mp[input[i]];
            cnt[i]=x;
            if(input[i]!=y)
            {
                k=i;
                y=input[i];
            }
            start[i]=k;
        }
        build(1,1,n);
        for1(i,q)
        {
            int qlow,qhigh,cnt1,cnt2,cnt3;
            sff(qlow,qhigh);
            if(input[qlow]!=input[qhigh])
            {
                int k=start[qlow]+cnt[qlow];
                cnt1=k-qlow;
                cnt2=qhigh-start[qhigh]+1;
                cnt3=query(1,1,n,k,start[qhigh]-1);
                pf("%d\n",max3(cnt1,cnt2,cnt3));
            }
            else pf("%d\n",qhigh-qlow+1);
        }
        ms(tree,0);
        ms(cnt,0);
        ms(start,0);
        mp.clear();
    }
    return 0;
}

SPOJ – DQUERY – D-query

#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define pii                 pair<int,int>
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define pii                 pair<int,int>
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define for0(i,n)          for(int i=0;i<n;i++)
#define for1(i,n)          for(int i=1;i<=n;i++)
#define forrev(i,n)        for(int i=n-1; i>=0; i--)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define forba(i,b,a)       for(int i=b;i>=a;i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d: ",z)
#define PI                  acos(-1) //3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<row && ty>=0 && ty<col
#define intlim              2147483648
#define MAX                 100005
#define inf                 100000008

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

int a[30004],cnt[1000006],result[200005],block_sz,ans=0;

struct node
{
    int l,r,i;
}s[200005];

bool comp(node x,node y)
{
    if (x.l/block_sz != y.l/block_sz) /// Different blocks, sort by block.
        return x.l/block_sz < y.l/block_sz;
    return x.r < y.r; /// Same block, sort by R value
}

void add(int pos)
{
    cnt[a[pos]]++;
    if(cnt[a[pos]]==1) ans++;
}

void rmv(int pos)
{
    cnt[a[pos]]--;
    if(cnt[a[pos]]==0) ans--;
}

void MO(int n,int q)
{
    block_sz=(int)sqrt(n);
    sort(s,s+q,comp);
    int c_l=0, c_r=0;

    for0(i,q)
    {
        int l=s[i].l, r=s[i].r;

        while(c_l<l)
        {
            rmv(c_l);
            c_l++;
        }
        while(c_l>l)
        {
            add(c_l-1);
            c_l--;
        }
        while(c_r<=r)
        {
            add(c_r);
            c_r++;
        }
        while(c_r>r+1)
        {
            rmv(c_r-1);
            c_r--;
        }
        result[s[i].i]=ans;
    }
    for0(i,q) pf("%d\n",result[i]);
}

int main()
{
    //CIN;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,q;
    sf(n);
    for0(i,n) sf(a[i]);
    sf(q);
    for0(i,q)
    {
        sff(s[i].l,s[i].r);
        s[i].l--;s[i].r--;
        s[i].i=i;
    }
    MO(n,q);
    return 0;
}

SPOJ – HORRIBLE – Horrible Queries

#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define qu                  queue
#define pqu                 priority_queue
#define vc                  vector
#define vi                  vector<int>
#define vll                 vector<long long>
#define pii                 pair<int,int>
#define pis                 pair<int,string>
#define psi                 pair<string,int>
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define for0(i,n)          for(int i=0;i<n;i++)
#define for1(i,n)          for(int i=1;i<=n;i++)
#define forcmp(i,n)        for(int i=1;i<n;i++)
#define forrev(i,n)        for(int i=n-1; i>=0; i--)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define forba(i,b,a)       for(int i=b;i>=a;i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d:\n",z)
#define PI                  acos(-1) //3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<row && ty>=0 && ty<col
#define intlim              2147483648
#define MAX                 1000000
#define inf                 100000000
#define mx                  100005

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

struct data
{
    ll prop,sum;
};

data tree[3*mx];

void pushdown(int node, int low, int high)
{
    if(tree[node].prop!=0)
    {
        int mid=(low+high)/2;
        tree[node*2].sum+=(mid-low+1)*tree[node].prop;
        tree[node*2+1].sum+=(high-mid)*tree[node].prop;
        tree[node*2].prop+=tree[node].prop;
        tree[node*2+1].prop+=tree[node].prop;
        tree[node].prop=0;
    }
}

void update(int node, int low, int high, int qlow, int qhigh, ll val)
{
    if(qlow>high || qhigh<low) return;
    if(low>=qlow && high<=qhigh)
    {
        tree[node].sum+=(high-low+1)*val;
        tree[node].prop+=val;
        return;
    }

    pushdown(node,low,high);

    int left=node*2;
    int right=left+1;
    int mid=(low+high)/2;

    update(left,low,mid,qlow,qhigh,val);
    update(right,mid+1,high,qlow,qhigh,val);

    tree[node].sum=tree[left].sum+tree[right].sum;
}

ll query(int node, int low, int high, int qlow, int qhigh)
{
    if(qlow>high || qhigh<low) return 0;
    if(low>=qlow && high<=qhigh) return tree[node].sum;

    pushdown(node,low,high);

    int left=node*2;
    int right=left+1;
    int mid=(low+high)/2;

    ll l=query(left,low,mid,qlow,qhigh);
    ll r=query(right,mid+1,high,qlow,qhigh);
    return l+r;
}

int main()
{
    //CIN;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    sf(t);
    for1(z,t)
    {
        int n,q,mark,qlow,qhigh,val;
        sff(n,q);
        for1(i,q)
        {
            sfff(mark,qlow,qhigh);
            if(mark==0)
            {
                sf(val);
                update(1,1,n,qlow,qhigh,val);
            }
            else pf("%lld\n",query(1,1,n,qlow,qhigh));
        }
        ms(tree,0);
    }
    return 0;
}


Light OJ – 1080 – Binary Simulation

#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define qu                  queue
#define pqu                 priority_queue
#define vc                  vector
#define vi                  vector<int>
#define vll                 vector<long long>
#define pii                 pair<int,int>
#define pis                 pair<int,string>
#define psi                 pair<string,int>
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define for0(i,n)          for(int i=0;i<n;i++)
#define for1(i,n)          for(int i=1;i<=n;i++)
#define forcmp(i,n)        for(int i=1;i<n;i++)
#define forrev(i,n)        for(int i=n-1; i>=0; i--)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define forba(i,b,a)       for(int i=b;i>=a;i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d:\n",z)
#define PI                  acos(-1) //3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<row && ty>=0 && ty<col
#define intlim              2147483648
#define MAX                 1000000
#define inf                 100000000
#define mx                  100005

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

struct data
{
    ll prop,sum;
};

bool c;
data tree[3*mx],input[mx];

void build(int node,int low,int high)
{
    if(low==high) {tree[node].sum=input[low].sum;return; }
    int mid=(low+high)/2;
    int left=2*node;
    int right=2*node+1;
    build(left,low,mid);
    build(right,mid+1,high);
}

void pushdown(int node, int low, int high)
{
    if(tree[node].prop!=0)
    {
        int mid=(low+high)/2;
        int left=2*node;
        int right=2*node+1;
        tree[left].prop+=tree[node].prop;
        tree[right].prop+=tree[node].prop;
        tree[node].prop=0;
    }
}

void update(int node, int low, int high, int qlow, int qhigh)
{
    if(high<qlow || low>qhigh) return;  ///No Overlap
    else if(low>=qlow && high<=qhigh)  ///Total Overlap
    {
        tree[node].prop++;
        return;
    }

    pushdown(node,low,high);

    int left=node*2;
    int right=left+1;
    int mid=(low+high)/2;

    update(left,low,mid,qlow,qhigh);
    update(right,mid+1,high,qlow,qhigh);
}

int query(int node, int low, int high, int qlow, int qhigh)
{
    if(high<qlow || low>qhigh) return 0; ///No Overlap
    else if(low>=qlow && high<=qhigh)   ///Total Overlap
    {
        if(tree[node].prop%2==1) c= 1-tree[node].sum;
        else c= tree[node].sum;
        return 0;
    }
    pushdown(node,low,high);
    int left=node*2;
    int right=left+1;
    int mid=(low+high)/2;

    query(left,low,mid,qlow,qhigh);
    query(right,mid+1,high,qlow,qhigh);
}

int main()
{
    //CIN;
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int t;
    sf(t);
    for1(z,t)
    {
        string str;
        cin>>str;
        for0(i,sz(str)) input[i+1].sum=str[i]-'0';
        build(1,1,sz(str));
        int q,qlow,qhigh,val;
        sf(q);
        case2(z);
        for1(i,q)
        {
            getchar();
            char ch;
            sfc(ch);
            if(ch=='I')
            {
                sff(qlow,qhigh);
                update(1,1,sz(str),qlow,qhigh);
            }
            else
            {
                sf(val);
                query(1,1,sz(str),val,val);
                pf("%d\n",c);
            }
        }
        ms(tree,0);
    }
    return 0;
}