博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UOJ #349. 【WC2018】即时战略
阅读量:4312 次
发布时间:2019-06-06

本文共 2045 字,大约阅读时间需要 6 分钟。

Description

1146405-20180613225758260-1503051214.png

Solution

链的情况是 \(O(n+log)\) 的,要分开讨论

由于链的情况已知的点一定是一段连续的,维护两个端点不断往两边扩展即可

树的情况是 \(O(n*log)\)

要支持快速查找到一个点所在的位置,我们可以用点分治做一下,找到这个点属于哪一个儿子所在的块,递归找下去就可以了
由于需要存一个儿子所在的块,需要用到 \(map\) ,复杂度是 \(O(n*log^2)\) 的,并且要定期重构.
另一种是用 \(LCT\) , \(access\) 均摊复杂度的做法
两种做法操作次数是 \(O(n*log)\)

#include "rts.h"#include
using namespace std;const int N=300010;int id[N],ch[N][2],fa[N],L[N],R[N];bool vis[N];inline bool isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}inline void upd(int x){ L[x]=R[x]=x; if(ch[x][0])L[x]=L[ch[x][0]]; if(ch[x][1])R[x]=R[ch[x][1]];}inline void rotate(int x){ int y=fa[x];bool t=ch[y][1]==x; ch[y][t]=ch[x][!t];fa[ch[y][t]]=y; ch[x][!t]=y;fa[x]=fa[y]; if(!isrt(y))ch[fa[y]][ch[fa[y]][1]==y]=x; fa[y]=x;upd(y);upd(x);}inline void splay(int x){ while(!isrt(x)){ int y=fa[x],p=fa[y]; if(isrt(y))rotate(x); else if((ch[p][0]==y)==(ch[y][0]==x))rotate(y),rotate(x); else rotate(x),rotate(x); }}inline void access(int x){ int y=0; while(x)splay(x),ch[x][1]=y,upd(x),x=fa[y=x];}inline int getroot(int x){while(!isrt(x))x=fa[x];return x;}inline void ins(int x){ int s=getroot(1),t; while(!vis[x]){ t=explore(s,x); if(t==L[ch[s][1]])s=ch[s][1]; else if(t==R[ch[s][0]])s=ch[s][0]; else if(vis[t])s=getroot(t); else fa[t]=s,vis[s=t]=1; } access(x);}void play(int n, int T, int dataType){ srand(time(NULL)); for(int i=2;i<=n;i++)id[i]=i; for(int i=1;i<=5;i++)random_shuffle(id+2,id+n+1); if(dataType==3){ int l=1,r=explore(1,id[2]); vis[r]=1; for(int i=2;i<=n;i++){ int x=id[i]; if(vis[x])continue; int t=explore(l,x); if(vis[t]){while(r!=x)vis[r=explore(r,x)]=1;} else{ vis[t]=1; while(l!=x)vis[l=explore(l,x)]=1; } } return ; } for(int i=1;i<=n;i++)L[i]=R[i]=i; for(int i=2;i<=n;i++)if(!vis[id[i]])ins(id[i]);}

转载于:https://www.cnblogs.com/Yuzao/p/9180679.html

你可能感兴趣的文章
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
TZC Intercommunication System
查看>>
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
Java Timer触发定时器
查看>>
Page Object设计模式
查看>>
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>