hdu 1540 Tunnel Warfare STL
看完题的第一眼就在怀疑tag
能二分的事为什么要写线段树
普通二分没法支持动态插入
用c++自带的stl
set<int>st;
st.clear()清空
st.insert(x)插入
r返回第一个>=x的数.upper_bound()为第一个>x的数
l返回第一个<x的数,只要用r指向的位置-1即可
在有序数组中插入一个数,必然只能位于两个数之间
r=*st.lower_bound(x);
l=*(--st.lower_bound(x));
--
#include <iostream> #include <math.h> #include <string.h> #include <vector> #include <map> #include <queue> #include <stdio.h> #include <algorithm> #include <cstdio> #include <set> using namespace std; int d[500000],cnt=0; set<int>st; int main( ) { int n,m; //freopen("lys.in","r",stdin); while(cin>>n>>m) { cnt=0; int x; st.clear(); st.insert(0); st.insert(n+1); for(int i=1;i<=m;i++) { char type[2]; scanf("%s",type); if(type[0]=='R') { st.erase(d[cnt]); cnt--; } else if(type[0]=='D') { scanf("%d",&x); st.insert(x); cnt++; d[cnt]=x; } else { int l,r; scanf("%d",&x); if(st.count(x)) { printf("0\n");continue; } r=*st.lower_bound(x); l=*(--st.lower_bound(x)); // cout<<l<<" "<<r<<endl; printf("%d\n",r-l-1); } } } }
分类:
数据结构
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)