SHOI2009 会场预约

题目传送门

嗯,这道题的标签是STL,因为这个STL用的确实太妙了


这道题目要求维护一堆区间,而一个重要的操作是要删除所有与新区间冲突的区间

虽然可以用\(Splay\)来操作,但用STL里的set也绝对不虚
其中最精妙的当属这个重载运算符
它的意思是当两个区间相交时,这两个区间相等

struct zzz {
    int l, r;
    bool operator < (const zzz &y) const { return r < y.l; }
}; set <zzz> q;

重载完运算符,再凭借set的find函数,就可以很容易的通过此题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#define LL long long
using namespace std;
LL read() {
    LL k = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9')
        k = k * 10 + c - 48, c= getchar();
    return k;
}
char read_c() {
    char c = getchar();
    while(c != 'A' && c != 'B') c = getchar();
    return c;
}
struct zzz {
    int l, r;
    bool operator < (const zzz &y) const { return r < y.l; }
}; set <zzz> q;
int main() {
    int m = read();
    while(m--) {
        char opt = read_c();
        if(opt == 'A') {
            int l = read(), r = read(), cnt = 0;
            zzz k = (zzz){l, r};
            set<zzz> :: iterator it = q.find(k);
			//将相交的区间全都删去
            while(it != q.end()) {
                ++cnt; q.erase(it);
                it = q.find(k);
            }
            q.insert(k);
            printf("%d\n", cnt);
        }
        else printf("%d\n", q.size());
        
    }
    return 0;
}
posted @ 2019-11-14 10:32  MorsLin  阅读(70)  评论(0编辑  收藏  举报