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.
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 }
1080 - Binary Simulation的更多相关文章
- Light OJ 1080 - Binary Simulation
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1080 1080 - Binary Simulation PDF (Englis ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- (转)深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0
深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0 发表于2016年07月15号由52nlp 接上文<深度学习主机攒机小记>,这台GTX10 ...
- 【转】What's the difference between simulation and emulation
摘要:这2个单词 还是用英文解释,比较准确.按我的理解:simulation就是模拟,可以做些改变. emulation是仿真,是按照原来的样子进行部署,不可以改变. Yes, the concept ...
- ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)
致谢源代码网址:https://github.com/Tutorgaming/kamtoa-simulation kamtoa simulation学习与示例分析(一) 源码学习与分析是学习ROS,包 ...
- 深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow
接上文<深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0>,我们继续来安装 TensorFlow,使其支持GeForce GTX 1080显卡 ...
- 基于Ubuntu16.04的GeForce GTX 1080驱动安装,遇到的问题及对应的解决方法
1.在主机上插上GPU之后,查看设备: $ nvidia-smi Tue Dec :: +------------------------------------------------------- ...
- 算法学习笔记之——priority queue、heapsort、symbol table、binary search trees
Priority Queue 类似一个Queue,但是按照priority的大小顺序来出队 一般存在两种方式来实施 排序法(ordered),在元素入队时即进行排序,这样插入操作为O(N),但出队为O ...
- Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)
Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...
随机推荐
- 31-Longest Common Prefix
Longest Common Prefix My Submissions Difficulty: Easy Write a function to find the longest common pr ...
- 26-Palindrome Number
回文数的判定,不使用额外的空间 Determine whether an integer is a palindrome. Do this without extra space. 思路:将一个整数逆 ...
- day09搭建均衡负载和搭建BBS博客系统
day09搭建均衡负载和搭建BBS博客系统 搭建BBS博客系统 本次搭建bbs用到的技术 需要用到的: 1.Nginx+Django 2.Django+MySQL 环境准备 主机 IP 身份 db01 ...
- 文件和目录之间建立链接 (ln)
- Hbase(二)【shell操作】
目录 一.基础操作 1.进入shell命令行 2.帮助查看命令 二.命名空间操作 1.创建namespace 2.查看namespace 3.删除命名空间 三.表操作 1.查看所有表 2.创建表 3. ...
- 【c++】解析多文件编程的原理
其实一直搞不懂为什么头文件和其他cpp文件之间的关系,今晚索性一下整明白 [c++]解析多文件编程的原理 a.cpp #include<stdio.h> int main(){ a(); ...
- Java中方法的定义与使用
Java中方法的定义与使用 1.方法的定义: 方法是一段可以被重复调用的代码块. 方法的声明: public static 方法返回值 方法名([参数类型 变量--]){ 方法代码体: return ...
- vue-cli4脚手架搭建一
涉及内容 html css javascript node.js npm webpack 2.9.6是常用版本 vue-cli4是基于webpack的 webpack是基于node ...
- 【JAVA】【JVM】内存结构
虽然jvm帮我们做了内存管理的工作,但是我们仍需要了解jvm到底做了什么,下面我们就一起去看一看 jvm启动时进行一系列的工作,其中一项就是开辟一块运行时内存.而这一块内存中又分为了五大区域,分别用于 ...
- 什么是JMS规范?
一.简介 JMS是什么:JMS是Java提供的一套技术规范和关于消息中间件的协议 JMS干什么用:通过生产者Producer,消息服务器,以及消费者通力合作,使异构系统能进行集成通信,缓解系统瓶颈,提 ...