• Codeforces Round 900 (Div. 3)--E. Iva & Pav(前缀和+二分)


    Iva and Pav are a famous Serbian competitive programming couple. In Serbia, they call Pav "papuca" and that's why he will make all of Iva's wishes come true.

    Iva gave Pav an array a of n elements.

    Let's define f(l,r)=al & al+1 &…& ar (here && denotes the bitwise AND operation).

    Note that f(l,r)is not defined when l>r.

    Iva also gave Pav q queries.

    Each query consists of 2 numbers, k and l, and she wants Pav to find the largest index r (l≤r≤n), such that f(l,r)≥k.

    Pav wants to solve this problem fast because he doesn't want to upset Iva. He needs your help.

    Input

    The first line contains a single integer t (1≤t≤10^4) — the number of test cases.

    The first line of each test case contains a single integer n (1≤n≤2⋅10^5) — the length of array a.

    The second line of each test case contains n integers a1,a2,…,an (1≤ai≤10^9) — the elements of array a.

    The third line of each test case contains a single integer q (1≤q≤10^5) — the number of queries Iva gave Pav.

    The next q lines of each test case contains two numbers, l and k (1≤l≤n, 1≤k≤10^9) — the left bound for the subsegment, and the integer k described in statement.

    It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5. Also, it is guaranteed that the sum of q over all test cases does not exceed 2⋅10^5.

    Output

    For each query output maximal index r (l≤r≤n) such that al & al+1 &…& ar ≥ k.

    If such r doesn't exist, output −1.

    input

    1. 3
    2. 5
    3. 15 14 17 42 34
    4. 3
    5. 1 7
    6. 2 15
    7. 4 5
    8. 5
    9. 7 5 3 1 7
    10. 4
    11. 1 7
    12. 5 7
    13. 2 3
    14. 2 2
    15. 7
    16. 19 20 15 12 21 7 11
    17. 4
    18. 1 15
    19. 4 4
    20. 7 12
    21. 5 7

    output

    1. 2 -1 5
    2. 1 5 2 2
    3. 2 6 -1 5

    题意:给定n个数,定义f(l,r)=al & al+1 &…& ar,即区间按位与的值,给定q个询问,给定l,k,问最远的r满足f(l,r)>=k,如果不存在输出-1.

    解析:根据按位与的性质可知,全一才为1,否则为0,因此,一段区间内部肯定是单调的,因此可以二分,我们可以记录一下前i个数,二进制是j的个数的前缀和,对于每个询问二分>=k的右边界即可。

    1. #include
    2. using namespace std;
    3. const int N=2e5+5;
    4. int sum[N][35];//前i个数,二进制是j的个数
    5. bool check(int r,int l,int k)
    6. {
    7. int s=0;//总和
    8. for(int i=1;i<=32;i++)
    9. {
    10. if(sum[r][i]-sum[l-1][i]==r-l+1)//全1才是1
    11. {
    12. s+=1<<(i-1);
    13. if(s>=k) return true;//满足直接退出即可
    14. }
    15. }
    16. return false;
    17. }
    18. void solve()
    19. {
    20. int n;
    21. scanf("%d",&n);
    22. for(int i=1;i<=n;i++)
    23. {
    24. int x,cnt=0;
    25. scanf("%d",&x);
    26. while(x)
    27. {
    28. sum[i][++cnt]=x%2;//
    29. x/=2;
    30. }
    31. }
    32. //累加前缀和
    33. for(int i=1;i<=n;i++)
    34. for(int j=1;j<=32;j++) sum[i][j]+=sum[i-1][j];
    35. int q;
    36. scanf("%d",&q);
    37. while(q--)
    38. {
    39. int l,k,ans=-1;
    40. scanf("%d%d",&l,&k);
    41. int L=l,R=n;
    42. //注意下写法,l和L区别
    43. while(L<=R)
    44. {
    45. int mid=L+R>>1;
    46. if(check(mid,l,k)) ans=mid,L=mid+1;
    47. else R=mid-1;
    48. }
    49. printf("%d ",ans);
    50. }
    51. printf("\n");
    52. //初始化清空
    53. for(int i=1;i<=n;i++)
    54. for(int j=1;j<=32;j++) sum[i][j]=0;
    55. }
    56. int main()
    57. {
    58. int t=1;
    59. scanf("%d",&t);
    60. while(t--) solve();
    61. return 0;
    62. }
  • 相关阅读:
    邮箱发送验证码(nodemailer)
    如何在 Tomcat 中为 Web 应用程序启用和配置缓存?
    【数据结构--二叉树】合并二叉树
    HTIML知识概述
    C++11 学习之路
    直流有刷电机调速原理及Matlab/Simulink仿真
    Ubuntu升级自带的Python3版本
    SpringBoot(七) - Redis 缓存
    golang Context应用举例
    Go语言设计与实现 学习笔记 第一章 介绍
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/133415229