博客
关于我
[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 NLP完整项目实战教程(1)
    查看>>
    Python NLP自然语言处理详解
    查看>>
    python nltk nltk_data 离线安装,chatterbot
    查看>>
    Redis 限流的 3 种方式,还有谁不会?
    查看>>
    Python这么慢,为啥大公司还在用?
    查看>>
    python note 06 编码方式
    查看>>
    python numba 转灰度图_使用NumPy、Numba的简单使用(二)
    查看>>
    Python Numpy 关于 linspace()函数 使用详解(全)
    查看>>
    Python numpy插入、读取至postgreSQL数据库中bytea类型字段
    查看>>
    Python numpy数据的保存和读取
    查看>>
    python numpy矩阵索引_python – 在2D numpy ndarray或numpy矩阵中获取前N个值的索引
    查看>>
    Python OCR库:自动化测试验证码识别神器!
    查看>>
    python opencv - 斑点检测或圆形检测
    查看>>
    Python还可以做情感分析你不看看吗?
    查看>>
    Python OpenCV HoughLinesP 无法检测线
    查看>>
    Python OpenCV-使用透明度覆盖图像
    查看>>
    python Opencv图像基础操作
    查看>>
    Python OpenCV将图像转换为字节字符串?
    查看>>
    python OpenCV视频的读取及保存
    查看>>
    python open和file的区别
    查看>>