博客
关于我
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工作笔记014
查看>>
Python_生产者消费者模型、管道、数据共享、进程池
查看>>
python_程序格式_缩进_行注释_段注释---python工作笔记012
查看>>
python_程序的构成---python工作笔记015
查看>>
python_超文本传输协议
查看>>
Python_集合
查看>>
Python|AsyncIO|TypeError:应为协程
查看>>
python——random模块的应用
查看>>
Python——基础知识细节
查看>>
Python——正则表达式
查看>>
Python—面向对象2
查看>>
Python“和“带整数的运算符
查看>>
Python“文件存在“制作目录时出错
查看>>
Python“文件存在“制作目录时出错
查看>>
Python、IMAP 和 GMail.将消息标记为已查看
查看>>
python一个一个获取迭代器的元素
查看>>
python一维数组 提取_python – 如何从numpy中的1维数组中提取零维切片
查看>>
python三大库之--numpy(一)
查看>>
python三大库之--numpy(二)
查看>>
python上的字无效字符_标识中的无效字符
查看>>