博客
关于我
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-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>
python出现 unknown encoding: idna 的处理方法
查看>>
Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
查看>>
Python-numpy(2)数组的操作
查看>>
Python-Numpy的tile函数用法
查看>>
python-open函数
查看>>
Python-Pandas-将特定函数应用于给定级别-多索引数据框架
查看>>
python-queue队列通信
查看>>
python-ros No module named PyKDL
查看>>
python-social-auth 是否需要会话
查看>>
Python再获年度编程语言,微软或成最大赢家
查看>>
python-week5
查看>>
Python写表情包搜索工具(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>
Python-【1】配置
查看>>
Python-从子文件夹执行绝对导入
查看>>
Python-刷新页面,直到元素可单击
查看>>