2010山东ACM省赛 Ivan comes again! Sdibt 2408

Description

The Fairy Ivan gave Saya three problems to solve (Problem F). After Saya finished the first problem (Problem H), here comes the second.
This is the enhanced version of Problem H.
There is a large matrix whose row and column are less than or equal to 1000000000. And there are three operations for the matrix:
1)
add: Mark an element in the matrix. The element wasn’t marked before it is marked.
2)remove: Delete an element’s mark. The element was marked before the element’s mark is deleted.
3)find: Show an element’s row and column, and return a marked element’s row and column, where the marked element’s row and column are larger than the showed element’s row and column respectively. If there are multiple solutions, return the element whose row is the smallest; and if there are still multiple solutions, return the element whose column is the smallest. If there is no solution, return -1.
Of course, Saya comes to you for help again.

Input

The input consists of several test cases.
The first line of input in each test case contains one integer N (0<N200000), which represents the number of operations.
Each of the next N lines containing an operation, as described above.
The last case is followed by a line containing one zero.

Output

For each case, print the case number (1, 2 …) first. Then, for each “find” operation, output the result. Your output format should imitate the sample output. Print a blank line after each test case.

Sample Input

4
add 2 3
find 1 2
remove 2 3
find 1 2

0

Sample Output

Case 1:
2 3
-1


这题和那道Hello World的题目如出一辙,而题目也正是这么描述的,是那道题目的加强版,没错,那道题是全部数据给出后再全部查询,而这道题目是随机的增加,减少,查询数据,无法知道准确的数据量,如果还用结构体做,势必会造成空间的浪费,最关键的是,每次插入删除数据都要对数据重新排序,如果数据很多,会造成时间的严重浪费,看到这里,如果你对STL略知一二的话,可能已经想到了set,没错,在这个容器中的数据都是有序的,而且插入(insert())和删除(erase())函数都是现成的,只有查找的时候需要写一下,可以说相当的方便,需要注意几个地方:

1.数据是成对出现的,所以要使用pair<int,int>定义一个泛型,作为set中的元素

2.在查找数据的时候,迭代器没必要从头开始,因为数据本身已经进行了排序,所以只要使用lower_bound(p)返回小于等于p的第一个元素的位置即可,从这个位置往后查,很快就可以查到数据

3.同样不要忘了每组测试数据最后有一个空行

#include <iostream>
#include<stdio.h>
#include<cstring>
#include <set>
using namespace std;
int main()
{
    pair<int,int>p;
    int n,cas=1;
	char op[10];
    while(scanf("%d",&n),n)
    {
        cout<<"Case "<<cas++<<":"<<endl;
        set< pair<int,int> >s;
        set< pair<int,int> >::iterator it;
        while(n--)
        {
        	scanf("%s",op);
        	scanf("%d%d",&p.first,&p.second); 
            //cin>>op>>p.first>>p.second;
            if(op[0]=='a')
                s.insert(p);
            if(op[0]=='r')
                s.erase(p);
            if(op[0]=='f')
            {
                for(it=s.lower_bound(p);it!=s.end();it++)
                {
                    if(it->first>p.first&&it->second>p.second)
                    {
                    	printf("%d%s%d\n",it->first," ",it->second);
                        //cout<<it->first<<" "<<it->second<<endl;
                        break;
                    }
                }
                if(it==s.end())
                printf("%d\n",-1);
                    //cout<<-1<<endl;
            }
        }
        cout<<endl;
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值