博客
关于我
codeforces 543E. Listening to Music
阅读量:254 次
发布时间:2019-03-01

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

线段树的每个节点需要存储四个值:ls、rs、min、tag。由于内存空间有限,这些值被压缩到一个unsinged long long中。具体来说,t[x] = (ls * N + rs) * T + val + tag。通过t[x] % T,可以得到val + tag的值。此外,ls = t[x] / T / N,rs = t[x] / T % N。经过标记和永久化处理后,可以通过左右子节点的值来解出自己的val,然后再解出tag。这种压缩方式极大地节省了内存空间。

为了实现这一压缩,将代码进行了极大的优化。例如,使用递归更新和查询函数,通过递归分割区间并合并子节点的信息。代码结构如下:

#include 
#include
#include
#include
#include
#include
using namespace std;vector
vec(200010);struct trnode { int lc, rc, c, u; tr[3800010]; int tot = 0, root[200010]; void update(int &x, int l, int r, int fl, int fr, int c) { tr[++tot] = tr[x]; x = tot; if (l == fl && r == fr) { tr[x].c += c; tr[x].u += c; return; } int mid = (l + r) / 2; if (fr <= mid) { update(tr[x].lc, l, mid, fl, fr, c); } else if (fl > mid) { update(tr[x].rc, mid + 1, r, fl, fr, c); } else { update(tr[x].lc, l, mid, fl, mid, c); update(tr[x].rc, mid + 1, r, mid + 1, fr, c); tr[x].c = min(tr[tr[x].lc].c, tr[tr[x].rc].c) + tr[x].u; } } int findans(int x, int l, int r, int fl, int fr) { if (!x) return 0; if (l == fl && r == fr) return tr[x].c; int mid = (l + r) / 2; if (fr <= mid) { return findans(tr[x].lc, l, mid, fl, fr) + tr[x].u; } else if (fl > mid) { return findans(tr[x].rc, mid + 1, r, fl, fr) + tr[x].u; } else { return min(findans(tr[x].lc, l, mid, fl, mid), findans(tr[x].rc, mid + 1, r, mid + 1, fr)) + tr[x].u; } } int n, m, cnt = 0, b[200010]; struct node { int a, num; }; bool cmp(node a, node b) { return a.a < b.a; } a[200010];}

该代码使用递归更新和查询方法,通过递归分割区间并合并子节点的信息,实现了线段树的高效操作。代码中定义了tr数组存储线段树的节点,使用递归函数update和findans分别进行区间更新和查询操作。通过这种方法,可以高效地处理区间查询和更新问题。

转载地址:http://kmza.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在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
查看>>
python在gpu上运行_【python】python开启GPU加速
查看>>
Python在def函数中使用for循环
查看>>
Python在def函数中使用for循环
查看>>
Python在Conda环境中,但在Windows虚拟环境中没有激活
查看>>
python系列【仅供参考】:python pip 错误 ModuleNotFoundError: No module named pip._internal 解决办法
查看>>