hdu 3627 Giant For(map+set模拟)

题目链接:

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值