又见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. 关于sql 资源竞争死锁现象

    问题:System.Exception: 事务(进程 ID 321)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品.请重新运行该事务 死锁最深层的原因就是一个:资源竞争 表现 ...

  2. Android - This Handler class should be static or leaks might occur.

    今天学习了使用 HTTP协议,从Android客户端往Tomcat服务器端以GET发送请求,途中无意中发现自己写的Handler类被Android提示:This Handler class shoul ...

  3. jsp - java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

    使用jsp连接数据库真的不那么容易,之前使用纯java连接sql Server 2008,都是很正常的,但是在使用jsp调用的时候,总是报错ClassNotFoundException.很郁闷 jar ...

  4. iis7.5 应用程序池 经典模式和集成模式的区别

    在 IIS 7.5 中,应用程序池有两种运行模式:集成模式和经典模式. 应用程序池模式会影响服务器处理托管代码请求的方式. 如果托管应用程序在采用集成模式的应用程序池中运行,服务器将使用 IIS 和 ...

  5. OC多文件开发介绍

    OC多文件开发介绍: 1.为什么要使用多文件? 在工作中,通常把不同的类放到不同的文件中,每个类的声明和实现分开,声明写在.h头文件中,实现写在相应的.m文件中去,类名是什么,文件名的前缀就是什么.假 ...

  6. Nginx 独立图片服务器的搭建

    为什么需要独立图片服务器? 如果你留心的话,可以发现,现在主流的网站都是有单独的图片服务器的,例如,人人网的为rrimg,淘宝的为taobaocdn,下面还有很多的二级域名. 独立的图片服务器有诸多好 ...

  7. HDU 5491 The Next

    Problem Description Let L denote the number of 1s in integer D’s binary representation. Given two in ...

  8. 桂电在线-转变成bootstrap版

    由于angularjs的不熟悉,而且SEO需要学习更多东西,于是先采用bootstrap版本,毕竟工作上也需要使用bootstrap,然后参照视频教程学习. bootstrap 基本模板 <!D ...

  9. node 后台ajax文件(同时支持http、https)

    var http = require("http"), Url = require("url"), querystring = require('queryst ...

  10. 工欲善其事必先利其器-Notepad++使用小记(Python)

    大学开始就一直使用Notepad++ 作为代码编辑器,喜欢它的简洁明了,喜欢它的个性,也喜欢它各种各样骚气的插件. 今天闲来无事,写篇文章记录一下平时使用的种种,包括但不限于个性化使用一些宏,快捷键, ...