又见bzoj的语言歧视,囧……
bzoj3083过了本地的数据在上面出现各种奇葩的TLE
835083 phile 3083 Time_Limit_Exceed 17092 kb 4872 ms Pascal/Edit 4931B 2015-01-11 19:53:32
10s的时限在逗我?
UPD:现在已经没有问题了……
这两道题目还是不错的
首先这道题是很像动态树的题目,我们发现树的形态不发生变化,而只是根在变化
考虑树链剖分,修改显然跟根的变化毫无关系
主要是查询,首先对于子树的查询肯定是dfs序(而轻重链剖分正是dfs序的一种特殊形式)
下面我们想,一次换跟操作对哪些点的查询会产生影响
画图可知,换根只会影响新根的祖先们的查询,如果不是祖先,那直接查询即可
而是祖先的话,设查询点x和新根路径上离x最近的那个点为y
显然,除去在原图上以y为根的子树,其余部分都应该是换根后的x的子树
由此题目得解,这里给出bzoj3083的代码

 const inf=;
type node=record
po,next:longint;
end; var c,e,b,a,d,p,size,top,fa:array[..] of longint;
w:array[..] of node;
tree,lazy:array[..*] of longint;
anc:array[..,..] of longint;
root,ch,i,s,len,n,m,x,y,z:longint; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure push(i:longint);
begin
lazy[i*]:=lazy[i];
lazy[i*+]:=lazy[i];
tree[i*]:=lazy[i];
tree[i*+]:=lazy[i];
lazy[i]:=;
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure add(x,y:longint);
begin
inc(len);
w[len].po:=y;
w[len].next:=p[x];
p[x]:=len;
end; procedure dfs1(x:longint);
var i,y:longint;
begin
for i:= to s do
begin
y:=anc[x,i-];
if anc[y,i-]= then break;
anc[x,i]:=anc[y,i-];
end;
size[x]:=;
i:=p[x];
while i<> do
begin
y:=w[i].po;
if fa[x]<>y then
begin
d[y]:=d[x]+;
fa[y]:=x;
anc[y,]:=x;
dfs1(y);
size[x]:=size[x]+size[y];
end;
i:=w[i].next;
end;
end; procedure dfs2(x:longint);
var q,i,y:longint;
begin
inc(len);
b[len]:=x;
c[x]:=len;
q:=;
i:=p[x];
while i<> do
begin
y:=w[i].po;
if fa[y]=x then
if size[y]>size[q] then q:=y;
i:=w[i].next;
end;
if q<> then
begin
top[q]:=top[x];
dfs2(q);
end;
i:=p[x];
while i<> do
begin
y:=w[i].po;
if (fa[y]=x) and (y<>q) then
begin
top[y]:=y;
dfs2(y);
end;
i:=w[i].next;
end;
e[x]:=len; //【c[x],e[x]】表示了以x根的子树所代表的区间
end; procedure build(i,l,r:longint);
var m:longint;
begin
if l=r then tree[i]:=a[b[l]]
else begin
m:=(l+r) shr ;
build(i*,l,m);
build(i*+,m+,r);
tree[i]:=min(tree[i*],tree[i*+]);
end;
end; procedure change(i,l,r,x,y:longint);
var m:longint;
begin
if (x<=l) and (y>=r) then
begin
lazy[i]:=z;
tree[i]:=z;
end
else begin
if lazy[i]<> then push(i);
m:=(l+r) shr ;
if x<=m then change(i*,l,m,x,y);
if y>m then change(i*+,m+,r,x,y);
tree[i]:=min(tree[i*],tree[i*+]);
end;
end; procedure work(x,y:longint);
var f1,f2:longint;
begin
f1:=top[x];
f2:=top[y];
while f1<>f2 do
begin
if d[f1]>=d[f2] then
begin
change(,,n,c[f1],c[x]);
x:=fa[f1];
end
else begin
change(,,n,c[f2],c[y]);
y:=fa[f2];
end;
f1:=top[x];
f2:=top[y];
end;
if c[x]>c[y] then swap(x,y);
change(,,n,c[x],c[y]);
end; function getans(i,l,r,x,y:longint):longint;
var m,s:longint;
begin
if x>y then exit(inf);
if (x<=l) and (y>=r) then exit(tree[i])
else begin
if lazy[i]<> then push(i);
m:=(l+r) shr ;
s:=inf;
if x<=m then s:=getans(i*,l,m,x,y);
if y>m then s:=min(s,getans(i*+,m+,r,x,y));
exit(s);
end;
end; function check(x,y:longint):boolean;
var i,k:longint;
begin
if d[y]>=d[x] then exit(false);
for i:=s downto do
if d[x]- shl i>=d[y] then x:=anc[x,i];
if x=y then exit(true) else exit(false);
end; function ask(x:longint):longint;
var a,z,i:longint;
begin
if x=root then exit(tree[]);
if not check(root,x) then exit(getans(,,n,c[x],e[x]))
else begin
z:=root;
for i:=s downto do
if d[z]- shl i>d[x] then z:=anc[z,i]; //找最近点
a:=min(getans(,,n,,c[z]-),getans(,,n,e[z]+,n));
exit(a);
end;
end; begin
readln(n,m);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
s:=trunc(ln(n)/ln());
for i:= to n do
read(a[i]);
readln(root);
dfs1(root);
top[root]:=root;
len:=;
dfs2(root);
build(,,n);
for i:= to m do
begin
read(ch);
if ch= then
begin
readln(x);
root:=x;
end
else if ch= then
begin
readln(x,y,z);
work(x,y);
end
else begin
readln(x);
writeln(ask(x));
end;
end;
end.

bzoj3083 3306的更多相关文章

  1. 【BZOJ3083/3306】遥远的国度/树 树链剖分+线段树

    [BZOJ3083]遥远的国度 Description 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了 ...

  2. centos7 打开mysql 3306端口并 设置外部访问

    mysql安装后默认是localhost访问,如果需要外部访问可以设置一个新的账号把host改为%,意味着所有ip均可以访问 grant all privileges on *.* to 'outUs ...

  3. bzoj3083 遥远的国度 && bzoj3626 LCA (树链剖分)

    今早刷了两道树剖的题目,用时两小时十五分钟= = 树剖的题目代码量普遍120+ 其实打熟练之后是很容易调的,不熟练的话代码量大可能会因为某些小细节调很久 3083:裸树剖+"换根" ...

  4. linux下mysql开启远程访问权限及防火墙开放3306端口

    默认mysql的用户是没有远程访问的权限的,因此当程序跟数据库不在同一台服务器上时,我们需要开启mysql的远程访问权限. 主流的有两种方法,改表法和授权法. 相对而言,改表法比较容易一点,个人也是比 ...

  5. centos7开启3306端口,liunx查看防火墙是否开启

    Can't connect to MySQL server on localhost (10061)这个就属于下面要说的情况 启动服务 systemctl start mariadb.service ...

  6. iptables 开启3306端口

    [root@mysqld ~]# mysql -uroot -h 192.168.1.35 -p Enter password: ERROR 1130 (HY000): Host '192.168.1 ...

  7. ping通IP,telnet 3306不通

    一个同事装的MySQL数据库,无法连接.​​1.查看权限​​2.查看防火墙​​检查用户权限,防火墙都没问题,就是无法连接,能ping通,但是telnet 3306 端口无法成功.​​检查了下数据库配置 ...

  8. Linux配置防火墙,开启80端口、3306端口(转)

    vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火 ...

  9. 1045 | error connecting to master 'slave_user@192.168.0.75:3306' - retry-time: 6

    mysql 主从复制问题整理   问题:      1045 | error connecting to master 'slave_user@192.168.0.75:3306' - retry-t ...

随机推荐

  1. Servie学习总结

    一.什么是Service Service是一个应用程序组件,它是安卓实现程序后台运行的一个解决方案. 二.分类 服务有两种类别started.bound.但是一个服务类所要继承的类是一样的,都是Ser ...

  2. 黑马程序员-IO(二)

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 装饰设计模式: 当想要对已有对象进行功能增强时.可以定义类,将已经有的类传入,基于已经有的功能, ...

  3. CodeSmith中SchemaExplorer属性的介绍

    CodeSmith与数据库的联系,在CodeSmith中自带一个程序集SchemaExplorer.dll,这个程序集中的类主要用于获取数据库中各种对象的结构. <%@ Property Nam ...

  4. Ajax结合Js操作灵活操作表格

    Table页面: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...

  5. Ubuntu常用终端快捷键

    CTRL+k:删除从光标到行尾的部分 CTRL+u:删除从光标到行首的部分 ALT+d:删除从光标到当前单词结尾的部分 CTRL+w:删除从光标到当前单词开头的部分 CTRL+a:将光标移到行首 CT ...

  6. LLDB中的小技巧

    1.打印视图层次结构 po [self.view recursiveDescription]   2.临时调整界面UI      比如说现在你需要改变一个控件的背景色来更好的查看布局的问题,这是就不需 ...

  7. POJ 3181 Dollar Dayz(高精度 动态规划)

    题目链接:http://poj.org/problem?id=3181 题目大意:用1,2...K元的硬币,凑成N元的方案数. Sample Input 5 3 Sample Output 5 分析: ...

  8. 开发错误日志之No matching bean of type [xxx] found for dependency

    No matching bean of type [org.springframework.data.mongodb.core.MongoTemplate] found for dependency ...

  9. jquery 之选择符

    css:选择符$('#selected-plays > li') 使用了子元素组合符,查找 ID 为 selected-plays 的元素的子元素( > )中所有的列表 li$('#sel ...

  10. linq 多个left join 和 sql union all -> linq union 方法

     (   from s in Base_SysMenus   join r in Base_RoleRights on s.Menu_Id equals r.Menu_Id into temp   f ...