hdu1540

题解思路用set维护把每个被破坏的点当做边界然后求左右边界就可以了
题目链接

#include
#include
#include
#include
#include
using namespace std;
set<int>st;
int main(){
    int n,m,x;
    char s[30];
    while(scanf("%d%d",&n,&m)!=EOF){
        stack<int>sk; 
        st.clear();
        st.insert(n+1);
        st.insert(0);
        while(m--){
            scanf("%s",s);
            if(s[0] == 'D'){
                scanf("%d",&x);
                st.insert(x);
                sk.push(x);
            }
            else if(s[0] == 'R'){
                x = sk.top();
                sk.pop();
                st.erase(x);
            }
            else{
                scanf("%d",&x);
                auto it = st.lower_bound(x);
                auto it2 = it;
                it2--;
                if(*it == x)
                    cout<<"0"<else
                    printf("%d\n",*it-*it2-1);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(数据结构)