CodeforcesBeta Round #19 D. Points (线段树)

题目链接:点这里!!!!


题意:

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

数据范围:操作数<=2e5;x,y<=1e9


题解:

1、这道题是五一之前写的,然后因为回家加上生了病就耽搁到现在才写完,才发现好水。


2、首先我们先对x进行离散化,将其当作线段树的坐标。利用y值去更新。然而我们1个x有多个y,所以我们就对每个x开个set(STL里的结构,自行学习)。


3、我们线段树记录的是每个x的最大值,判断是否存在解,只要在[x+1,cnt]是否存在大于y(询问的y)的值就可,然后一直找最左边符合条件的即可。具体看代码。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin
const int  MOD = 1E9+7;
const LL N = 2e5+15;
const int maxn = 1e4+1000;
const int letter = 130;
const int INF = 1e17;
const double pi=acos(-1.0);
const double eps=1e-8;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
int ps[N],mx[N<<2];
set<int>index[N];
struct node{
    char ch[10];
    int x,y;
}que[N];
inline void pushup(int lr){
    mx[lr]=max(mx[lr<<1],mx[lr<<1|1]);
}
inline void add_point(int x,int y,int lr,int l,int r){
    if(l==r){
        mx[lr]=max(mx[lr],y);
        index[x].insert(y);
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid) add_point(x,y,lson);
    else add_point(x,y,rson);
    pushup(lr);
}
inline void del_point(int x,int y,int lr,int l,int r){
    if(l==r){
        index[x].erase(y);
        mx[lr]=index[x].empty()?0:(*--index[x].end());
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid) del_point(x,y,lson);
    else del_point(x,y,rson);
    pushup(lr);
}
inline pa query(int ll,int rr,int v,int lr,int l,int r){
    if(mx[lr]<v||r<ll) return make_pair(0,0);
    if(l==r) return make_pair(l,*(index[l].lower_bound(v)));
    int mid=(l+r)>>1;
    pa ans=query(ll,rr,v,lson);
    if(ans.first!=0) return ans;
    return query(ll,rr,v,rson);
}
int main(){
    scanf("%d",&n);
    int cnt=0;
    for(int i=1;i<=n;i++){
        scanf("%s%d%d",que[i].ch,&que[i].x,&que[i].y);
        ps[++cnt]=que[i].x;
    }
    sort(ps+1,ps+cnt+1);
    cnt=unique(ps+1,ps+cnt+1)-(ps+1);
    for(int i=1;i<=n;i++) que[i].x=lower_bound(ps+1,ps+cnt+1,que[i].x)-ps;
    clr(mx,0);
    for(int i=1;i<=n;i++){
        char ch=que[i].ch[0];
        int x=que[i].x,y=que[i].y;
        if(ch=='a') add_point(x,y,1,1,cnt);
        else if(ch=='r') del_point(x,y,1,1,cnt);
        else {
            pa ans=query(x+1,cnt,y+1,1,1,cnt);
            if(ans.first==0) puts("-1");
            else printf("%d %d\n",ps[ans.first],ans.second);
        }
    }
    return 0;
}
/*
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值