P2161 [SHOI2009]会场预约

这一题是用来练习stl的,,,
stl的set固然很方便, 但是在c++98里erase好像是没有返回值的, 不能像c++11一样

it = S.erase(it);

所以c++98里删掉以后最好重新找以防RE.
具体在这道题中, 就是每一次lower_bound以后看看能不能删前面的或者后面的.
c++98真是反人类啊...什么时候noip能够用c++11呢.

#include <set>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
const int MAXN = 200000 + 10;
inline int read(){
    char ch = getchar(); int x = 0;
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x;
}

int N;
set<P> S;

int main(){
    // freopen("p2161.in", "r", stdin);
    // freopen("p2161.out", "w", stdout);
    cin>>N;
    while(N--) {
        char opt; scanf(" %c", &opt);
        if(opt == 'A') {
            int l = read(), r = read();
            P cur = P(l, r); int cnt = 0;
            set<P>::iterator it;
            while(true) {
                it = S.lower_bound(cur); bool flag = false;
                if(it->first <= r && it->second >= l) 
                    ++cnt, S.erase(it), flag = true;
                it = S.lower_bound(cur);
                if(it != S.begin()) {
                    --it; 
                    if(it->first <= r && it->second >= l) 
                        ++cnt, S.erase(it), flag = true;
                }
                if(!flag) break;
            }
            S.insert(cur); printf("%d\n", cnt);
        }
        else printf("%d\n", (int)S.size());
    }
    return 0;
}
posted @ 2018-11-05 11:54  俺是小程  阅读(142)  评论(0编辑  收藏  举报