- #include <bits/stdc++.h>
- #define C 100003
- using namespace std;
- char Binary[C];
- struct{
- int sum, prop;
- }Tree[C*3];
- int MX;
- void build(int b, int e, int node){
- if(b >= e){
- if(b == e)Tree[node].sum = Binary[b]-'0';
- return;
- }
- int mid = (b+e)/2, child1 = node*2, child2 = child1+1;
- build(b, mid, child1);
- build(mid+1, e, child2);
- Tree[node].sum = Tree[child1].sum + Tree[child2].sum;
- }
- void update(int b, int e, int node, int I, int J){
- if(b > e)return;
- if(b > J || e < I)return;
- if(b >= I && e <= J){
- Tree[node].sum += ((e-b+1)*1);
- Tree[node].prop += 1;
- return;
- }
- int mid = (b+e)/2, child1 = node*2, child2 = child1+1;
- update(b, mid, child1, I, J);
- update(mid+1, e, child2, I, J);
- Tree[node].sum = Tree[child1].sum + Tree[child2].sum + (e-b+1)*Tree[node].prop;
- }
- int query(int b, int e, int node, int I, int carry = 0){
- if(b > e)return 0;
- if(b > I || e < I)return 0;
- if(b == I && e == I)return Tree[node].sum + carry*(e-b+1);
- int mid = (b+e)/2, child1 = node*2, child2 = child1+1;
- int x = query(b, mid, child1, I, carry+Tree[node].prop);
- int y = query(mid+1, e, child2, I, carry+Tree[node].prop);
- return x + y;
- }
- int main(){
- int T, I, J, Q, CS = 0;
- char que[2];
- scanf("%d", &T);
- while(T--){
- scanf("%s", Binary);
- int S = strlen(Binary)-1;
- memset(Tree, 0, sizeof(Tree));
- build(0, S, 1);
- scanf("%d", &Q);
- printf("Case %d:\n", ++CS);
- for(int i = 1; i <= Q; i++){
- scanf("%s", que);
- if(que[0] == 'I'){
- scanf("%d %d", &I, &J);
- update(0, S, 1, I-1, J-1);
- }
- else{
- scanf("%d", &I);
- printf("%d\n", query(0, S, 1, I-1)%2);
- }
- }
- }
- return 0;
- }
Saturday, June 6, 2015
LightOJ - 1080 - Binary Simulation
Subscribe to:
Post Comments (Atom)
ভাইয়া এখানে রেজাল্ট কে 2 দিয়ে মডুলাস কেন করা হয়েছে ?
ReplyDeleteWe need here is wt is the nth digit of given string into changing their values...the answer will b 1 or 0 as it's a binary representation of a number..so if we mod any number by 2 we can get 0 or 1...so you can get it now, I guess.
ReplyDelete