博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JLOI2015 城池攻占
阅读量:7095 次
发布时间:2019-06-28

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

额...左偏树(可并堆)。

两件事情,1:需要用到lazy标记(左偏树是二叉数据结构,可以PushDown)2.用到lazy标记时,注意先乘后加(在打标记的时候修改加法标记)

剩下的,就是dfs模拟(有一些奇怪的OJ会发生爆栈的鬼畜事情)

维护小根堆,以能到达这个节点的骑士的战斗力为排序基准

之后每次删除不能满足的节点,剩下的就是类板子了,可以放心食用

递归版

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 #define N 300005 10 #define ll long long 11 struct node 12 { 13 int ls,rs,dis;ll x,add,mul; 14 }mp[N<<1]; 15 struct no 16 { 17 int to,next; 18 }e[N]; 19 ll h[N],v[N]; 20 int head[N],cnt,fa[N],dep[N],a[N],siz[N],ans[N],s[N],n,m; 21 void add(int x,int y) 22 { 23 e[cnt].to=y; 24 e[cnt].next=head[x]; 25 head[x]=cnt++; 26 return ; 27 } 28 void PushDown(int x) 29 { 30 if(mp[x].mul!=1) 31 { 32 mp[mp[x].ls].x*=mp[x].mul; 33 mp[mp[x].rs].x*=mp[x].mul; 34 mp[mp[x].ls].mul*=mp[x].mul; 35 mp[mp[x].rs].mul*=mp[x].mul; 36 mp[mp[x].ls].add*=mp[x].mul; 37 mp[mp[x].rs].add*=mp[x].mul; 38 mp[x].mul=1; 39 } 40 if(mp[x].add) 41 { 42 if(mp[x].ls) 43 mp[mp[x].ls].x+=mp[x].add,mp[mp[x].ls].add+=mp[x].add; 44 if(mp[x].rs) 45 mp[mp[x].rs].x+=mp[x].add,mp[mp[x].rs].add+=mp[x].add; 46 mp[x].add=0; 47 } 48 } 49 int merge(int x,int y) 50 { 51 if(!x)return y; 52 if(!y)return x; 53 if(mp[x].x>mp[y].x)swap(x,y); 54 PushDown(x); 55 mp[x].rs=merge(mp[x].rs,y); 56 if(mp[mp[x].rs].dis>mp[mp[x].ls].dis)swap(mp[x].ls,mp[x].rs); 57 mp[x].dis=mp[mp[x].rs].dis+1; 58 return x; 59 } 60 void dfs(int x,int from) 61 { 62 for(int i=head[x];i!=-1;i=e[i].next) 63 { 64 int to1=e[i].to; 65 dep[to1]=dep[x]+1; 66 dfs(to1,x); 67 int fx=fa[x],fy=fa[to1]; 68 fa[x]=merge(fx,fy); 69 } 70 if(!fa[x])return ; 71 int fx=fa[x]; 72 while(mp[fx].x
View Code

非递归版

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 #define N 300005 10 #define ll long long 11 struct node 12 { 13 int ls,rs,dis;ll x,add,mul; 14 }mp[N<<1]; 15 struct no 16 { 17 int to,next; 18 }e[N]; 19 ll h[N],v[N]; 20 int head[N],cnt,fa[N],dep[N],a[N],siz[N],ans[N],s[N],n,m; 21 void add(int x,int y) 22 { 23 e[cnt].to=y; 24 e[cnt].next=head[x]; 25 head[x]=cnt++; 26 return ; 27 } 28 void PushDown(int x) 29 { 30 if(mp[x].mul!=1) 31 { 32 mp[mp[x].ls].x*=mp[x].mul; 33 mp[mp[x].rs].x*=mp[x].mul; 34 mp[mp[x].ls].mul*=mp[x].mul; 35 mp[mp[x].rs].mul*=mp[x].mul; 36 mp[mp[x].ls].add*=mp[x].mul; 37 mp[mp[x].rs].add*=mp[x].mul; 38 mp[x].mul=1; 39 } 40 if(mp[x].add) 41 { 42 if(mp[x].ls) 43 mp[mp[x].ls].x+=mp[x].add,mp[mp[x].ls].add+=mp[x].add; 44 if(mp[x].rs) 45 mp[mp[x].rs].x+=mp[x].add,mp[mp[x].rs].add+=mp[x].add; 46 mp[x].add=0; 47 } 48 } 49 int merge(int x,int y) 50 { 51 if(!x)return y; 52 if(!y)return x; 53 if(mp[x].x>mp[y].x)swap(x,y); 54 PushDown(x); 55 mp[x].rs=merge(mp[x].rs,y); 56 if(mp[mp[x].rs].dis>mp[mp[x].ls].dis)swap(mp[x].ls,mp[x].rs); 57 mp[x].dis=mp[mp[x].rs].dis+1; 58 return x; 59 } 60 int f[N]; 61 void dfs(int x) 62 { 63 f[1]=0; 64 while(x) 65 { 66 int son=0; 67 for(int i=head[x];i!=-1;i=e[i].next) 68 { 69 int to1=e[i].to; 70 if(to1!=f[x]) 71 { 72 f[to1]=x; 73 dep[to1]=dep[x]+1; 74 head[x]=e[i].next; 75 x=to1; 76 son=1; 77 break; 78 } 79 } 80 if(!son) 81 { 82 if(!fa[x]) 83 { 84 x=f[x]; 85 continue; 86 } 87 int fx=fa[x]; 88 while(mp[fx].x
View Code

bfs版没有写...

转载于:https://www.cnblogs.com/Winniechen/p/8890801.html

你可能感兴趣的文章
Windows 网卡超过序列
查看>>
shiro-简介
查看>>
nndl_读数笔记
查看>>
优化网站设计系列文章总结和导读
查看>>
ORACLE SET命令
查看>>
【Python3爬虫】第一个Scrapy项目
查看>>
数据结构之最短路径(1) [迪杰斯特拉算法]
查看>>
static变量
查看>>
dubbo之结果缓存
查看>>
java.lang的接口
查看>>
HDU 3001 Travelling 【状态压缩DP】
查看>>
HDU 1116 Play on Words【欧拉通路or回路】
查看>>
php or java?choose。
查看>>
C++静态库与动态库
查看>>
javascript数据结构与算法--基本排序算法(冒泡、选择、排序)及效率比较
查看>>
first day~
查看>>
字符串的替换问题
查看>>
BeautifulSoup
查看>>
详细设计(改)
查看>>
5步减小你的CSS样式表
查看>>