分组赛的题……madan原题,考试想不出来真是SB得不行

首先,从大往小加边,每次加边如果成环必然弹出环上最大边

考虑询问[x,y],如果边权在[x,y]的边弹出了小于等于y的边j,说明j不在最小生成树上

其余边权在[x,y]的边都在最小生成树上

因此我们每次只要询问,在一段区间内边权小于等于y的边权和是多少,显然用主席树维护

处理弹出的最大边可以暴力,因为n不大

但我写了lct还是t真是简直了……

 const inf=;
type way=record
x,y,z:longint;
end;
node=record
l,r,s:longint;
end; var tree:array[..*] of node;
a:array[..] of way;
h,st,w,f,c,fa,mx:array[..] of longint;
son:array[..,..] of longint;
rev:array[..] of boolean;
ans,j,len,wh,p,i,t,tt,n,m,q,x,y:longint; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure sort(l,r:longint);
var i,j,x:longint;
y:way;
begin
i:=l;
j:=r;
x:=a[(l+r) shr ].z;
repeat
while a[i].z>x do inc(i);
while x>a[j].z do dec(j);
if not(i>j) then
begin
y:=a[i]; a[i]:=a[j]; a[j]:=y;
inc(i);
dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function getf(x:longint):longint;
begin
if f[x]<>x then f[x]:=getf(f[x]);
exit(f[x]);
end; function root(x:longint):boolean;
begin
exit((son[fa[x],]<>x) and (son[fa[x],]<>x));
end; procedure update(x:longint);
var l,r:longint;
begin
l:=son[x,]; r:=son[x,];
mx[x]:=x;
if w[mx[l]]>w[mx[x]] then mx[x]:=mx[l];
if w[mx[r]]>w[mx[x]] then mx[x]:=mx[r];
end; procedure rotate(x,w:longint);
var y:longint;
begin
y:=fa[x];
if not root(y) then
begin
if son[fa[y],]=y then son[fa[y],]:=x
else son[fa[y],]:=x;
end;
fa[x]:=fa[y];
son[y,-w]:=son[x,w];
if son[x,w]<> then fa[son[x,w]]:=y;
son[x,w]:=y;
fa[y]:=x;
update(y);
end; procedure push(x:longint);
begin
if rev[x] then
begin
rev[son[x,]]:=not rev[son[x,]];
rev[son[x,]]:=not rev[son[x,]];
swap(son[x,],son[x,]);
rev[x]:=false;
end;
end; procedure splay(x:longint);
var i,y,t:longint;
fl:boolean;
begin
i:=x;
t:=;
while not root(i) do
begin
inc(t);
st[t]:=i;
i:=fa[i];
end;
inc(t);
st[t]:=i;
for i:=t downto do
push(st[i]);
if t= then exit;
fl:=true;
while fl do
begin
y:=fa[x];
if y=st[t] then
begin
if son[y,]=x then rotate(x,)
else rotate(x,);
fl:=false;
end
else begin
if fa[y]=st[t] then fl:=false;
if son[fa[y],]=y then
begin
if son[y,]=x then rotate(y,)
else rotate(x,);
rotate(x,);
end
else begin
if son[y,]=x then rotate(x,)
else rotate(y,);
rotate(x,);
end;
end;
end;
update(x);
end; procedure access(x:longint);
var y:longint;
begin
y:=;
repeat
splay(x);
son[x,]:=y;
update(x);
y:=x;
x:=fa[x];
until x=;
end; procedure makeroot(x:longint);
begin
access(x);
splay(x);
rev[x]:=not rev[x];
end; procedure link(x,y:longint);
begin
makeroot(x);
fa[x]:=y;
end; procedure cut(x,y:longint);
begin
makeroot(x);
access(y);
splay(y);
son[y,]:=; fa[x]:=;
end; function get(x,y:longint):longint;
begin
makeroot(x);
access(y);
splay(y);
exit(mx[y]);
end; function build(l,r:longint):longint;
var m,q:longint;
begin
inc(t); q:=t;
tree[q].s:=;
if l<>r then
begin
m:=(l+r) shr ;
tree[q].l:=build(l,m);
tree[q].r:=build(m+,r);
end;
exit(q);
end; function add(l,r,last,x,y:longint):longint;
var m,q:longint;
begin
inc(t); q:=t;
if l=r then tree[q].s:=tree[last].s+c[x]*y
else begin
m:=(l+r) shr ;
if x<=m then
begin
tree[q].r:=tree[last].r;
tree[q].l:=add(l,m,tree[last].l,x,y);
end
else begin
tree[q].l:=tree[last].l;
tree[q].r:=add(m+,r,tree[last].r,x,y);
end;
tree[q].s:=tree[tree[q].l].s+tree[tree[q].r].s;
end;
exit(q);
end; function ask(l,r,x,y,k:longint):longint;
var m:longint;
begin
if l=r then
begin
if c[l]<=k then exit(tree[y].s-tree[x].s)
else exit();
end
else begin
m:=(l+r) shr ;
if k<=c[m] then exit(ask(l,m,tree[x].l,tree[y].l,k))
else exit(tree[tree[y].l].s-tree[tree[x].l].s+ask(m+,r,tree[x].r,tree[y].r,k));
end;
end; function find1(l,r,x:longint):longint;
var m:longint;
begin
find1:=r+;
while l<=r do
begin
m:=(l+r) shr ;
if (c[a[m-].z]>x) and (c[a[m].z]<=x) then exit(m);
if c[a[m].z]<=x then r:=m- else l:=m+;
end;
end; function find2(l,r,x:longint):longint;
var m:longint;
begin
find2:=l-;
while l<=r do
begin
m:=(l+r) shr ;
if (c[a[m].z]>=x) and (c[a[m+].z]<x) then exit(m);
if c[a[m].z]>=x then l:=m+ else r:=m-;
end;
end; begin
readln(tt);
while tt> do
begin
dec(tt);
readln(n,m);
for i:= to n+m do
begin
f[i]:=i;
fa[i]:=; son[i,]:=; son[i,]:=;
w[i]:=; rev[i]:=false;
end;
for i:= to m do
readln(a[i].x,a[i].y,a[i].z);
sort(,m);
for i:= to m do
w[n+i]:=a[i].z;
p:=;
c[]:=a[m].z;
a[m].z:=;
for i:=m- downto do
begin
if a[i].z<>c[p] then
begin
inc(p);
c[p]:=a[i].z;
end;
a[i].z:=p;
end;
t:=;
h[]:=build(,p);
for i:= to m do
begin
x:=getf(a[i].x);
y:=getf(a[i].y);
h[i]:=h[i-];
if x<>y then f[x]:=y
else begin
wh:=get(a[i].x,a[i].y);
h[i]:=add(,p,h[i],a[wh-n].z,-);
cut(a[wh-n].x,wh);
cut(a[wh-n].y,wh);
end;
h[i]:=add(,p,h[i],a[i].z,);
link(a[i].x,n+i);
link(a[i].y,n+i);
end;
c[p+]:=inf;
a[].z:=p+;
a[m+].z:=;
c[]:=-;
readln(q);
ans:=;
for i:= to q do
begin
readln(x,y);
x:=x-ans;
y:=y-ans;
len:=y;
if x>y then ans:=
else begin
x:=find2(,m,x);
y:=find1(,m,y);
ans:=ask(,p,h[y-],h[x],len);
end;
writeln(ans);
end;
end;
end.

bzoj4046的更多相关文章

  1. BZOJ4046 [Cerc2014] Pork barre

    我们把边按权值从大到小依次加入图中 如果加到边权$V$,则当前的最小生成森林中边权$v\in[V, V']$(其中$V'$是任意值)形成的森林的边权和就是对于询问$[V, V']$的答案 由于点数不多 ...

随机推荐

  1. Java 8 VM GC Tuning Guide Charter3-4

    第三章 Generations One strength of the Java SE platform is that it shields the developer from the compl ...

  2. 在云服务器搭建WordPress博客(二)使用xampp并解决端口冲突问题

    要搭建一台外界可以访问的服务器,就必须有对应的服务器环境.在这里我用的xampp集成环境(我是菜鸟级......),xampp集成了PHP+Apache+MySQL+perl,安装方便,不用再特意去设 ...

  3. apache + tomcat 集群

    apache2.2与tomcat集成(可以多个tomcat) 需求概况: 有3个服务: localhost:9091, localhost:9190. localhost:9191分别对应3个tomc ...

  4. JS打字效果的动态菜单代码分享

    这篇文章主要介绍了JS打字效果的动态菜单,推荐给大家,有需要的小伙伴可以参考下. 这是一款基于javascript实现的打字效果的动态菜单特效代码,分享给大家学习学习. 小提示:浏览器中如果不能正常运 ...

  5. POJ 3553 Task schedule

    原题链接:http://poj.org/problem?id=3553 这道题主要就是贪心思想吧,对于每个job,根据其截止时间 dj 从小到大排序,我们必须要尽快把dj最小的job完成掉,这样才能使 ...

  6. 剑指offer--面试题8

    题目:求旋转数组中的最小数字 以下为自己所写代码: #include "stdafx.h" #include <iostream> #include <excep ...

  7. 解决java写入xml报错org.w3c.dom.DOMException:DOM002 Illeg

    Exception is -- > org.w3c.dom.DOMException: DOM002 Illegal character 字符不被允许 org.w3c.dom.DOMExcept ...

  8. ASP.NET WebAPI2 发布之后404 Note Found

    方法一:首先确保服务器安装.Net FrameWork 4.0 并且注册IIS 如果先安装.net framework4.0,再安装IIS,则会出现4.0的ISAPI没有注册的情况. 运行:C:\Wi ...

  9. 管理Java垃圾回收的五个建议

    [编者按]本文作者是Niv Steingarten,是Takipi 的联合创始人,热衷于编写优雅简洁的代码.作者通过对垃圾收集器的介绍和梳理,在管理垃圾回收方面提出了五个建议,降低收集器开销,帮助大家 ...

  10. VS2010 MFC DataGrid绑定实例

    VS2010环境下MFC使用DataGrid绑定数据源 参考:http://blog.csdn.net/fddqfddq/article/details/7874706 详细介绍如何在MFC中使用Da ...