博客
关于我
[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下调用Linux的Shell命令
    查看>>
    Python接口自动化测试实战系列
    查看>>
    python下载_安装_配置_以及第一行python程序---python工作笔记009
    查看>>
    Python不协调机器人消息.内容
    查看>>
    Python与C#之间内存共享互传信息
    查看>>
    python与c语言混合编程_你试过C语言和Python一起混合编程吗?两者相加不是已经无敌了!...
    查看>>
    python与excel
    查看>>
    Python与Excel的完美邂逅:深入浅出,手把手教你轻松玩转Excel数据操作
    查看>>
    python与flask_Python编程中flask的简介与简单使用
    查看>>
    python与图灵机器人交互(WXPY版本)
    查看>>
    python与运算符_python运算符
    查看>>
    python两个列表获取交集,并集,差集
    查看>>
    python两个字典合并,两个list合并
    查看>>
    Python两个版本共存时,命令行升级pip
    查看>>
    Python两个版本共存时,怎么验证是否安装成功 scrapy
    查看>>
    Python两个版本共存时,通过pip安装Scrapy
    查看>>
    python两组变量的相关关系散点图_变量是什么?变量间的两种关系——函数关系和相关关系(小学版)...
    查看>>
    Python中 pass语句的作用
    查看>>
    python中 urllib, urllib2, httplib, httplib2 几个库的区别
    查看>>
    python中 yaml库 & json库 & xmltodict库使用
    查看>>