codeforces 19D 线段树+set

D. Points
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coordinate system on it as follows: point(0, 0) is located in the bottom-left corner, Ox axis is directed right, Oy axis is directed up. Pete gives Bob requests of three types:

  • add x y — on the sheet of paper Bob marks a point with coordinates(x, y). For each request of this type it's guaranteed that point(x, y) is not yet marked on Bob's sheet at the time of the request.
  • remove x y — on the sheet of paper Bob erases the previously marked point with coordinates(x, y). For each request of this type it's guaranteed that point(x, y) is already marked on Bob's sheet at the time of the request.
  • find x y — on the sheet of paper Bob finds all the marked points, lying strictly above and strictly to the right of point(x, y). Among these points Bob chooses the leftmost one, if it is not unique, he chooses the bottommost one, and gives its coordinates to Pete.

Bob managed to answer the requests, when they were 10, 100 or 1000, but when their amount grew up to2·105, Bob failed to cope. Now he needs a program that will answer all Pete's requests. Help Bob, please!

Input

The first input line contains number n (1 ≤ n ≤ 2·105) — amount of requests. Then there follown lines — descriptions of the requests. add x y describes the request to add a point, remove x y — the request to erase a point, find x y — the request to find the bottom-left point. All the coordinates in the input file are non-negative and don't exceed109.

Output

For each request of type find x y output in a separate line the answer to it — coordinates of the bottommost among the leftmost marked points, lying strictly above and to the right of point(x, y). If there are no points strictly above and to the right of point(x, y), output -1.

Sample test(s)
Input
7
add 1 1
add 3 4
find 0 0
remove 1 1
find 0 0
add 1 1
find 0 0
Output
1 1
3 4
1 1
Input
13
add 5 5
add 5 6
add 5 7
add 6 5
add 6 6
add 6 7
add 7 5
add 7 6
add 7 7
find 6 6
remove 7 7
find 6 6
find 4 4
Output
7 7
-1
5 5

题意: 有n种操作, add表示在平面上添加一个点(x,y), remove表示删除已经存在的一个点(x,y), find表示要你找一个点, 这个点的x坐标比给定点的x大, y也要比给定的y大, 如果存在多个点, 那么输出x最小的, 如果还是有多个, 输出x最小的情况下y最小的.

分析:这题以前在杭电上做过的, 原先是用了一个set和一个map, 杭电的数据比较弱,时间比较多, 就过了, 但是那个代码放到这里就超时间了. 只好另寻出路.

考虑先把x轴离散化, 这样x最大就是n了, 可以用set数组来存放对应x轴的那些y, 同时用线段树来记录和查找每个x对应的最大值, 当查找的时候, 先把x的区间找出来, 然后在这个区间里在找最大值大于给定的y的最小的 x, 至于如何查找这个最小的x, 我用的是二分查找(话说这个地方并不是有序的, 但是可以用二分耶. ). 找到了以后用set的二分查找就能找到满足条件的最小的y;

ps: 里面真是各种log啊~, 姿势不好的话可能就T了, 另外输入数据量比较大, 最好是加个输入挂~ , 等以后学了新姿势再来重新写以下把..

#include<bitset> //1684ms
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;char c;int f=1;
    while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
const int N=200010;

struct st
{
    int l,r,mx;
}a[N<<2];

set<int> s[N];
set<int>::iterator it;
char c[N];
pii x[N],tmp[N];

void build(int l,int r,int i)
{
    a[i].l=l,a[i].r=r;
    if(l==r)
    {
        a[i].mx=-1;
        return;
    }
    int mid=l+r>>1;
    build(l,mid,i<<1);
    build(mid+1,r,i<<1|1);
    a[i].mx=max(a[i<<1].mx,a[i<<1|1].mx);
}

void update(int l,int r,int i,int val)
{
    if(a[i].l>=l && a[i].r<=r)
    {
        a[i].mx=val;
        return;
    }
    if(a[i].l>r || a[i].r<l) return;
    update(l,r,i<<1,val);
    update(l,r,i<<1|1,val);
    a[i].mx=max(a[i<<1].mx,a[i<<1|1].mx);
}

int query(int l,int r,int i)
{
    if(a[i].l>=l && a[i].r<=r) return a[i].mx;
    if(a[i].l>r || a[i].r<l) return -1;
    return max(query(l,r,i<<1),query(l,r,i<<1|1));
}

int main()
{
    int n=in(),m=1;
    for(int i=0;i<n;i++)
    {
        c[i]=getchar();
        tmp[i].F=in(),tmp[i].S=in();
        if(c[i]=='a' || c[i]=='r') x[m++]=tmp[i];
    }
    sort(x+1,x+m);
    build(1,m,1);
    for(int i=0;i<n;i++)
    {
        if(c[i] == 'a')
        {
            int pos = lower_bound(x+1,x+m,tmp[i])-x;
            if(s[pos].size() && *(--s[pos].end()) >= tmp[i].S)
            {
                s[pos].insert(tmp[i].S);
                continue;
            }
            s[pos].insert(tmp[i].S);
            update(pos,pos,1,tmp[i].S);
        }
        else if(c[i]=='r')
        {
            int pos = lower_bound(x+1,x+m,tmp[i])-x;
            s[pos].erase(tmp[i].S);
            if(s[pos].size()==0) update(pos,pos,1,-1);
            else update(pos,pos,1,*(--s[pos].end()));
        }
        else
        {
            int pos=lower_bound(x+1,x+m,pii(tmp[i].F+1,-1))-x;
            int l=pos,r=n,ans=-1;

            if(query(l,r,1)<=tmp[i].S)
            {
                puts("-1");
                continue;
            }
            while(l<r-1)
            {
                int mid=l+r>>1;
                if(query(l,mid,1) > tmp[i].S)
                {
                    r=mid;
                }
                else l=mid+1;
            }
            if(query(l,l,1)>tmp[i].S) ans=l;
            if(ans==-1 && query(r,r,1)>tmp[i].S) ans=r;
            if(ans==-1)
            {
                puts("-1");
                continue;
            }
            it = s[ans].upper_bound(tmp[i].S);
            printf("%d %d\n",x[ans].F,*it);
        }
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值