又见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. JavaScript入门(2)

    一.JS输出内容--(document.write) document.write()可用于直接向HTML输出流写内容,即直接在网页中输出内容. 第一种:输出内容用" "括起来,直 ...

  2. jar打包通过exe4j转换成exe文件

    去年的时候有用过,最近写java的时候偶然用到,mark一下,方便以后看 下载链接后面附上 首先我们在eclipse上打包成jar文件,我这里只把简单的截图贴出来,详细的可以自行百度 打包jar文件: ...

  3. PHP 正则通配符

    $a = preg_match('/ph+p/','aaaphpbbbp'); +的前导就是h $a = preg_match('/ph+p/','aaaphhhhhhhhhhpbbbp'); //第 ...

  4. Using load balance for thrift servers

    Software load balance .Nginx(http://nginx.org) 1.Install nginx download source code from http://ngin ...

  5. LINQ 101——分区、Join、聚合

    一.Partitioning 分区 Take 例1:取前3个数 static void Linq1() { , , , , , , , , , }; ); Console.WriteLine(&quo ...

  6. struts2 测试错题解析

    解析:$.parseJSON()方法是将字符串转换成Json类型数据,$.getJSON()方法是获取JSON数据,两者不用联合使用. 解析: A:ActionContext接口没有getReques ...

  7. C++学习笔记-1-自增和自减运算符

    1. post-increment and pre-increment 的区别 来源:http://www.c4learn.com/c-programming/c-increment-operator ...

  8. CSAPP Lab2: Binary Bomb

    著名的CSAPP实验:二进制炸弹 就是通过gdb和反汇编猜测程序意图,共有6关和一个隐藏关卡 只有输入正确的字符串才能过关,否则会程序会bomb终止运行 隐藏关卡需要输入特定字符串方会开启 实验材料下 ...

  9. 博客系统-3.0CodeIgniter系统SAE版本的配置 application/config/

    autoload.php(系统启动时自动加载的文件:包,类库,驱动,方法助手,配置) $autoload['libraries'] = array('database', 'access', 'pag ...

  10. js验证手机号码 ,昵称,密码

    手机号 /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/ 传真~  /^(\d{3,4}-)?\d{7,8}$/ 邮箱 ^[a-z0-9]+([._\\ ...