poj 2892 Tunnel Warfare(线段树#5/树状数组)

维护一个表,能够插入和删除,并且能够对某一点求前驱和后继。

方法一:线段树 422ms


#include <cstdio>
#include <cstring>
#include <stack> 
#include <set>
#include <algorithm> 
#define N 50010
using namespace std; 
 
stack<int> st;
 int n, m;
  
 

int main()
{
           
  
     char op[3];
      int x;
      //FILE * fp = fopen("in.txt", "r"); 
      scanf( "%d %d", &n, &m);
      set<int> v; 
      while (m--)
      {
            scanf( "%s", op);
            if (op[0] == 'D')
            {
                      scanf( "%d", &x);
                      st.push(x);
                      v.insert(x); 
            }
            else if (op[0] == 'R')
            {
                     int cc = st.top();
                     st.pop();
                     v.erase(cc); 
            }
            else {
                     scanf( "%d", &x);
                     int ans; 
                     if (v.count(x) == 1)ans = 0;
                     else if (v.size() == 0) ans = n; 
                     else 
                     {
                          int l, r; 
                          set<int>::iterator it = v.upper_bound(x);
                          if (it == v.end())r = n + 1, l = (*(--it)), ans = (r - l - 1);
                          else if (it == v.begin())r = (*it), l = 0, ans = (r - l - 1);
                          else {
                                set<int>::iterator itt = (--it);
                                it++;
                                ans = (*it - *itt - 1);
                          }
                     } 
                     printf("%d", ans);
                     if (m)puts(""); 
            }
      }
     // getchar(); 
      return 0;
} 



方法二: STL  594ms


#include <cstdio>
#include <cstring>
#include <stack> 
#include <set>
#include <algorithm> 
#define N 50010
using namespace std; 
 
stack<int> st;
 int n, m;
  
 

int main()
{
           
  
     char op[3];
      int x;
      //FILE * fp = fopen("in.txt", "r"); 
      scanf( "%d %d", &n, &m);
      set<int> v; 
      while (m--)
      {
            scanf( "%s", op);
            if (op[0] == 'D')
            {
                      scanf( "%d", &x);
                      st.push(x);
                      v.insert(x); 
            }
            else if (op[0] == 'R')
            {
                     int cc = st.top();
                     st.pop();
                     v.erase(cc); 
            }
            else {
                     scanf( "%d", &x);
                     int ans; 
                     if (v.count(x) == 1)ans = 0;
                     else if (v.size() == 0) ans = n; 
                     else 
                     {
                          int l, r; 
                          set<int>::iterator it = v.upper_bound(x);
                          if (it == v.end())r = n + 1, l = (*(--it)), ans = (r - l - 1);
                          else if (it == v.begin())r = (*it), l = 0, ans = (r - l - 1);
                          else {
                                set<int>::iterator itt = (--it);
                                it++;
                                ans = (*it - *itt - 1);
                          }
                     } 
                     printf("%d", ans);
                     if (m)puts(""); 
            }
      }
     // getchar(); 
      return 0;
} 


 法三:树状数组+二分查找  这个懒得写了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值