hdu3627 Giant For

网上很多用线段树做的,,好复杂,,,

这个是用纯STL做的,map+set

转自http://blog.csdn.net/zz_1215/article/details/7318800

View Code
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
using namespace std;
map<int, set<int> > m;//x映射到一个y坐标的集合
map<int, set<int> >::iterator mi;
set<int>::iterator si;
set<int>s;
map<int, int>yy;//记录出现过的y坐标以及次数
int n;
int tx, ty;
char c[11];
int temp, t2;
int tt;
int main()
{
    int cas = 0;
    while(scanf("%d",&n)==1 && n)
    {
        if(cas)puts("");
        printf("Case %d:\n",++cas);
        m.clear();
        s.clear();
        yy.clear();
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", c);
            scanf("%d%d", &tx, &ty);
            if(c[0] == 'a')
            {
                if(yy.find(ty) == yy.end())//ty没有出现过
                {
                    yy[ty] = 1;
                }
                else
                {
                    yy[ty]++;//出现过这累加次数
                }
                if(s.find(tx) == s.end())
                {
                    s.insert(tx);
                }
                mi = m.find(tx);
                if(mi != m.end())//在tx对应的集合中插入ty
                {
                    si = mi -> second.find(ty);
                    if(si != mi -> second.end())
                    {
                        continue;
                    }
                    else
                    {
                        mi->second.insert(ty);
                    }
                }
                else
                {
                    m[tx].insert(ty);
                }
            }
            else if(c[0] == 'f')
            {
                if(yy.upper_bound(ty) == yy.end())//判断是否存在大于ty的最小上界
                {
                    printf("-1\n");
                    continue;
                }
                tt = tx;
                while(true)//依次往上枚举大于tx的值
                {
                    if(s.empty())
                    {
                        printf("-1\n");
                        break;
                    }
                    si = s.upper_bound(tt);
                    if(si != s.end())
                    {
                        temp = *si;
                        tt = temp;
                        if(m[temp].empty())
                        {
                            continue;
                        }
                        si = m[temp].upper_bound(ty);
                        if(si != m[temp].end())
                        {
                            t2 = *si;
                            printf("%d %d\n", temp, t2);
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        printf("-1\n");
                        break;
                    }
                }
            }
            else if(c[0] == 'r')
            {
                m[tx].erase(ty);
                if(m[tx].empty())
                    s.erase(tx);
                if(!--yy[ty])
                    yy.erase(ty);
            }
        }
    }
    return 0;
}

 

posted @ 2012-04-15 09:35  枕边梦  阅读(285)  评论(0编辑  收藏  举报