1080 - Binary Simulation

   PDF (English) Statistics Forum
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

思路:线段树;

线段树维护加反了多少次。

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 #include<queue>
 6 #include<math.h>
 7 using namespace std;
 8 char str[100005];
 9 int tree[4*100005];
10 void in(int l,int r,int k,int nn,int mm)
11 {
12     if(l>mm||r<nn)
13     {
14         return ;
15     }
16     else if(l<=nn&&r>=mm)
17     {
18         tree[k]++;
19         tree[k]%=2;
20         return ;
21     }
22     else
23     {
24         tree[2*k+1]+=tree[k];
25         tree[2*k+1]%=2;
26         tree[2*k+2]+=tree[k];
27         tree[2*k+2]%=2;
28         tree[k] = 0;
29         in(l,r,2*k+1,nn,(nn+mm)/2);
30         in(l,r,2*k+2,(nn+mm)/2+1,mm);
31     }
32 }
33 int ask(int l,int r,int k,int nn,int mm)
34 {
35     if(l>mm||r<nn)
36     {
37         return 0;
38     }
39     else if(l<=nn&&r>=mm)
40     {
41         return tree[k];
42     }
43     else
44     {
45         tree[2*k+1]+=tree[k];
46         tree[2*k+1]%=2;
47         tree[2*k+2]+=tree[k];
48         tree[2*k+2]%=2;
49         tree[k] = 0;
50         int nx = ask(l,r,2*k+1,nn,(nn+mm)/2);
51         int ny = ask(l,r,2*k+2,(nn+mm)/2+1,mm);
52         return (nx + ny)%2;
53     }
54 }
55 int main(void)
56 {
57     int T;
58     scanf("%d",&T);
59     int __ca = 0;
60     while(T--)
61     {
62         __ca++;
63         printf("Case %d:\n",__ca);
64         memset(tree,0,sizeof(tree));
65         scanf("%s",str);
66         int n;int l = strlen(str);
67         scanf("%d ",&n);
68         while(n--)
69         {
70             char a[10];
71             int x,y;
72             scanf("%s",a);
73             if(a[0] == ‘I‘)
74             {scanf("%d %d",&x,&y);
75              in(x-1,y-1,0,0,l-1);
76             }
77             else
78             {
79                 int x;
80                 scanf("%d",&x);
81                 int ny = ask(x-1,x-1,0,0,l-1);
82                 printf("%d\n",(str[x-1]-‘0‘+ny)%2);
83             }
84         }
85     }return 0;
86 }
时间: 2024-12-19 20:51:35

1080 - Binary Simulation的相关文章

Light OJ 1080 - Binary Simulation

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1080 1080 - Binary Simulation PDF (English) problem=1080" style="color:rgb(79,107,114)">Statistics problem=1080" style="color:rgb(79,107,114)">Forum Time Limit:

Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)

Description 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 signif

2017-4-10:CQUPT训练题

A - Binary Simulation(BIT || SegmentTree) 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

An example of how to do a simulation by LAMMPS

The polymer chain of 100 atoms was specially prepared in MATLAB. The atom's Z coordinate does not varies much, all of them are within 2 ?. The distance between atoms is about 1.5 ?. Basically, the chain goes from left upper to right lower corner of t

(转)深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0

深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0 发表于2016年07月15号由52nlp 接上文<深度学习主机攒机小记>,这台GTX1080主机准备好之后,就是配置深度学习环境了,这里选择了比较熟悉Ubuntu系统,不过是最新的16.04版本,另外在Nvidia GTX1080的基础上安装相关GPU驱动,外加CUDA8.0,因为都比较新,所以踩了很多坑. 1. 安装Ubuntu16.04 不考虑双系统,直接安装 Ubuntu16.04,从ubun

深度学习服务器环境配置: Ubuntu17.04+Nvidia GTX 1080+CUDA 9.0+cuDNN 7.0+TensorFlow 1.3

本文来源地址:http://www.52nlp.cn/tag/cuda-9-0 一年前,我配置了一套"深度学习服务器",并且写过两篇关于深度学习服务器环境配置的文章:<深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0> 和 <深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow> , 获得了很多关注和引用. 这一年来,深度学习的大潮继续,特别是前段时间,吴恩达(And

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh