[HZOI 2016]tree—增强版
[HZOI 2016]tree—增强版
注意事项
为了体现增强版,题目限制和数据范围有所增强:
时间限制:1.5s
内存限制:128MB
对于15% 的数据,1<=N,Q<=1000.
对于35% 的数据,1<=N,Q<=10000.
对于50% 的数据,1<=N,Q<=100000,且数据均为官方数据.
对于100% 的数据,1<=N,Q<=1000000.
请注意常数因子对于程序运行的影响。
解题报告
原题数据水到不行= =
直接找父节点都能上榜= =
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int read(){
int sum();
char ch(getchar());
for(;ch<''||ch>'';ch=getchar());
for(;ch>=''&&ch<='';sum=sum*+(ch^),ch=getchar());
return sum;
}
struct edge{
int e;
edge *n;
edge():e(),n(NULL){}
}a[],*pre[];
int tot;
inline void insert(int s,int e){
a[++tot].e=e;
a[tot].n=pre[s];
pre[s]=&a[tot];
}
int n,q;
char op[];
bool bj[];
int fa[];
inline void dfs(int u){
for(edge *i=pre[u];i;i=i->n){
int e(i->e);
if(e!=fa[u]){
fa[e]=u;
dfs(e);
}
}
}
inline int query(int u){
while(!bj[u])
u=fa[u];
return u;
}
inline int gg(){
freopen("heoi2016_tree.in","r",stdin);
freopen("heoi2016_tree.out","w",stdout);
memset(pre,NULL,sizeof(pre));
n=read(),q=read();
bj[]=;
for(int i=;i<n;++i){
int x(read()),y(read());
insert(x,y),insert(y,x);
}
dfs();
while(q--){
scanf("%s",op);
int x(read());
if(op[]=='Q')
printf("%d\n",query(x));
else
bj[x]=;
}
return ;
}
int K(gg());
int main(){;}
然而加强版数据真的不是闹着玩的= =
首先我们看,它是一个不断往上爬,直到爬到第一个有标记的点或者是根节点时才停止的过程,那么我们很容易想到树剖,从当前节点往上爬就好了
同时我们注意到,题目要求找离当前节点最近的被标记的祖先,所以我们只要去找深度最深的被标记的祖先即可,而由树链剖分中$dfs$序的性质可知,在同一条链上越深的节点,它的时间戳就越大,那么我们显然可以维护一颗最大值的线段树,假如该点被标记,就把它的时间戳加入线段树
剩下的就很好实现了
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
inline int read(){
int sum=;
char ch=getchar();
for(;ch<''||ch>'';ch=getchar());
for(;ch>=''&&ch<='';sum=sum*+(ch^),ch=getchar());
return sum;
}
struct edge{
int e,n;
}a[];
int pre[];
int tot;
inline void insert(int s,int e){
a[++tot].e=e;
a[tot].n=pre[s];
pre[s]=tot;
}
int dep[],fa[],son[],size[];
inline void dfs1(int u){
size[u]=,son[u]=;
for(int i=pre[u];i!=-;i=a[i].n){
int e=a[i].e;
if(e!=fa[u]){
fa[e]=u;
dep[e]=dep[u]+;
dfs1(e);
size[u]+=size[e];
if(size[e]>size[son[u]])
son[u]=e;
}
}
}
int cnt;
int top[],id[],pos[];
inline void dfs2(int u,int rt){
top[u]=rt;
id[u]=++cnt;
pos[cnt]=u;
if(son[u])
dfs2(son[u],rt);
else
return;
for(int i=pre[u];i!=-;i=a[i].n){
int e=a[i].e;
if(e!=fa[u]&&e!=son[u])
dfs2(e,e);
}
}
int mx[];
inline void pushup(int i){
mx[i]=max(mx[i<<],mx[i<<|]);
}
inline void update(int pos,int l,int r,int i){
if(l==r){
mx[i]=pos;
return;
}
int mid=l+r>>;
if(pos<=mid)
update(pos,l,mid,i<<);
else
update(pos,mid+,r,i<<|);
pushup(i);
}
inline int query(int ll,int rr,int l,int r,int i){
if(ll<=l&&r<=rr)
return mx[i];
int mid=l+r>>,ret=;
if(ll<=mid)
ret=max(query(ll,rr,l,mid,i<<),ret);
if(mid<rr)
ret=max(query(ll,rr,mid+,r,i<<|),ret);
return ret;
}
int n,q;
char op[];
inline int ask(int x){
int ret();
while(x){
int tmp=query(id[top[x]],id[x],,n,);
if(tmp)
ret=dep[ret]>dep[pos[tmp]]?ret:pos[tmp];
x=fa[top[x]];
}
return ret;
}
int main(){
int __size__ = << ;
char *__p__ = (char*)malloc(__size__) + __size__;
__asm__("movl %0, %%esp\n" :: "r"(__p__));
freopen("tree++.in","r",stdin);
freopen("tree++.out","w",stdout);
memset(pre,-,sizeof(pre));
n=read(),q=read();
for(int i=;i<n;++i){
int x=read(),y=read();
insert(x,y);
}
dfs1();
dfs2(,);
int x;
update(id[],,n,);
for(int i=;i<q;++i){
scanf("%s",op);
x=read();
if(op[]=='Q')
printf("%d\n",ask(x));
else
update(id[x],,n,);
}
return ;
}
[HZOI 2016]tree—增强版的更多相关文章
- office 2016 专业增强版 和 visio 2016 专业版 下载安装(附带激活工
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://babyshen.blog.51cto.com/8405584/1697910 o ...
- [系统相关]WPS Office 2016 专业增强版 10.8.0.6470 免序列号无限制
WPS Office (10.8.0.6470) 新增功能列表 ============================================= 改进功能列表 ------------ W ...
- 【办公】Microsoft Office 2016 专业增强版下载及永久激活-亲测分享
Win7 x64,安装 Office 2016. 1. 下载 Office 2016,用迅雷网上下载飞快.(这里分享我的下载链接,2.39G用迅雷分分钟就下好了) 2. 按 此博客 ,安装激活工具. ...
- 微软发布 Windows Server 2016 预览版第三版,开发者要重点关注Nano Server
微软已经发布 Windows Server 2016 和 System Center 2016 第三个技术预览版,已经提供下载.Windows Server 2016 技术预览版第三版也是首个包括了容 ...
- 最新GHOST XP系统下载旗舰增强版 V2016年
系统来自:系统妈:http://www.xitongma.com 深度技术GHOST xp系统旗舰增强版 V2016年3月 系统概述 深度技术ghost xp系统旗舰增强版集合微软JAVA虚拟机IE插 ...
- 最新深度技术GHOST XP系统旗舰增强版 V2016年
来自系统妈:http://www.xitongma.com 深度技术GHOST xp系统旗舰增强版 V2016年 系统概述 深度技术ghost xp系统旗舰增强版集合微软JAVA虚拟机IE插件,增强浏 ...
- WinNTSetup v3.8.7 正式版绿色增强版
最强系统安装利器:WinNTSetup 现已更新至 v3.8.7 正式版!这次更新修复调整了诸多问题,新版非常好用接近完美!WinNTSetup 现在已经自带BCDBoot 选项,并且完全支持Wind ...
- 《zw版·delphi与halcon系列原创教程》zw版_THOperatorSetX控件函数列表 v11中文增强版
<zw版·delphi与halcon系列原创教程>zw版_THOperatorSetX控件函数列表v11中文增强版 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就 ...
- zw版_zw中文增强版Halcon官方Delphi例程
[<zw版·delphi与halcon系列原创教程>zw版_zw中文增强版Halcon官方Delphi例程 源码下载:http://files.cnblogs.com/files/ziwa ...
随机推荐
- git ldap
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md ldap : enabled : true host : 'ope ...
- 【BZOJ 2160】 拉拉队排练
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2160 [算法] 先简化题意 : 给定一个字符串,求最长的k个奇回文子串长度的乘积 先 ...
- 2017 nodeJS
一.版本迅速更新 Chrome浏览器已经蹦到57版本了,是名副其实的版本帝,作为兄弟的Node.js也一样,1.0之前等了6年,而从1.0到8.0,只用了2年时间,现在已到9以上了 我们就数一下 从v ...
- Hadoop 的使用
hadoop:hadoop启动:./sbin/start-dfs.shhadoop关闭:./sbin/stop-dfs.shbin文件用于在HDFS创建数据HDFS 中创建用户目录:./bin/hdf ...
- Behavior Designer扩展
BehaviorManager.instance.Tick(behaviorTree); 卸载update里u3d直接卡死 = = SharedVariable直接赋值会改变他的引用关系,必须用XXX ...
- 题解报告:hdu 1162 Eddy's picture
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
- VMware 14 Pro 永久许可证激活密钥
VMware 14 Pro 永久许可证激活密钥 FF31K-AHZD1-H8ETZ-8WWEZ-WUUVACV7T2-6WY5Q-48EWP-ZXY7X-QGUWD
- vs2010 视图 aspx页面设计窗口创建控件时出错 未将对象引用设置到对象的实例
第一步,首先关闭aspx页面 第二步,在单击项目右击,选择“清理” 第三步,然后在打开aspx页面,就可以看到正常的页面了. 注:一次不行的会,多做几次. 如果还是不行的话,你看看你.cs页面是否继承 ...
- vue-cli的创建、基本配置和遇到的问题总结
vue-cli的创建及基本配置 1. 创建 vue-cli 项目 确保本地安装了最新版本的nodejs环境(会自带npm环境); 全局安装vue-cli,命令:npm i -g vue-cli 创建项 ...
- 微信接口本地调试(IIS服务器)
1.下载ngrok,并注册获得token.官网下载地址:https://ngrok.com/ 如果你是在官网下载的,到后面映射域名的时候会要求购买他们的服务. 这里我们用一个国内免费的ngrok服务器 ...


