码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. }
  • 相关阅读:
    HTTP 网络协议请求的消息结构,具体详解(2024-04-25)
    架构师核心-云计算&云上实战(云计算基础、云服务器ECS、云设施实战、云上高并发Web架构)
    疫苗预约系统,疫苗接种管理系统,疫苗预约管理系统毕设作品
    python基于PHP+MyQL的科研实验室管理系统
    代碼隨想錄算法訓練營|第四十一天|第九章 动态规划 理论基础 、509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯。刷题心得(c++)
    你绝对不知道的接口加密解决方案:Python的各种加密实现
    东方博易OJ——1005 - 【入门】已知一个圆的半径,求解该圆的面积和周长
    2022-08-11 TCP/IP协议
    大学生抗疫逆行者网页作业 感动人物HTML网页代码成品 最美逆行者dreamweaver网页模板 致敬疫情感动人物网页设计制作
    luckysheet的使用——13.开启表格非编辑模式(指定单元格可编辑,除此以外其他单元格全部不可编辑)
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/133415229
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号