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

你可能感兴趣的文章
python3.6使用pickle序列化class
查看>>
python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
查看>>
Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
查看>>
python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
查看>>
Python3.7安装cv2踩过的坑
查看>>
python写txt文件
查看>>
python关于sorted排序函数详细解析 附实战代码(全)
查看>>
python3.8调用m文件,调用matlab
查看>>
python3.9新特性_Python 3.9 新特性速览
查看>>
Python3.x:遍历select下拉框获取value值
查看>>
python360百科_11月编程排行榜:Python超过Java成第二
查看>>
Python关于Paginator分页函数详细解析 附实战代码
查看>>
python3、ipython3、setup-tools、pip等环境搭建详细总结
查看>>
python3下使用cv2.imwrite存储带有中文路径图片或者绝对路径图片
查看>>
python3与django中@property详解
查看>>
python3中如何表示无穷大
查看>>
Python3中提示:no module named 'wxpy'
查看>>
python3中选择文件对话框的格式打开和保存图片
查看>>
python3为什么print永远都是地址_Python3 print 函数用法总结
查看>>
Python3之uuid模块
查看>>