博客
关于我
[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_IDE开发环境使用_建立Python源文件---python工作笔记011
    查看>>
    Python_web框架解析
    查看>>
    Python__Enter__/__Exit__vs__init__(或__new__)/__del__
    查看>>
    python_对象的基本组成和内存示意图---python工作笔记016
    查看>>
    python_开发环境介绍_交互模式使用_IDE介绍和使用---python工作笔记010
    查看>>
    python_引用的本质_栈内存和堆内存_内存示意图---python工作笔记017
    查看>>
    python_标识符_帮助系统的使用_命名规则---python工作笔记018
    查看>>
    python_海龟绘图_坐标系问题_画笔各种方法---python工作笔记013
    查看>>
    python_海龟绘图_画出奥运五环图---python工作笔记014
    查看>>
    Python_生产者消费者模型、管道、数据共享、进程池
    查看>>
    python_程序格式_缩进_行注释_段注释---python工作笔记012
    查看>>
    python_程序的构成---python工作笔记015
    查看>>
    python_超文本传输协议
    查看>>
    Python_集合
    查看>>
    Python|AsyncIO|TypeError:应为协程
    查看>>
    python——random模块的应用
    查看>>
    Python——基础知识细节
    查看>>
    Python——正则表达式
    查看>>
    Python—面向对象2
    查看>>
    Python“和“带整数的运算符
    查看>>