codeforces 19D(线段树的应用)

                                                                                                      D. Points
                                           time limit per test 2 seconds                              memory limit per test 256 megabytes
                                                               input standard input              output tandard 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 to2·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 follown 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 exceed109.

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.

Examples
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

题目链接:http://codeforces.com/problemset/problem/19/D
简要题意:
在xoy轴上定义的点,有三种操作:
add  加入一个点;
remove 将该点删去;
find 寻找x,y严格大于改点的第一个点,x为主排序(即线比较x,在比较y);


思路:
  首先很容易想到set,set本身带有插入删除操作,而且自动生成有序序列;
  其次是线段树,当x的顺序已经排好了,就是查找操作,这刚好是线段树的区间最大值操作;
#include<stdio.h>
#include<string.h>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=2*1e5+4;
set<int>tree[maxn];
vector<int>v;
char p[maxn][10];
int x[maxn],y[maxn];
int num[maxn*8];
void update(int o,int l,int r,int t,int value)
{
    if(l==r)
    {
        num[o]=value;
        return;
    }
    int m=(l+r)>>1;
    if(t<=m)
      update(o<<1,l,m,t,value);
    else
      update(o<<1|1,m+1,r,t,value);
    num[o]=max(num[o<<1],num[o<<1|1]);
}
int query(int o,int l,int r,int L,int R,int value)
{
    if(num[o]<=value)
        return -1;
    if(l==r)
        return l;
    int m=(l+r)>>1;
    int k1=-1,k2=-1;
    if(L<=m&&num[o<<1]>value)
    {
        k1=query(o<<1,l,m,L,R,value);
        if(k1!=-1)
            return k1;
    }
    if(R>=m+1&&num[o<<1|1]>value)
        k2=query(o<<1|1,m+1,r,L,R,value);
    return k2;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)
        {
            scanf("%s%d%d",p[i],&x[i],&y[i]);
            if(p[i][0]!='f')
            v.push_back(x[i]);
        }
        sort(v.begin(),v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        int m=v.size();
        for(int i=0;i<n;i++)
        {
            if(p[i][0]=='a'||p[i][0]=='r')
            {
                int t=lower_bound(v.begin(),v.end(),x[i])-v.begin();
                if(p[i][0]=='a')
                    tree[t].insert(y[i]);
                else if(p[i][0]=='r')
                    tree[t].erase(y[i]);
                int value;
                if(tree[t].empty())
                   value=-1e9;
                else
                {
                     value=*--tree[t].end();
                }
                update(1,0,m-1,t,value);
            }
            else
            {
                int t=upper_bound(v.begin(),v.end(),x[i])-v.begin();
                int z=query(1,0,m-1,t,m-1,y[i]);
                if(z==-1||t==m)
                {
                    printf("-1\n");
                    continue;
                }
                int ans=*tree[z].upper_bound(y[i]);
                printf("%d %d\n",v[z],ans);
            }
        }
    }
    return 0;
}
 线段树的操作唯一要注意的就是数组记得*4; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值