hdu 1540 Tunnel Warfare(Treap)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540
思路:三种操作:
D摧毁一个点
R重建最晚被修改的那个点
Q询问点x联通的点有多少个
逆向思维,D操作可以看成在平衡树上建个点,R看成在平衡树上删除一个点,Q询问x联通点个数,我们就可以直接在平衡树上找x的前驱l和后继r,点的个数就是r - l - 1,特判下当x点也被摧毁了,那么联通点数量为0。
实现代码:
#include<bits/stdc++.h>
using namespace std;
#define ls t[x].ch[0]
#define rs t[x].ch[1]
const int M = 6e5 + ;
const int inf = 0x3f3f3f3f;
map<int,int>mp;
int last[M],n,m,root,cnt;
struct node{
int ch[],cnt,siz,val,rd;
}t[M]; void up(int x){
t[x].siz = t[ls].siz + t[rs].siz+t[x].cnt;
} void rotate(int &x,int d){
int son = t[x].ch[d];
t[x].ch[d] = t[son].ch[d^];
t[son].ch[d^] = x; up(x); up(x=son);
} void Insert(int &x,int val){
if(!x){
x = ++cnt;
t[x].cnt = t[x].siz = ;
t[x].val = val,t[x].rd = rand();
return ;
}
t[x].siz ++;
if(t[x].val == val){
t[x].cnt++; return ;
}
int d = t[x].val < val; Insert(t[x].ch[d],val);
if(t[x].rd > t[t[x].ch[d]].rd) rotate(x,d);
} void del(int &x,int val){
if(!x) return ;
if(t[x].val == val){
if(t[x].cnt > ){
t[x].cnt--,t[x].siz--;return ;
}
bool d = t[ls].rd > t[rs].rd;
if(ls == ||rs == ) x = ls+rs;
else rotate(x,d),del(x,val);
}
else t[x].siz--,del(t[x].ch[t[x].val<val],val);
} int rk(int x,int val){
if(!x) return ;
if(t[x].val == val) return t[ls].siz+;
if(t[x].val > val) return rk(ls,val);
return rk(rs,val)+t[ls].siz+t[x].cnt;
} int kth(int root,int k){
int x = root;
while(){
if(k <= t[ls].siz) x = ls;
else if(k > t[ls].siz+t[x].cnt)
k -= t[ls].siz+t[x].cnt,x = rs;
else return t[x].val;
}
} int pre(int x,int val){
if(!x) return -inf;
if(t[x].val >= val) return pre(ls,val);
return max(pre(rs,val),t[x].val);
} int nex(int x,int val){
if(!x) return inf;
if(t[x].val <= val) return nex(rs,val);
return min(nex(ls,val),t[x].val);
} int main()
{
string op;
ios::sync_with_stdio();
cin.tie(); cout.tie();
while(cin>>n>>m){
int tot = ,l,r,x;root = ;
mp.clear();t[root].siz = ;
Insert(root,); Insert(root,n+);
for(int i = ;i <= m;i ++){
cin>>op;
if(op[]=='D'){
cin>>x;
if(mp[x] == ){
Insert(root,x);
mp[x] = ;
}
last[++tot] = x;
}
else if(op[] == 'R'){
if(!tot) continue;
del(root,last[tot]);
mp[last[tot]] = ;
tot--;
}
else{
cin>>x;
l = pre(root,x);
r = nex(root,x);
if(mp[x] == ) l = r;
// cout<<l<<" "<<r<<endl;
cout<<max(r-l-,)<<endl;
}
}
}
}
hdu 1540 Tunnel Warfare(Treap)的更多相关文章
- HDU 1540 Tunnel Warfare(线段树+区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争期间进行地道战,存在n个村庄用地道连接,输入D表示破坏某个村庄(摧毁与其相连的地道, 包 ...
- hdu 1540 Tunnel Warfare (线段树,维护当前最大连续区间)
Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively i ...
- hdu 1540 Tunnel Warfare (线段树 区间合并)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1540 Tunnel Warfare (线段树)
Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...
- hdu 1540 Tunnel Warfare(线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. ...
- HDU - 1540 Tunnel Warfare(线段树区间合并)
https://cn.vjudge.net/problem/HDU-1540 题意 D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 分析 线段树的区间内,我 ...
- HDU 1540 Tunnel Warfare(最长连续区间 基础)
校赛,还有什么途径可以申请加入ACM校队? Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/ ...
- hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
随机推荐
- H5 60-浮动元素排序规则
60-浮动元素排序规则 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- ZOJ - 1610 经典线段树染色问题
这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...
- element ui主题色跟换
node_modules\ element ui\ lib\ theme-dafault 下载的主题色替换掉改文件... ================== 但是会出现 搜索框iocon 样式换 ...
- hdu 4135 a到b的范围中多少数与n互质(容斥)
Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...
- 使用HDTune规避硬盘上损坏的扇区
如何使用HDTune扫描磁盘上的错误在网上已经有很多帖子了,但扫描到之后如何用HDTune来规避硬盘上损坏的扇区呢? HDTune并不能直接规避,而是需要重新划分磁盘的卷.HDTune一行有50个小方 ...
- NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plus
NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plushttps://docs. ...
- java 工具
JClassLib 4.2 发布了,该版本支持 Java 7 和 Java 8 的类文件属性查看. JClassLib不但是一个字节码阅读器而且还包含一个类库允许开发者读取,修改,写入Java Cla ...
- 手机移动端input date placehoder不显示
要解决这个问题,我们可以伪造一个placehoder,通过css跟js来解决这个问题. 为什么要用js的原因是因为当你选择了时间之后,placehoder的文字没有清除掉,所以我们就需要把这个伪造的p ...
- [转帖]一段关于Unix与 Linux的暗黑史
一段关于Unix与 Linux的暗黑史 https://blog.csdn.net/a343315623/article/details/51436715 微软曾经开发过 MS-DOS Xenix O ...
- Python 基础知识----流程控制
判断语句 循环语句 嵌套