简体   繁体   English

查询范围[L,R]

[英]Querying in a range[L,R]

Given a binary string (that is a string consisting of only 0 and 1). 给定一个二进制字符串(即仅包含0和1的字符串)。 They were supposed to perform two types of query on the string. 他们应该对字符串执行两种类型的查询。 Problem 问题

Type 0: Given two indices l and r.Print the value of the binary string from l to r modulo 3. 类型0:给定两个索引l和r,将二进制字符串的值从l到r以模3进行打印。

Type 1: Given an index l flip the value of that index if and only if the value at that index is 0. 类型1:给定索引l当且仅当该索引的值为0时,才翻转该索引的值。

I am trying to solve this using BIT . 我正在尝试使用BIT解决此问题。

If the number in range [l,r] is even then: 如果范围[l,r]中的数字为偶数
if the sum of the numbers of one is even then the answer is 0 else 2 如果一个数字的总和为偶数,则答案为0,否则为2

If the number in range [l,r] is odd 如果范围[l,r]中的数字为奇数
if the sum of the numbers of one is even then the answer is 0 else 1 如果一个数字的总和为偶数,则答案为0,否则为1

But I am getting wrong answer for some test cases what wrong is in my approach. 但是对于某些测试用例,我的答案是错误的。

public static void update(int i){

    while(A.length>i){
        A[i]+=1;
        i+=i&-i;
    }

}

public static int ans(int i){
    int a=0;

    while(i>0){
        a+=A[i];
        i-=i&-i;
    }
    return a;
}

Answer for each Query. 回答每个查询。

while(Q>0){
    Q--;
    int x = in.nextInt();
    int l = in.nextInt()+1;
    if(x==1){
        if((ans(l)-ans(l-1))==0) update(l);

        continue;
    }
    int r  = in.nextInt()+1;

    int f = ans(r) - ans(r-1);

    if(f==0){

        int sum = ans(r)- ans(l-1);
        if(sum%2==0) System.out.println(0);
        else System.out.println(2);
    }else{

        int sum = ans(r)- ans(l-1);
        if(sum%2==0) System.out.println(0);
        else System.out.println(1);

    }
}

Full CODE 完整代码

When building a binary number, from the left digit to the right digit, if you consider only a currently parsed section of the string, it is a binary number. 在构建二进制数时,从左数位到右数位,如果仅考虑字符串的当前解析部分,则它是二进制数。 We'll call it n. 我们称它为n。

When you append a digit to the right of n, this shifts it left, and then also adds the digit (1 or 0). 当您在n的右边附加一个数字时,这会将其向左移动,然后还添加数字(1或0)。 So n0 = 2*n and n1 = 2*n+1 所以n0 = 2 * n和n1 = 2 * n + 1

Because you only care about the number modulo 3, you can just keep track of this modulo 3. 因为您只关心模数3,所以可以跟踪此模数3。

You can note that 你可以注意到

0 (mod 3) * 2     = 0 (mod 3)
0 (mod 3) * 2 + 1 = 1 (mod 3)
1 (mod 3) * 2     = 2 (mod 3)
1 (mod 3) * 2 + 1 = 0 (mod 3)
2 (mod 3) * 2     = 1 (mod 3)
2 (mod 3) * 2 + 1 = 2 (mod 3)

You can construct a simple fsm to represent these relationships and simply use the section of the string which you are interested in as input to it. 您可以构造一个简单的fsm来表示这些关系,而只需使用您感兴趣的字符串部分作为其输入即可。 Or implement it however else you want. 或实施它,但是其他您想要的。

Hopefully you realise that "Given an index l flip the value of that index if and only if the value at that index is 0." 希望您认识到“给定索引l当且仅当该索引的值为0时,才翻转该索引的值。” simply means set the value at l to 1. 简单地表示将l的值设置为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计数子数组的总和在[L,R]范围内 - Counting subarray have sum in range [L, R] 在范围内查找 min(A[L], max(A[L+1], min(A[L+2],...,a[R]))) - Find min(A[L], max(A[L+1], min(A[L+2],...,a[R]))) in range 范围[l,r]中可以置换为回文的子串数 - Number of substrings in range [l, r] that can be permuted to palindrome 在 [l, r](含)范围内,n 和 m 能整除多少个数? - How many numbers are divisible by n and m in the range [l, r](inclusive)? 查找将所有K数转换为[L,R]范围(即L≤x≤R)所需的操作次数的最佳方法 - Optimal way to find number of operation required to convert all K numbers to lie in the range [L,R] (i.e. L≤x≤R) 范围查询:数组A的索引范围[l:r]中存在的大于X且小于Y的最大元素 - Range Query: Maximum element more than X and less than Y present in the index range [l : r] in array A 查询几天和几小时的范围 - Querying for a range of days and hours X的XOR与范围L到R的每个数组元素的总和 - summation of XOR of X with each of the array element from range L to R both 如何最有效地找到L,R范围内的数组中最频繁的数字及其频率? - How to find the most frequent number and its frequency in an array in range L,R most efficiently? 如何在 l 到 r 范围内找到 z 的最小值,使得 (x & z).(y & z) 最大 - how to find the minimum value of z in the range l to r such that (x & z).(y & z) is maximum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM