bzoj1267 3784
双倍经验题
像这种方案太多不能全部求出来但求前k大一般有这样一个思路
将所有方案无重复不漏的分为若干类,每个类的元素满足单调性,然后我们用堆维护就行了!
对于这道题,可以想到用树的分治来处理路径,当处理根为x时
我们处理当前的子树每个点i和之前的子树上的组成的路径,
我们完全可以把访问顺序状态记录成一条链(状态是nlogn规模不会爆)
当前子树的每个点i在链上都有一段连续的可选区间,这个区间的点和i构成一条过根x的路径
这样链上的每个点就代表了一类路径
很明显,我们就可以noi超级钢琴的做法来解决,st表预处理+堆维护
type way=record
po,num,next:longint;
end;
node=record
l,r,s,t:longint;
end; var e:array[..] of way;
h:array[..] of node;
w:array[..,..] of longint;
d:array[..] of longint;
a:array[..] of longint;
q:array[..,..] of longint;
f,p,s:array[..] of longint;
v:array[..] of boolean;
l,r,root,len,t,tot,i,n,k,x,y,z,j:longint;
m:node; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function mx(x,y:longint):longint;
begin
if a[x]>a[y] then exit(x) else exit(y);
end; function ask(x,y:longint):longint;
var k:longint;
begin
k:=trunc(ln(y-x+)/ln());
exit(mx(w[x,k],w[y-d[k]+,k]));
end; procedure swap(var a,b:node);
var c:node;
begin
c:=a;
a:=b;
b:=c;
end; procedure add(x,y,z:longint);
begin
inc(len);
e[len].po:=y;
e[len].num:=z;
e[len].next:=p[x];
p[x]:=len;
end; procedure up(i:longint);
var j:longint;
begin
j:=i shr ;
while j> do
begin
if a[h[j].s]+a[h[j].t]<a[h[i].s]+a[h[i].t] then
begin
swap(h[i],h[j]);
i:=j;
j:=j shr ;
end
else break;
end;
end; procedure sift(i:longint);
var j:longint;
begin
j:=i shl ;
while j<=t do
begin
if (j<t) and (a[h[j+].s]+a[h[j+].t]>a[h[j].s]+a[h[j].t]) then inc(j);
if a[h[j].s]+a[h[j].t]>a[h[i].s]+a[h[i].t] then
begin
swap(h[i],h[j]);
i:=j;
j:=j shl ;
end
else break;
end;
end; procedure getroot(x,fa:longint);
var i,y:longint;
begin
s[x]:=;
f[x]:=;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if (y<>fa) and not v[y] then
begin
getroot(y,x);
s[x]:=s[x]+s[y];
f[x]:=max(f[x],s[y]);
end;
i:=e[i].next;
end;
f[x]:=max(f[x],tot-s[x]);
if f[x]<f[root] then root:=x;
end; procedure get(x,fa,len:longint);
var i,y:longint;
begin
inc(t);
a[t]:=len;
q[t,]:=l; q[t,]:=r;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if (y<>fa) and not v[y] then get(y,x,len+e[i].num);
i:=e[i].next;
end;
end; procedure work(x:longint);
var i,y:longint;
begin
v[x]:=true;
i:=p[x];
inc(t);
l:=t;
r:=t;
q[t,]:=l; q[t,]:=r;
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
r:=t;
get(y,x,e[i].num);
end;
i:=e[i].next;
end;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
root:=;
tot:=s[y];
getroot(y,);
work(root);
end;
i:=e[i].next;
end;
end; begin
readln(n,k);
for i:= to n- do
begin
readln(x,y,z);
add(x,y,z);
add(y,x,z);
end;
f[]:=;
tot:=n;
getroot(,);
work(root);
tot:=trunc(ln(t)/ln());
d[]:=;
for i:= to tot do
d[i]:=d[i-]*; for i:= to t do
w[i,]:=i;
for j:= to tot do
for i:= to t do
if i+d[j]-<=t then
w[i,j]:=mx(w[i,j-],w[i+d[j-],j-])
else break; tot:=t; t:=;
for i:= to tot do
begin
inc(t);
with h[t] do
begin
l:=q[i,]; r:=q[i,];
s:=i;
t:=ask(l,r);
end;
up(t);
end;
for i:= to k do
begin
writeln(a[h[].s]+a[h[].t]);
swap(h[],h[t]);
m:=h[t];
dec(t);
sift();
if m.t>m.l then
begin
inc(t);
with h[t] do
begin
l:=m.l; r:=m.t-;
s:=m.s;
t:=ask(l,r);
end;
up(t);
end;
if m.t<m.r then
begin
inc(t);
with h[t] do
begin
l:=m.t+; r:=m.r;
s:=m.s;
t:=ask(l,r);
end;
up(t);
end;
end;
end.
bzoj1267 3784的更多相关文章
- BZOJ 3784: 树上的路径
Description 问一棵树上前 \(k\) 大路径的边权. Sol 边分治. 非常感谢数据没有菊花图. 为了写写边分治试试然后就开了这道题. 边分治非常好想,选一条重边,分成两部分,然后分别求最 ...
- bzoj 3784: 树上的路径 堆维护第k大
3784: 树上的路径 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 88 Solved: 27[Submit][Status][Discuss] ...
- POJ 3784.Running Median
2015-07-16 问题简述: 动态求取中位数的问题,输入一串数字,每输入第奇数个数时求取这些数的中位数. 原题链接:http://poj.org/problem?id=3784 解题思路: 求取中 ...
- bzoj 3784
第三道点分治. 首先找到黄学长的题解,他叫我参考XXX的题解,但已经没有了,然后找到另一个博客的简略题解,没看懂,最后看了一个晚上黄学长代码,写出来然后,写暴力都拍了小数据,但居然超时,....然后改 ...
- HDU 3784 继续xxx定律 & HDU 2578 Dating with girls(1)
HDU 3784 继续xxx定律 HDU 2578 Dating with girls(1) 做3748之前要先做xxx定律 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ ...
- 【POJ 3784】 Running Median
[题目链接] http://poj.org/problem?id=3784 [算法] 对顶堆算法 要求动态维护中位数,我们可以将1-M/2(向下取整)小的数放在大根堆中,M/2+1-M小的数放在小根堆 ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
随机推荐
- JavaScript基础-面向对象编程<1>
1.1 函数与对象 1.定义函数的方式定义类 定义类的方法: function class1(){ //类成员的定义及构造函数部分 } class1既是一个函数,也是一个类. 使用 new 操作符获 ...
- Struts1运行原理以及整合步骤
Struts1 struts1运行步骤 1.项目初始化:项目启动时加载web.xml,struts1的总控制器ActionServlet是一个Servlet,它在web.xml中是配置成自动启动的S ...
- 15_CXF和Spring开发手机号查询网站
[整体分析] [生成客户端代码] wsdl网址: http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx 生成的客户端代码 [工程截图(已拷入客户端 ...
- 代码静态分析工具PCLint, Splint
一.PCLint REFER: 代码静态检查工具PC-Lint运用实践 二.Splint 1.在PC-Linux上安装 ①make error undefined reference toyywrap ...
- 在Window IIS中安装运行node.js应用—你疯了吗
[原文发表地址]Installing and Running node.js applications within IIS on Windows - Are you mad? [原文发表时间]201 ...
- crontab环境变量问题
今天设置linux定时任务时,python内调用的shell指令总执行失败,单独调用python脚本则无问题,考虑到是环境变量未生效引起. 故在执行crontab -e编辑配置文件时,将shell内执 ...
- PHP联合sqlserver2008使用的全过程 ( 原创 亲测)
一.环境 php5.2.5 sqlserver2008 win7 二.配置PHP 1.打开php.in将extension=php_mssql.dll的注释符号去掉. 2.打开php.in将mssql ...
- 从Python传递JSON到JavaScript
OS: Windows 8.1 with update 关键字:Python 3.4,HTML5,JSON,JavaScript 1.LocalServer.py,启动server,打开网页,传递JS ...
- 2014年度辛星css教程夏季版第三节
第二节我们讲述的几乎全是CSS的选择器,那么下面这一节我们来讲一下CSS的颜色和文本的一些东西,虽然我对调色不大敏感,但是对于颜色还是比较感兴趣的. *********CSS中的颜色********* ...
- 转载:PCB名詞解釋:通孔、盲孔、埋孔
在[電子製造業]打滾多年,分享 SMT.焊錫.塑膠射出.產品設計.瓦楞包裝…等經驗.請注意文章內容不見得都正確,服用前請三思… 之前有網友提醒我有篇文章把PCB的盲孔(Blind hole).埋孔(B ...