loj#6038 「雅礼集训 2017 Day5」远行
分析
代码
- #include<bits/stdc++.h>
- using namespace std;
- #define fi first
- #define se second
- #define _(x) int x
- #define mp make_pair
- #define pr pair<int,int>
- int fa[],son[][],a[],siz[],r[];
- inline void up(int x){siz[x]=siz[son[x][]]+siz[son[x][]]+;return;}
- inline void rev(int x){swap(son[x][],son[x][]);r[x]^=;return;}
- inline void pd(int x){if(r[x]){for(_(i)=;i<;i++)if(son[x][i])rev(son[x][i]);r[x]=;}return;}
- inline int notroot(int x){return x==son[fa[x]][]||x==son[fa[x]][];}
- inline void pushall(int x){if(notroot(x))pushall(fa[x]);pd(x);return;}
- inline int gs(int x){return x==son[fa[x]][];}
- inline void rot(int x){
- int y=fa[x],z=fa[y],b=gs(x),c=gs(y),d=son[x][!b];
- if(notroot(y))son[z][c]=x;
- fa[x]=z;if(d)fa[d]=y;
- son[y][b]=d,son[x][!b]=y;
- fa[y]=x;up(y),up(x);return;
- }
- inline void splay(int x){
- pushall(x);
- while(notroot(x)){
- int y=fa[x],z=fa[y];
- if(notroot(y)){
- if(gs(x)==gs(y))rot(y);
- else rot(x);
- }
- rot(x);
- }
- return;
- }
- inline void access(int x){for(int y=;x;y=x,x=fa[x])splay(x),son[x][]=y,up(x);return;}
- inline void makeroot(int x){access(x),splay(x),rev(x);return;}
- inline void spt(int x,int y){makeroot(x),access(y),splay(y);return;}
- inline void link(int x,int y){makeroot(x);fa[x]=y;return;}
- pr d[];
- int n,m,q,f[],p1,p2,mx;
- inline int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
- inline void work(int x,int y){
- spt(x,y);int res=siz[y]-;
- if(res>mx)mx=res,p1=x,p2=y;
- return;
- }
- inline void mer(int x,int y){
- pr a=d[sf(x)],b=d[sf(y)];
- mx=;link(x,y);
- work(a.fi,a.se),work(b.fi,b.se);
- work(a.fi,b.fi),work(a.fi,b.se);
- work(a.se,b.fi),work(a.se,b.se);
- f[sf(x)]=sf(y);d[sf(y)]=mp(p1,p2);
- return;
- }
- inline int que(int x){
- pr a=d[sf(x)];
- int res=;
- spt(a.fi,x),res=max(res,siz[x]-);
- spt(a.se,x),res=max(res,siz[x]-);
- return res;
- }
- int main(){
- int i,j,k,la=,opt,x,y;
- scanf("%d%d%d",&opt,&n,&q);
- for(i=;i<=n;i++)f[i]=i,d[i]=mp(i,i),siz[i]=;
- while(q--){
- scanf("%d",&k);
- if(k==)scanf("%d%d",&x,&y);
- else scanf("%d",&x);
- x^=(la*opt),y^=(la*opt);
- if(k==)mer(x,y);
- else printf("%d\n",la=que(x));
- }
- return ;
- }
loj#6038 「雅礼集训 2017 Day5」远行的更多相关文章
- LOJ#6038. 「雅礼集训 2017 Day5」远行(LCT)
题面 传送门 题解 要不是因为数组版的\(LCT\)跑得实在太慢我至于去学指针版的么--而且指针版的完全看不懂啊-- 首先有两个结论 1.与一个点距离最大的点为任意一条直径的两个端点之一 2.两棵树之 ...
- 【刷题】LOJ 6038 「雅礼集训 2017 Day5」远行
题目描述 Miranda 生活的城市有 \(N\) 个小镇,一开始小镇间没有任何道路连接.随着经济发现,小镇之间陆续建起了一些双向的道路但是由于经济不太发达,在建设过程中,会保证对于任意两个小镇,最多 ...
- LOJ#6038. 「雅礼集训 2017 Day5」远行 [LCT维护子树的直径]
树的直径一定是原联通块4个里的组合 1.LCT,维护树的直径,这题就做完了 2.直接倍增,lca啥的求求距离,也可以吧- // powered by c++11 // by Isaunoya #inc ...
- [LOJ#6039].「雅礼集训 2017 Day5」珠宝[决策单调性]
题意 题目链接 分析 注意到本题的 \(C\) 很小,考虑定义一个和 \(C\) 有关的状态. 记 \(f(x,j)\) 表示考虑到了价格为 \(x\) 的物品,一共花费了 \(j\) 元的最大收益. ...
- loj#6040. 「雅礼集训 2017 Day5」矩阵(线性代数+递推)
题面 传送门 题解 我的线代学得跟屎一样看题解跟看天书一样所以不要指望这题我会写题解 这里 //minamoto #include<bits/stdc++.h> #define R reg ...
- @loj - 6039@ 「雅礼集训 2017 Day5」珠宝
目录 @description@ @solution@ @accpeted code@ @details@ @description@ Miranda 准备去市里最有名的珠宝展览会,展览会有可以购买珠 ...
- loj #6039 「雅礼集训 2017 Day5」珠宝 分组背包 决策单调性优化
LINK:珠宝 去年在某个oj上写过这道题 当时懵懂无知wa的不省人事 终于发现这个东西原来是有决策单调性的. 可以发现是一个01背包 但是过不了 冷静分析 01背包的复杂度有下界 如果过不了说明必然 ...
- loj6038「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目传送门 https://loj.ac/problem/6038 题解 根据树的直径的两个性质: 距离树上一个点最远的点一定是任意一条直径的一个端点. 两个联通块的并的直径是各自的联通块的两条直径的 ...
- 【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^5$ ,$ ...
随机推荐
- java学习笔记(5)多线程
一.简介(过段时间再写,多线程难度有点大) --------------------------------------- 1.进程:运行时的概念,运行的应用程序 2.线程:应用程序内部并发执行的代码 ...
- vue-cli常用插件安装教程
1.安装sass npm i sass-loader node-sass --save-dev 2.安装stylus cnpm install stylus --save-dev cnpm insta ...
- python语音提示
#coding:utf8 import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") whi ...
- 学习-Pytest(三)setup/teardown
1. 用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效 ...
- python-字符编码的转换
python-字符编码的转换 1.了解基础知识 ASCII 一个英文,占一个字节.只能存英文和特殊字符. gb2312 约可以存7000中文 gb1830 约可以存27000中文 gbk 默认中文, ...
- 页面中获取 iframe 中的值
3.页面中获取 iframe 中的值 var obj=document.getElementsByClassName(".ke-edit-iframe").contentWindo ...
- AIX中crontab和at 定时任务
1.crontab crontab文件用于在指定日期和时间周期性地执行作业 crontab 作业存放在/var/spool/cron/crontabs/$USER cron根据crontab文件项运行 ...
- MapReduce计数程序(自主复习)
1.MyWordCount类 注意: 1.本机+测试,两个注释都放开 2.本机跑集群,要开异构平台为true 3.集群跑,把两个注释都注起来,然后在集群上面跑 package com.littlepa ...
- @ResponseBody 注解是什么意思?
1. @ResponseBody注解的作用是将Controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据 ...
- linux shell鼠标键盘快捷键