Codeforces Beta Round #19 D. Points 线段树+离散化离散化

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.

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


1.离散化看来已经很常见了。

2.对于离散化后的x值,每个x都建立一个集合set,表示坐标为x的点的y值。

3.

线段树数组存取,一个区间内的最大y值,这样做的目的是便于查找函数query。

见代码。


4.我认为还有一种查找方式是对于find x y,先找到[0,x]区间内有多少个点,记为num,然后去找第num+1个。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
//const int maxn=    ;

int m,n;
const int maxm=2*1e5+20;
struct  Operation
{
    int kind;
    int x,y;
} op[maxm];
vector<int >ve;
set<int >pos[maxm];
map<int,int >mp;
char s[15];
void readop(int ind)
{
    Operation & now=op[ind];
    scanf("%s",s);

    if(s[0]=='f') now.kind=0;
    else if(s[0]=='a') now.kind=1;
    else             now.kind=-1;

    scanf("%d%d",&now.x,&now.y);
    ve.push_back(now.x);
}

struct Segmenttree
{
   int  maxi[4*maxm];

   void build(int ind,int le,int ri)
   {
       if(le==ri)
       {
           maxi[ind]=-1;//天啊,从0开始离散化,这里要初始化为-1,不是0啊!!!
           return;
       }
       MID;
       build(lson);
       build(rson);
       maxi[ind]=max(maxi[ind*2],maxi[ind*2+1]);
   }

   void add(int ind,int le,int ri,int x,int y)
   {
       if(  le==ri)
       {
           maxi[ind]=max(maxi[ind],y);
           pos[le].insert(y);
           return;
       }
        MID;
        if(x<=mid)  add(lson,x,y);
        else        add(rson,x,y);
        maxi[ind]=max(maxi[ind*2],maxi[ind*2+1]);
   }
    void remove(int ind ,int le,int ri,int x,int y)
    {
        if(le==ri)
        {
            pos[le].erase(y );

            maxi[ind]  =  pos[le].empty()? -1:  *(--pos[le].end() );
            return;
        }
        MID;
        if(x<=mid)      remove(lson,x,y);
        else            remove(rson,x,y);
        maxi[ind]=max(maxi[2*ind],maxi[2*ind+1]);
    }
    pair<int,int > query(int ind,int le,int ri,int x,int y)
    {
        if(x>ri|| y>maxi[ind] )  return mk(-1,-1);
        if(le==ri)
        {
            return mk(le,* pos[le].lower_bound(y)  );//这里写x绝壁错
        }
        MID;
         pair<int ,int > tmp= query(lson,x,y);
         if(tmp._f !=-1)  return tmp;
         return  query(rson,x,y);
    }

}sgt;
int main()
{
  while(~scanf("%d",&m))
  {

      ve.clear();
      mp.clear();
      for(int i=1;i<=m;i++)
        readop(i);

      sort(ve.begin(),ve.end());  //这句话都写掉了,真TM坑爹。
      ve.erase(unique(ve.begin(),ve.end()),ve.end());
      n=ve.size();
      for(int i=0;i<n;i++)
      {
          int x=ve[i];//我艹!!!!   int x=op[i].x;
          mp[x]=i;
      }
      for(int i=0;i<n;i++)
        pos[i].clear();

      sgt.build(1,0,n);
//      cout<<mp[7]<<endl;
      for(int i=1;i<=m;i++)
      {
          int kind=op[i].kind;
          int x=op[i].x,y=op[i].y;
          /*
          for(set<int>:: iterator it=pos[2].begin();it!=pos[2].end();it++)
            cout<<*it<<" ";
          cout<<endl;
*/

          if(kind==1)  sgt.add(1,0,n,mp[x],y);
          else if(kind==-1) sgt.remove(1,0,n, mp[x],y);
          else
          {
              pair<int ,int > pa=sgt.query(1,0,n,mp[x]+1,y+1);
              if(pa._f==-1)  puts("-1");
              else printf("%d %d\n",ve[pa._f],pa._s);
          }

      }


  }

    return 0;
}

/*
5
add 7 7
find 6 6
remove 7 7
find 6 6
find 4 4
*/


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
//const int maxn=    ;

int m,n;
const int maxm=2*1e5+20;
struct  Operation
{
    int kind;
    int x,y;
} op[maxm];
vector<int >ve;
set<int >pos[maxm];
map<int,int >mp;
char s[15];
void readop(int ind)
{
    Operation & now=op[ind];
    scanf("%s",s);

    if(s[0]=='f') now.kind=0;
    else if(s[0]=='a') now.kind=1;
    else             now.kind=-1;

    scanf("%d%d",&now.x,&now.y);
    ve.push_back(now.x);
}

struct Segmenttree
{
   int  maxi[4*maxm];

   void build(int ind,int le,int ri)
   {
       if(le==ri)
       {
           maxi[ind]=-1;//天啊,从0开始离散化,这里要初始化为-1,不是0啊!!!
           return;
       }
       MID;
       build(lson);
       build(rson);
       maxi[ind]=max(maxi[ind*2],maxi[ind*2+1]);
   }

   void add(int ind,int le,int ri,int x,int y)
   {
       if(  le==ri)
       {
           maxi[ind]=max(maxi[ind],y);
           pos[le].insert(y);
           return;
       }
        MID;
        if(x<=mid)  add(lson,x,y);
        else        add(rson,x,y);
        maxi[ind]=max(maxi[ind*2],maxi[ind*2+1]);
   }
    void remove(int ind ,int le,int ri,int x,int y)
    {
        if(le==ri)
        {
            pos[le].erase(y );

            maxi[ind]  =  pos[le].empty()? -1:  *(--pos[le].end() );
            return;
        }
        MID;
        if(x<=mid)      remove(lson,x,y);
        else            remove(rson,x,y);
        maxi[ind]=max(maxi[2*ind],maxi[2*ind+1]);
    }
    pair<int,int > query(int ind,int le,int ri,int x,int y)
    {
        if(y>maxi[ind] )  return mk(-1,-1);
        if(le==ri)
        {
            return mk(le,* pos[le].lower_bound(y)  );//这里写x绝壁错
        }
        MID;
         pair<int ,int > tmp= x<=mid?query(lson,x,y):mk(-1,-1);
         if(tmp._f !=-1)  return tmp;
         return  query(rson,x,y);
    }

}sgt;
/*
void debug()
{
    for(int i=0;i<ve.size();i++)
    {
        cout<<ve[i]<<" "<<mp[ve[i]]<<" ";
    }
    cout<<endl;
}*/
int main()
{
  while(~scanf("%d",&m))
  {

      ve.clear();
      mp.clear();
      for(int i=1;i<=m;i++)
        readop(i);

      sort(ve.begin(),ve.end());  //这句话都写掉了,真TM坑爹。
      ve.erase(unique(ve.begin(),ve.end()),ve.end());
      n=ve.size();
      for(int i=0;i<n;i++)
      {
          int x=ve[i];//我艹!!!!   int x=op[i].x;
          mp[x]=i;
      }
      for(int i=0;i<n;i++)
        pos[i].clear();

//      debug();
      sgt.build(1,0,n);
//      cout<<mp[7]<<endl;
      for(int i=1;i<=m;i++)
      {
          int kind=op[i].kind;
          int x=op[i].x,y=op[i].y;
          /*
          for(set<int>:: iterator it=pos[2].begin();it!=pos[2].end();it++)
            cout<<*it<<" ";
          cout<<endl;
*/

          if(kind==1)  sgt.add(1,0,n,mp[x],y);
          else if(kind==-1) sgt.remove(1,0,n, mp[x],y);
          else
          {
              pair<int ,int > pa=sgt.query(1,0,n,mp[x]+1,y+1);
              if(pa._f==-1)  puts("-1");
              else printf("%d %d\n",ve[pa._f],pa._s);
          }

      }


  }

    return 0;
}

/*
5
add 7 7
find 6 6
remove 7 7
find 6 6
find 4 4
*/



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值