博客
关于我
[bzoj3110][整体二分]K大数查询
阅读量:108 次
发布时间:2019-02-26

本文共 2207 字,大约阅读时间需要 7 分钟。

????????????????????????????????????????????C?????????????????????Fenwick Tree??????? Indexed Tree???????????????

????

  • Fenwick Tree ????????

    • ????????????????????????????????
    • ???????????????????
  • ?????

    • ???????????????????C??????????????????????????????????????C????
  • ????

    import sysdef main():    # ????    input = sys.stdin.read    data = input().split()    idx = 0    N = int(data[idx])    idx += 1    M = int(data[idx])    idx += 1    # Fenwick Tree??    class FenwickTree:        def __init__(self, size):            self.n = size            self.tree = [0] * (self.n + 2)        def update(self, idx, delta):            while idx <= self.n:                self.tree[idx] += delta                idx += idx & -idx        def query(self, idx):            res = 0            while idx > 0:                res += self.tree[idx]                idx -= idx & -idx            return res    ft = FenwickTree(N)    # ??????    for _ in range(M):        opt = int(data[idx])        idx +=1        u = int(data[idx])        idx +=1        v = int(data[idx])        idx +=1        k = int(data[idx])        idx +=1        if opt == 1:            a = u            b = v            c = k            ft.update(a, c)            if b+1 <= N:                ft.update(b+1, -c)        else:            a = u            b = v            c = k            # ????            low = 1            high = 10**18  # ???????1e18            ans = 0            while low <= high:                mid = (low + high) // 2                # ???? [a, b] ? >= mid ???                sum_mid = ft.query(b) - ft.query(a-1)                sum_mid -= ft.query(b) - ft.query(a-1)  # ???????????????????                # ???????                cnt = ft.query(b) - ft.query(a-1)                if cnt >= c:                    ans = mid                    high = mid - 1                else:                    low = mid + 1            print(ans)if __name__ == "__main__":    main()

    ????

  • Fenwick Tree ??

    • ????????????N?????
    • update ??????????????????????
    • query ?????????????1?idx??????
  • ????

    • ??????????
    • ???Fenwick Tree?
    • ???????
      • ??1??????????????????????????????
      • ??2???????????C???????????????????????????????????????
  • ??????????????????O(logN)?????????????

    转载地址:http://cvmu.baihongyu.com/

    你可能感兴趣的文章
    python系列【仅供参考】:python 远程rdp
    查看>>
    python基础
    查看>>
    Python基本语法与变量类型
    查看>>
    python基本数据类型(容器)- tuple list dict set
    查看>>
    python基本工资的调整方案_Python薪资又涨了!这可咋办!
    查看>>
    Python基准测试和性能分析内存管理和垃圾回收
    查看>>
    Python基于RESTful风格的接口自动化测试实战
    查看>>
    python基于pygame实现跨年烟花效果
    查看>>
    python基于flask搭建http服务(四)—— Docker容器化部署
    查看>>
    python基于flask搭建http服务(二)—— 实现Excel上传、数据清洗、入库
    查看>>
    python基于flask搭建http服务(三)—— 使用gunicorn部署项目
    查看>>
    python基于flask搭建http服务(一)—— 实现数据查询和Excel导出
    查看>>
    Python块键盘/鼠标输入
    查看>>
    Python在正确的时区获取当前时间
    查看>>
    python在心理学研究中的应用有哪些_心理学在线研究可用平台简介和应用进展
    查看>>
    python系列【仅供参考】:python tornado 集成redis消息订阅的异步任务之后tornado主程序无法启动,解决方案
    查看>>
    Python在列表理解中使用枚举
    查看>>
    python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
    查看>>
    python在gpu上运行_【python】python开启GPU加速
    查看>>
    Python在def函数中使用for循环
    查看>>