博客
关于我
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 在包含类似字符\x16、\x12、\x某某的数组中将以\x开头的字符找出来的方法
查看>>
Python 在并行进程之间共享字典
查看>>
Python 垃圾收集器文档
查看>>
python 基于 wordcloud + jieba + matplotlib 生成词云
查看>>
python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
查看>>
Python编程:Tkinter图形界面设计(2)
查看>>
Python 基础 - Day 5 Learning Note - 模块 之 标准库:time (1)
查看>>
Python 基础一
查看>>
python 基础操作知识整理总结
查看>>
Python 基础知识点整理与分享,期盼你的交流!
查看>>
python 基础第七篇
查看>>
Python编程:Tkinter图形界面设计(1)
查看>>
Python 基础语法:None
查看>>
Python 基础语法:基本数据类型(一)
查看>>
python编程读取写入excel_Python读取txt内容写入xls格式excel中的方法
查看>>
Python 基础语法:基本数据类型(字典)
查看>>
Python 基础语法:基本数据类型(字符串)
查看>>
Python 基础语法:基本数据类型(集合)
查看>>
Python编程的终极十大工具
查看>>