博客
关于我
[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/

    你可能感兴趣的文章
    Vue3+element plus+sortablejs实现table列表拖拽
    查看>>
    Nokia5233手机和我装的几个symbian V5手机软件
    查看>>
    non linear processor
    查看>>
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>