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);
            }
        }
    }
}

 

上一篇:Netvirt之流表分析(一):Netvirt介绍


下一篇:SuperEdge 云边隧道新特性:从云端SSH运维边缘节点