题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1080

1080 - Binary Simulation
Time Limit: 2 second(s) Memory Limit: 64 MB

Given a binary number, we are about to do some operations on the number. Two types of operations can be here.

'I i j'    which means invert the bit from i to j (inclusive)

'Q i'    answer whether the ith bit is 0 or 1

The MSB (most significant bit) is the first bit (i.e. i=1). The binary number can contain leading zeroes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing a binary integer having length n (1 ≤ n ≤ 105). The next line will contain an integer q (1 ≤ q ≤ 50000) denoting the number of queries. Each
query will be either in the form 'I i j'where i, j are integers and 1 ≤ i ≤ j ≤ n. Or the query will be in the form 'Q i' where i is an integer and 1 ≤ i ≤ n.

Output

For each case, print the case number in a single line. Then for each query 'Q i' you have to print 1 or 0 depending on the ith bit.

Sample Input

Output for Sample Input

2

0011001100

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

1011110111

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

Case 1:

0

1

1

0

Case 2:

0

0

0

1

Note

Dataset is huge, use faster i/o methods.


PROBLEM SETTER: JANE ALAM JAN

题目大意:给你一段二进制数,当输入为 I  x  y时。就把这段变成它的反串 。当为Q 就查询当前x这个位置的数字视为0还是1。

思路:此题有两种解题的思路,一种是线段树。一种是树状数组。当用树状数组时。能够看成是一个区间改动,单点查询的问题,当我们用线段树时。那么就须要用到懒惰标记。

对于这道题树状数组确实比线段是快非常多。

。。。

线段树代码:

//time 584 ms

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#define L(u) u<<1
#define R(u) u<<1|1 using namespace std; const int Max=100010;
char str[Max];
class Node
{
public:
int l,r;
char str;
int add;
}node[Max*4]; void build(int l,int r,int u)
{
node[u].l=l;
node[u].r=r;
node[u].add=0;
if(l==r)
{
node[u].str=str[l];
return;
}
int mid=(l+r)/2;
build(l,mid,L(u));
build(mid+1,r,R(u));
} void pushdown(int u) //注意这儿要懒惰标记
{
node[L(u)].add+=node[u].add; //主义要加上。。。 。 。
node[R(u)].add+=node[u].add;
node[u].add=0;
} void update(int l,int r,int u)
{
if(l<=node[u].l&&node[u].r<=r)
{
node[u].add++;
return ;
}
if(node[u].add) pushdown(u);
int mid=(node[u].l+node[u].r)/2;
if(r<=mid)
{
update(l,r,L(u));
}
else if(l>mid)
{
update(l,r,R(u));
}
else
{
update(l,mid,L(u));
update(mid+1,r,R(u));
}
return ;
} char query(int l,int r,int u)
{
if(l<=node[u].l&&node[u].r<=r)
{
if((node[u].add%2)==1)
{
node[u].add=0;
if(node[u].str=='1')
{
node[u].str='0';
return node[u].str;
}
else
{
node[u].str='1';
return node[u].str;
}
}
else
{
node[u].add=0;
return node[u].str;
}
}
if(node[u].add) pushdown(u);
int mid=(node[u].l+node[u].r)/2;
if(r<=mid)
{
return query(l,r,L(u));
}
else if(l>mid)
{
return query(l,r,R(u));
}
} int main()
{
int T,q,i,j,kk=0;;
scanf("%d",&T);
while(T--)
{
getchar();
kk++;
str[0]='3';
scanf("%s",str+1);
int len=strlen(str);
build(1,len-1,1);
scanf("%d",&q);
char ch[5];
printf("Case %d:\n",kk);
for(i=0;i<q;i++)
{
scanf("%s",ch);
if(ch[0]=='I')
{
int x,y;
scanf("%d%d",&x,&y);
update(x,y,1);
}
else
{
int x;
scanf("%d",&x);
printf("%c\n",query(x,x,1));
}
}
}
return 0;
}

树状数组代码:

//time 304 ms
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream> using namespace std; const int Max=100010; int a[Max];
int n; int lowbit(int x)
{
return x&(-x);
} void insert(int x,int d)
{
while(x<=n)
{
a[x]+=d;
x+=lowbit(x);
}
} int query(int x)
{
int res=0;
while(x>0)
{
res+=a[x];
x-=lowbit(x);
}
return res;
} int main()
{
char str[Max],ch[5];
int T,q,kk=0;
scanf("%d",&T);
while(T--)
{
kk++;
memset(a,0,sizeof(a));
str[0]='2';
scanf("%s",str+1);
n=strlen(str)-1;
scanf("%d",&q);
printf("Case %d:\n",kk);
for(int i=0;i<q;i++)
{
scanf("%s",ch);
if(ch[0]=='I')
{
int x,y;
scanf("%d%d",&x,&y);
insert(x,1);
insert(y+1,-1);
}
else
{
int x;
scanf("%d",&x);
int t=query(x);
if(t%2==1)
{
if(str[x]=='1')
{
printf("0\n");
}
else
{
printf("1\n");
}
}
else
{
printf("%c\n",str[x]);
}
}
} }
return 0;
}

Light OJ 1080 - Binary Simulation的更多相关文章

  1. 1080 - Binary Simulation

    1080 - Binary Simulation    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 64 ...

  2. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  3. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  4. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  5. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  6. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  7. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  8. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  9. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

随机推荐

  1. 2015 Multi-University Training Contest 7 hdu 5375 Gray code

    Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. BTrace介绍和生产环境样例

    BTrace latest realese: release-1.2.5.1 BTrace guide(1.2-20101020): http://kenai.com/projects/btrace/ ...

  3. 005推断两个字符串是否是变位词 (keep it up)

    写一个函数推断两个字符串是否是变位词. 变位词(anagrams)指的是组成两个单词的字符同样,但位置不同的单词.比方说, abbcd和abcdb就是一对变位词 这也是简单的题. 我们能够排序然后对照 ...

  4. HDU 4321 Contest 3

    题意:给定a和b,n,让你求b+a, b+2*a, .......b+n*a里面有多少1. 当统计第K位的时候 可以注意到 第 B+T*A 和 B+(T+2^(K+1))*A 位是相同的 那么 第K位 ...

  5. poj2965 The Pilots Brothers&#39; refrigerator(直接计算或枚举Enum+dfs)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem? id=2965 ---- ...

  6. POJ 1765 November Rain

    题目大意: 有一些屋顶,相当于一些线段(不想交). 问每一条线段能够接到多少水,相对较低的屋顶能够接到高屋顶留下的水(如题图所看到的).因为y1!=y2,所以保证屋顶是斜的. 解题思路: 扫描线,由于 ...

  7. ffmpeg在android上输出滑屏问题处理

    ffmpeg部分机器上有花屏的问题 原代码例如以下: while(av_read_frame(formatCtx, &packet)>=0 && !_stop & ...

  8. [linux]shell中,反引號(`)的应用

    反引號位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引號(')位于Enter键的左方的差别. 反引號位 (`)在Linux中起着命令替换的作用. 命令替换是指shell可以将一个命令的标准 ...

  9. BZOJ 3112 [Zjoi2013]防守战线 线性规划

    题意: 简单叙述: 一个长度为n的序列,在每一个点建塔的费用为Ci.有m个区间.每一个区间内至少有Dj个塔.求最小花费. 方法:线性规划 解析: 与上一题相似.相同使用对偶原理解题.解法不再赘述. 代 ...

  10. ie浏览器下get方式获取数据无效问题

    在ie浏览器用get方式获取数据时因为发送得到参数地址都是一样的,所以浏览器会优先从缓存获取数据,而不去服务器请求数据,post由于参数不同所以不会影响. 解决方法: 1.  Internet选项-- ...