题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3627
解题思路:
这题是hdu3626的加强版,如果直接暴力的话,肯定会超时,所以需要一点小小的优化,那就用map+set优化吧。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map<int,set<int> > m;
map<int,set<int> >::iterator itm;
set<int>::iterator its;
set<int> s;
map<int,int> mm;
int n;
int main(){
int t = 1;
while(scanf("%d",&n),n){
if(t != 1)
cout<<endl;
printf("Case %d:\n",t++);
m.clear();
s.clear();
mm.clear();
char op[10];
int tx,ty;
int tmpp,tmpx,tmpy;
for(int i = 1; i <= n; i++){
scanf("%s",op);
scanf("%d%d",&tx,&ty);
if(op[0]=='a'){
if(mm.find(ty) == mm.end())
mm[ty] = 1;
else
mm[ty]++;
if(s.find(tx) == s.end())
s.insert(tx);
itm = m.find(tx);
if(itm != m.end()){
its = itm -> second.find(ty);
if(its != itm->second.end())
continue;
else
itm->second.insert(ty);
}
else
m[tx].insert(ty);
}
else if(op[0] == 'f'){
if(mm.upper_bound(ty) == mm.end()){
printf("-1\n");
continue;
}
tmpp = tx;
while(true){
if(s.empty()){
printf("-1\n");
break;
}
its = s.upper_bound(tmpp);
if(its != s.end()){
tmpx = *its;
tmpp = tmpx;
if(m[tmpx].empty())
continue;
its = m[tmpx].upper_bound(ty);
if(its != m[tmpx].end()){
tmpy = *its;
printf("%d %d\n",tmpx,tmpy);
break;
}
else
continue;
}
else{
printf("-1\n");
break;
}
}
}
else if(op[0]=='r'){
m[tx].erase(ty);
if(m[tx].empty())
s.erase(tx);
if(!--mm[ty])
mm.erase(ty);
}
}
}
return 0;
}