# Light OJ 1080 – Binary Simulation

This problem can be solved by using both BIT (Binary Indexed Tree) and Segment tree. Both solution is Here..

```
/*BIT (Binary Indexed Tree)*/

/*
+-+ +-+ +-+
|R| |.| |S|
+-+ +-+ +-+
*/

#include &lt;bits/stdc++.h&gt;

#define pii             pair &lt;int,int&gt;
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout&lt;&lt;#x &quot; = &quot;&lt;&lt;(x)&lt;&lt;endl
#define VI              vector &lt;int&gt;
#define DBG             pf(&quot;Hi\n&quot;)
#define MOD             100007
#define MAX             100010
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf(&quot;%d&quot;,&amp;a)
#define sff(a,b)        scanf(&quot;%d%d&quot;,&amp;a,&amp;b)
#define sfff(a,b,c)     scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c)
#define loop(i,n)       for(int i=0;i&lt;n;i++)
#define REP(i,a,b)      for(int i=a;i&lt;b;i++)
#define TEST_CASE(t)    for(int z=1;z&lt;=t;z++)
#define PRINT_CASE      printf(&quot;Case %d:\n&quot;,z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

int tree[MAX],n;
char str[MAX];

void update2(int indx, int v)
{
while(indx&lt;=n)
{
tree[indx]+=v;
indx += (indx &amp; (-indx));
}
}

void update(int i, int j)
{
update2(i,1);
update2(j+1,-1);
}

int query(int indx)
{
int sum=0;
while(indx)
{
sum+=tree[indx];
indx-=(indx &amp; (-indx));
}
return sum;
}

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t;
sf(t);
TEST_CASE(t)
{
ms(tree,0);
sc(&quot;%s&quot;,&amp;str[1]);
n=strlen(&amp;str[1]);
char ch;
int m;
sf(m);
PRINT_CASE;
while(m--)
{
getchar();
sc(&quot;%c&quot;,&amp;ch);

if(ch=='I')
{
int a,b;
sff(a,b);
update(a,b);
}
else
{
int a;
sf(a);
if(query(a)%2==1)
{
if(str[a]=='0') pf(&quot;1\n&quot;);
else pf(&quot;0\n&quot;);
}
else
pf(&quot;%c\n&quot;,str[a]);
}
}
}
return 0;
}

```

Another solution using segment tree is here -> 1080 – Binary Simulation(Segment Tree)