sdut-2159 Ivan comes again!(set+线段树)

Ivan comes again!

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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<<i style="padding: 0px; margin: 0px;">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.

输出

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

用线段树维护每行的列的最大值,查找的时候就可以确定需要查找的set的位置,给的数虽然大,但是只有200000个操作,最多也就200000个数,所以可以先离线下来再离散处理一下
代码:
1、set的方法
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define INF 999999
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxd=100;
typedef long long ll;
//typedef pair<int,int> info;
typedef struct info
{
    int x,y;
    friend bool operator<(info a,info b)
    {
        if(a.x==b.x) return a.y<b.y;
        return a.x<b.x;
    }
} ;

int main()
{
    freopen("in.txt", "r", stdin);
    int n,x,y,kase=1;
    char str[maxd];
    set<info> s;
    while(scanf("%d",&n)!=EOF && n)
    {
        s.clear();
        printf("Case %d:\n",kase++);
        while(n--)
        {
            scanf("%s %d%d",str,&x,&y);
            info a={x,y};
            if(!strcmp(str,"add"))
                s.insert(a);
            else if(!strcmp(str,"remove"))
            {
                set<info>::iterator p=s.find({x,y});
                if(p!=s.end())
                    s.erase(p);
            }
            else
            {
                set<info>::iterator p=s.lower_bound(a);
                if(p==s.end()) p=s.begin();
                for(p; p!=s.end(); ++p)
                    if(p->first>x && p->second>y)
                    {
                        printf("%d %d\n",p->first,p->second);
                        break;
                    }


                if(p==s.end()) printf("-1\n");
            }
        }
        printf("\n");
    }
    return 0;
}
2、set+线段树优化
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define INF 999999
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxd=200000+5;
const int maxn=1000000000+5;
typedef long long ll;
typedef pair<int,int> pii;
int n,x,y,kase=1,cnt,Num;
char str[30];
set<int> s[maxd];
struct info
{
    int x,y,z;
} a[maxd];
int b[maxd],maxv[maxd*2];

int query(int num,int pos1,int l,int r,int o)///从pos1开始查找大于等于num
{
    if(l==r)
    {
        if(maxv[o]>=num) return l;
        return -1;
    }
    int m= l+(r-l)/2,ans=-1;
    if(pos1<=m && maxv[o*2]>=num) ans=query(num,pos1,l,m,o*2);
    if(ans!=-1) return ans;
    if(maxv[o*2+1]>=num) ans=query(num,pos1,m+1,r,o*2+1);
    return ans;
}

void update(int p,int v,int l,int r,int o)///a[p]=v;
{

    if(l==r) maxv[o]=v;
    else
    {
        int m=l+(r-l)/2;
        if(p<=m) update(p,v,l,m,o*2);
        else update(p,v,m+1,r,o*2+1);
        maxv[o]=max(maxv[o*2],maxv[o*2+1]);
    }
}

void init()
{
    Num=0;
    mem(maxv,-1);
    for(int i=0; i<maxd; ++i)
        s[i].clear();
    printf("Case %d:\n",kase++);
    for(int i=0; i<n; ++i)
    {
        scanf("%s %d%d",str,&a[i].x,&a[i].y);
        if(!strcmp(str,"add"))
        {
            a[i].z=1;
            b[Num++]=a[i].x;
        }
        else if(!strcmp(str,"find"))
            a[i].z=2;
        else
            a[i].z=3;

    }///先离线下来
    cnt=1;
    for(int i=1; i<Num; ++i)
    {
        if(b[i]!=b[i-1])
            b[cnt++]=b[i];
    }///离散化处理
    sort(b,b+cnt);
}

void solve()
{
    for(int i=0; i<n; ++i)
        if(a[i].z==1)///add
        {
            int pos=lower_bound(b,b+cnt,a[i].x)-b;
           // cout<<pos<<endl;
            s[pos].insert(a[i].y);
            update(pos,*(--s[pos].end()),0,cnt,1);
        }
        else if(a[i].z==2)///find
        {
            int pos=upper_bound(b,b+cnt,a[i].x)-b;
            if(pos==cnt)
            {
                printf("-1\n");
                continue;
            }
            int ans=query(a[i].y+1,pos,0,cnt,1);
            if(ans==-1) printf("-1\n");
            else printf("%d %d\n",b[ans],*(s[ans].lower_bound(a[i].y+1)));

        }
        else///remove
        {
            int pos=lower_bound(b,b+cnt,a[i].x)-b;
            s[pos].erase(a[i].y);
            if(s[pos].size()==0) update(pos,-1,0,cnt,1);
            else
                update(pos,*(--s[pos].end()),0,cnt,1);
        }
}

int main()
{
    freopen("in.txt", "r", stdin);
    while(scanf("%d",&n)!=EOF && n)
    {
        init();
        solve();
        printf("\n");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值