现在的位置: 首页 > 综合 > 正文

Codeforces 19 D. Points(线段树&set好题)

2019年04月12日 ⁄ 综合 ⁄ 共 3421字 ⁄ 字号 评论关闭

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

题意:

在二维坐标平面内进行n (1 ≤ n ≤ 2·105)
次操作。一共有三种类型操作。

1.add x,y 将点(x,y)加进坐标系。

2.remove x,y 将点(x,y)移除.

3.find x,y 找到点(x,y)右上角的点(xp>x,yp>y)。如果有多个输出x最小的。还是有多个输出y最小的。

x,y均为非负数。以上操作均合法。

思路:

我们可以用N个set维护很坐标为i的y有哪些。当然需离线对x离散化。这题关键是询问有点难对付。如果我们知道find x,y操作时。最小的满足的x是多少。我们就可以在对应的set里找最小满足的y了。所以怎样快速找到x就是突破点了。我们可以用一颗线段树维护x为v时y的最大值。这样我们只要查询x比v大的区间能否满足在二分就可以快速定位最小的满足的x了。

详细见代码:

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=200010;
typedef long long ll;
#define lson L,mid,ls
#define rson mid+1,R,rs
struct node
{
    int x,y,id;
    char cmd[20];
    void init(){    scanf("%s%d%d",cmd,&x,&y);  }
} q[maxn];
set<int> st[maxn];
int mav[maxn<<2],H[maxn];
int n,m;
void build(int L,int R,int rt)
{
    mav[rt]=-1;
    if(L==R)
        return;
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int rt,int p)
{
    if(L==R)
    {
        if(st[L].size())
            mav[rt]=*(--st[L].end());
        else
            mav[rt]=-1;
        return;
    }
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
    if(p>mid)
        update(rson,p);
    else
        update(lson,p);
    mav[rt]=max(mav[ls],mav[rs]);
}
int qu(int L,int R,int rt,int l,int r,int x)
{
    if(mav[rt]<=x||l>r)
        return -1;
    if(L==R)
        return L;
    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1,tp=-1;
    if(l<=mid)//想想怎样才会到左子树查。
        tp=qu(lson,l,r,x);
    if(tp!=-1)
        return tp;
    if(r>mid)
        return qu(rson,l,r,x);
}
void init()
{
    sort(H,H+m);
    m=unique(H,H+m)-H;
}
int Hash(int x)
{
    return lower_bound(H,H+m,x)-H;
}
int main()
{
    int i,h,p;

    while(~scanf("%d",&n))
    {
        m=n;
        for(i=0;i<n;i++)
            q[i].init(),H[i]=q[i].x;
        init();
        build(1,m,1);
        for(i=0;i<n;i++)
        {
            h=Hash(q[i].x)+1;
            if(q[i].cmd[0]=='a')
                st[h].insert(q[i].y),update(1,m,1,h);
            else if(q[i].cmd[0]=='r')
                st[h].erase(q[i].y),update(1,m,1,h);
            else
            {
                p=qu(1,m,1,h+1,m,q[i].y);//m打成n调了好久。。。。
                if(p==-1)
                    printf("-1\n");
                else
                    printf("%d %d\n",H[p-1],*st[p].upper_bound(q[i].y));//upper>.lower>=的第一个
            }
        }
    }
    return 0;
}

抱歉!评论已关闭.