CodeforcesBeta Round #19 D. Points(找出平面上坐标严格大于(x,y)的点)

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 to 2·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 follow n 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 exceed 109.

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.

Examples
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

有三种操作“add x y”往平面上添加(x,y)这个点,"remove x y",将平面上已经存在的点(x,y)删除,“find x y”找出平面上坐标严格大于(x,y)的点,如果有多个点找x最小的,再找y最小的。

 

思路:

方法一:

先将点离散化,x作为线段树上的位置,线段树上的每个位置维护x相同时的y能取到的最大值,每次查询用查询的点的x坐标定位线段树上的区间,然后用y来确定最靠近x的是哪一个位置。

 

时间复杂度分析:原来的区间最多被分为logn段不相交的区间,假设找到一个区间的最大值满足条件,再对那一段区间进行查找,也最多被分为logn,所以时间复杂度为logn*logn*n

 

#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=4*100100;
int maxv[4*maxn],t[maxn],HASH[maxn],ans_x,ans_y,pos,v,L,R;
multiset<int>st[maxn];
struct query{
    char s[10];
    int x,y;
}Q[maxn];

void pushup(int rt){
    maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}

void update(int l,int r,int rt){
    if(l==r){
        maxv[rt]=v;
        return ;
    }
    int m=(l+r)>>1;
    if(pos<=m)
        update(lson);
    else
        update(rson);
    pushup(rt);
}

void query(int l,int r,int rt){
    if(L>R)
        return ;
    if(l==r){
        ans_x=l;
        ans_y=maxv[rt];
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m&&maxv[rt<<1]>=v)
        query(lson);
    if(R>m&&maxv[rt<<1|1]>=v&&ans_y==-1)
        query(rson);
}

int main(){
    int n;
    memset(maxv,-1,sizeof(maxv));
    scanf("%d",&n);
    int cnt=0;
    for(int i=1;i<=n;i++){
        scanf("%s%d%d",Q[i].s,&Q[i].x,&Q[i].y);
        t[++cnt]=Q[i].x,t[++cnt]=Q[i].y;
    }
    sort(t+1,t+cnt+1);
    memset(maxv,-1,sizeof(maxv));
    int m=unique(t+1,t+cnt+1)-t-1;
    multiset<int>::iterator it;
    for(int i=1;i<=m;i++){
        HASH[i]=t[i];
        st[i].clear();
    }
    for(int i=1;i<=n;i++){
        if(Q[i].s[0]=='a'){
            int x=lower_bound(t+1,t+m+1,Q[i].x)-t;
            int y=lower_bound(t+1,t+m+1,Q[i].y)-t;
            st[x].insert(y);
            pos=x,v=*st[x].rbegin();
            update(1,m,1);
        }
        else if(Q[i].s[0]=='r'){
            int x=lower_bound(t+1,t+m+1,Q[i].x)-t;
            int y=lower_bound(t+1,t+m+1,Q[i].y)-t;
            it=st[x].lower_bound(y);
            st[x].erase(it);
            if(st[x].size()==0){
                pos=x,v=-1;
                update(1,m,1);
            }
            else{
                y=*st[x].rbegin();
                pos=x,v=y;
                update(1,m,1);
            }
        }
        else{
            L=lower_bound(t+1,t+m+1,Q[i].x)-t+1,R=m;
            v=lower_bound(t+1,t+m+1,Q[i].y)-t+1;
            ans_y=-1;
            query(1,m,1);
            if(ans_y==-1)
                printf("-1\n");
            else{
                ans_y=*st[ans_x].lower_bound(v);
                printf("%d %d\n",HASH[ans_x],HASH[ans_y]);
            }
        }
    }
}

方法二:

每次查询的时候用二分进行判断,判断[L,x]之间的最大值是不是大于等于y,时间复杂度logn*logn*n

#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=4*100100;
int maxv[4*maxn],t[maxn],HASH[maxn],pos,v,L,R;
multiset<int>st[maxn];
struct query{
    char s[10];
    int x,y;
}Q[maxn];

void pushup(int rt){
    maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}

void update(int l,int r,int rt){
    if(l==r){
        maxv[rt]=v;
        return ;
    }
    int m=(l+r)>>1;
    if(pos<=m)
        update(lson);
    else
        update(rson);
    pushup(rt);
}

int query(int l,int r,int rt){
    if(L<=l&&R>=r)
        return maxv[rt];
    int m=(l+r)>>1;
    int ret=-1;
    if(L<=m)
        ret=query(lson);
    if(R>m&&ret<v)
        ret=query(rson);
    return ret;
}

int main(){
    int n;
    memset(maxv,-1,sizeof(maxv));
    scanf("%d",&n);
    int cnt=0;
    for(int i=1;i<=n;i++){
        scanf("%s%d%d",Q[i].s,&Q[i].x,&Q[i].y);
        t[++cnt]=Q[i].x,t[++cnt]=Q[i].y;
    }
    sort(t+1,t+cnt+1);
    memset(maxv,-1,sizeof(maxv));
    int m=unique(t+1,t+cnt+1)-t-1;
    multiset<int>::iterator it;
    for(int i=1;i<=m;i++){
        HASH[i]=t[i];
        st[i].clear();
    }
    for(int i=1;i<=n;i++){
        if(Q[i].s[0]=='a'){
            int x=lower_bound(t+1,t+m+1,Q[i].x)-t;
            int y=lower_bound(t+1,t+m+1,Q[i].y)-t;
            st[x].insert(y);
            pos=x,v=*st[x].rbegin();
            update(1,m,1);
        }
        else if(Q[i].s[0]=='r'){
            int x=lower_bound(t+1,t+m+1,Q[i].x)-t;
            int y=lower_bound(t+1,t+m+1,Q[i].y)-t;
            it=st[x].lower_bound(y);
            st[x].erase(it);
            if(st[x].size()==0){
                pos=x,v=-1;
                update(1,m,1);
            }
            else{
                y=*st[x].rbegin();
                pos=x,v=y;
                update(1,m,1);
            }
        }
        else{
            v=lower_bound(t+1,t+m+1,Q[i].y)-t+1;
            int l=lower_bound(t+1,t+m+1,Q[i].x)-t+1,r=m,ANS_ID=-1;
            while(l<=r){
                int mid=(l+r)>>1;
                L=l,R=mid;
                if(query(1,m,1)>=v)
                    r=mid-1,ANS_ID=mid;
                else
                    l=mid+1;
            }
            if(ANS_ID==-1)
                printf("-1\n");
            else{
                int ans_y=*st[ANS_ID].lower_bound(v);
                printf("%d %d\n",HASH[ANS_ID],HASH[ans_y]);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值