hznu 1443 Ivan comes again!(set.lower_bound())

      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.

输入

      The input consists of several test cases.
      The first line of input in each test case contains one integer  N (0< N≤200000), 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.

输出

      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.

样例输入

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

0

样例输出

Case 1:
2 3
-1
find a b,输出现有的一对最小值符合(ai>a,bi>b)
当时的想法:用vector<set<int>>,这样比较好找,也能缩短时间,但考虑到vector数组需要开很大。。。
其实直接一个set也不会超时。学习set的 .lower_bound()用法(然而它好像只能找.first,不会看.second,因此第二份代码错)。
比如:
3
add 2 4
add 99 999999
find 1 1000 (第二份代码输出的是2 4,错)
#include<iostream>  
#include<algorithm>  
#include<string>  
#include<string.h>  
#include<set>  
#include<vector>  
#include<cmath>  
#include<cstdlib>  
#include<cstdio>  
#define ll long long  
using namespace std;  
char x[6];
set<pair<int,int> > y;
pair<int,int> r;
int main(){
    int n,cnt=0;
    while(scanf("%d",&n)&&n!=0){
        printf("Case %d:\n",++cnt);
        y.clear();
        int a,b;
        for(int i=0;i<n;++i){
            scanf("%s %d %d",x,&r.first,&r.second);
            if(x[0]=='a')
                y.insert(r);
            else if(x[0]=='r')
                y.erase(r);
            else{
                set<pair<int,int> >::iterator it=y.lower_bound(r);
                while(it!=y.end()){
                    if(it->first>r.first&&it->second>r.second){
                        printf("%d %d\n",it->first,it->second);
                        break;
                    }
                    ++it;
                } 
                if(it==y.end())
                    printf("-1\n");
            }
        }
        printf("\n");
    }
    return 0;
}
WA代码(y.lower_bound(node(a+1,b+1))是错的):
#include<iostream>  
#include<algorithm>  
#include<string>  
#include<string.h>  
#include<set>  
#include<vector>  
#include<cmath>  
#include<cstdlib>  
#include<cstdio>  
#define ll long long  
using namespace std;
struct node{
    int p,q;
    node(int pp=0,int qq=0){
        p=pp;q=qq;
    }
    bool operator < (const node &rhs) const  {  //自定义结构体排序方式 
        if(rhs.p != p)
        return rhs.p > p;  
        return rhs.q > q;  
    } 
};  
int main(){
    int n,cnt=0;
    while(scanf("%d",&n)&&n!=0){
        printf("Case %d:\n",++cnt);
        char x[6];
        set<node> y;
        int a,b;
        for(int i=0;i<n;++i){
            scanf("%s %d %d",x,&a,&b);
            if(x[0]=='a')
                y.insert(node(a,b));
            else if(x[0]=='r')
                y.erase(node(a,b));
            else{
                set<node>::iterator it=y.lower_bound(node(a+1,b+1));
                if(it==y.end())
                    printf("-1\n");
                else
                    printf("%d %d\n",it->p,it->q);
            }
        }
        for(set<node>::iterator it=y.begin();it!=y.end();++it)
        	cout<<it->p<<" "<<it->q<<endl;
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值