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
*/