//P2161 [SHOI2009]Booking 会场预约
//https://www.luogu.org/record/lists?pid=P2161内存,时间效率极高的算法 Splay 伸展树
//很有兴趣进行学习。
//http://blog.youkuaiyun.com/skydec/article/details/20151805先从此文入手
//http://blog.youkuaiyun.com/qq_33184171/article/details/73549164 写得比较基础
//冰冻三尺,非一日之功,先快速解决该题,是第一要素,用set方法吧,顺便开始一些C++的痕迹。
//https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P2161学习set方法
//https://zhidao.baidu.com/question/279995268.html学习了set lower_bound方法
//http://blog.youkuaiyun.com/yas12345678/article/details/52601454学习了C++ set
//一直对为什么重载<而不是=或是>,心存疑虑,此文讲解不错,http://bbs.youkuaiyun.com/topics/390261324摘抄如下:
//肯定可以,C++的设计哲学之一就是使得程序在对待自定义类型时和内置类型必须是一致的(甚至自定义类型的支持更好)。所以,肯定是你程序的问题,如下:
//《C++标准程序库》中明确指出:“只要是assignable、copyable、comparable(根据某个排序准则)的型别T,都可以成为set或multiset的元素型别。”。其中,所谓的comparable指的是less,即可进行<比较。
//反之,则不被支持,所以,问题是,你的A是否支持上述三种语义?
//找了半天,总算,找了个该题set写得比较象样的程序http://blog.youkuaiyun.com/darost/article/details/54882225
//2017-7-23 9:42 AC
#include <cstdio>
#include <set>
using namespace std;
struct node{
int left,right;
bool operator<(const node &v)const{//1 此处写成 bool operator<(const node &v)
if(right==v.right)
return left<v.left;
return right<v.right;
}
};
set<node> s;
int main(){
int n,i,cnt,left,right;
char cmd[5];
set<node>::iterator it;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%s",cmd);
if(cmd[0]=='A'){
scanf("%d%d",&left,&right);
cnt=0;
while(1){
it=s.lower_bound((node){0,left});//建议读者画图理解该句,整个程序中最核心的代码,也是最难写得代码//1 此处写成 it=s.lower_bound(node{0,left});
if(it!=s.end()&&right>=it->left){
s.erase(it);
cnt++;
continue;
}
s.insert((node){left,right});//1 此处写成 s.insert(node{left,right});
break;
}
printf("%d\n",cnt);
}else
printf("%d\n",s.size());
}
return 0;
}
//https://www.luogu.org/record/lists?pid=P2161内存,时间效率极高的算法 Splay 伸展树
//很有兴趣进行学习。
//http://blog.youkuaiyun.com/skydec/article/details/20151805先从此文入手
//http://blog.youkuaiyun.com/qq_33184171/article/details/73549164 写得比较基础
//冰冻三尺,非一日之功,先快速解决该题,是第一要素,用set方法吧,顺便开始一些C++的痕迹。
//https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P2161学习set方法
//https://zhidao.baidu.com/question/279995268.html学习了set lower_bound方法
//http://blog.youkuaiyun.com/yas12345678/article/details/52601454学习了C++ set
//一直对为什么重载<而不是=或是>,心存疑虑,此文讲解不错,http://bbs.youkuaiyun.com/topics/390261324摘抄如下:
//肯定可以,C++的设计哲学之一就是使得程序在对待自定义类型时和内置类型必须是一致的(甚至自定义类型的支持更好)。所以,肯定是你程序的问题,如下:
//《C++标准程序库》中明确指出:“只要是assignable、copyable、comparable(根据某个排序准则)的型别T,都可以成为set或multiset的元素型别。”。其中,所谓的comparable指的是less,即可进行<比较。
//反之,则不被支持,所以,问题是,你的A是否支持上述三种语义?
//找了半天,总算,找了个该题set写得比较象样的程序http://blog.youkuaiyun.com/darost/article/details/54882225
//该题,用set难点在于 结构体 情况下 lower_bound()的使用,以下附图,帮助大家理解,当然,有STL大神 补充,更佳。
//2017-7-23 9:42 AC
#include <cstdio>
#include <set>
using namespace std;
struct node{
int left,right;
bool operator<(const node &v)const{//1 此处写成 bool operator<(const node &v)
if(right==v.right)
return left<v.left;
return right<v.right;
}
};
set<node> s;
int main(){
int n,i,cnt,left,right;
char cmd[5];
set<node>::iterator it;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%s",cmd);
if(cmd[0]=='A'){
scanf("%d%d",&left,&right);
cnt=0;
while(1){
it=s.lower_bound((node){0,left});//建议读者画图理解该句,整个程序中最核心的代码,也是最难写得代码//1 此处写成 it=s.lower_bound(node{0,left});
if(it!=s.end()&&right>=it->left){
s.erase(it);
cnt++;
continue;
}
s.insert((node){left,right});//1 此处写成 s.insert(node{left,right});
break;
}
printf("%d\n",cnt);
}else
printf("%d\n",s.size());
}
return 0;
}