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

转载于:https://www.cnblogs.com/wxl-Ezio/p/11011374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值