Codeforce 19D Points 线段树+离散化

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

 分析;将x离散化后,记录每个x对应的最大y,以及x对应的所有y。线段树附加值为[l,r]内最大的y.找到满足条件的x,y即可。一开始不知道remove时怎么处理x对应的所有的y,还以为自己全想错了,然后看了题解知道set就可以做到了。 注意离散化处理的时候的sort,erase,size

code://居然有60+test。。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
//const int maxh=1e9;  //一开始想要记录每个x对应的最小y 后来发现不对
const int maxn=2e5+10;
int max(int a,int b){if(a>b)return a;return b;}

#define LL(o) (o<<1)
#define RR(o) (o<<1|1)

set<int> h[maxn];
bool cmp(int a,int b){ return a<b;}
struct Node{
int l,r;
int maxh;
};

struct Point{
int x,y;
};

vector<int> num;    //离散化这块必须要用 需熟练掌握
Node node[maxn*4];
map<int,int> p; 
void build(int o,int l,int r){
 node[o].l=l;
 node[o].r=r;
 node[o].maxh=-1;
 if(l!=r){
    int mid=l+(r-l)/2;
    build(LL(o),l,mid);
    build(RR(o),mid+1,r);
  }
}
void update(int p,int v,int o,int type){
    if(type==1){//插入
        int l=node[o].l,r=node[o].r;
        int mid=l+(r-l)/2;
        node[o].maxh=max(node[o].maxh,v);
        if(l==r) h[l].insert(v);
        else{
            if(p<=mid)update(p,v,LL(o),type);
            else update(p,v,RR(o),type);
        }
    }
    if(type==0){
        int l=node[o].l,r=node[o].r;
        int mid=l+(r-l)/2;
        if(l==r){ h[l].erase(v);//  一开始写的优先队列== 后来发现不能只能加不能去掉。。
                  if(!h[l].empty())node[o].maxh=*(--h[l].end());
                     else node[o].maxh=-1;
                 }
        else{if(p<=mid) update(p,v,LL(o),type);
                else update(p,v,RR(o),type);
                node[o].maxh=max(node[LL(o)].maxh,node[RR(o)].maxh);
     }
   }
}

Point querry(int wid,int height,int o){
   int l=node[o].l,r=node[o].r;
   Point ans;
   if(node[o].maxh<=height||r<=wid){ ans.x=-1;ans.y=-1;return ans;}
   if(l==r){ ans.x=num[l],ans.y=*(h[l].lower_bound(height+1)); return ans;}//set的lower_bound()
   ans=querry(wid,height,LL(o));
    if (ans.x!=-1)return ans;//一开始没写这个。全部-1-1-1-1
    else return querry(wid,height,RR(o));
}

int ww[maxn],height[maxn];
char s[maxn][15];
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
    scanf("%s%d%d",s[i],&ww[i],&height[i]);
    num.push_back(ww[i]);
}
//离散化
  sort(num.begin(),num.end());
  num.erase(unique(num.begin(),num.end()),num.end());
  int len=(int)num.size()-1;
for(int i=0;i<=len;i++)h[i].clear();
build(1,0,len);
for(int i=0;i<=len;i++)p[num[i]]=i;
for(int i=0;i<n;i++){
    if(s[i][0]=='a') update(p[ww[i]],height[i],1,1);
    if(s[i][0]=='r') update(p[ww[i]],height[i],1,0);
    Point ans;
    if(s[i][0]=='f') {
            ans=querry(p[ww[i]],height[i],1);//一开始写成了num[ww[i]]。。。醉。。
            if(ans.x!=-1)printf("%d %d\n",ans.x,ans.y);
            else printf("-1\n");
    }
}
return 0;
}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值