码迷,mamicode.com
首页 > 其他好文 > 详细

CF19D 线段树+STL各种应用

时间:2015-01-07 23:36:51      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

http://codeforces.com/problemset/problem/19/D

Description

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 Input

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
/**
CF 19D 线段树+STL
题目大意:在平面直角坐标系的第一象限里进行一系列的点的删除,添加,查询操作。查询是返回(x,y)有上方最靠左最靠下的点的坐标,若不存在返回-1
解题思路:主要有利用线段树的单点更新和单点查询,对于所有x横坐标相同的点放入一个set中,确定了x之后直接upper_bound就是对应点的纵坐标
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
const int maxn=200005;

struct note
{
    int mark,x,y;
} e[maxn];

int n,num[maxn];
char a[10];
set <int>s[maxn];

struct node
{
    int l,r;
    int max_y;
} tree[4*maxn];

void push_up(int root)
{
    tree[root].max_y=max(tree[root<<1].max_y,tree[root<<1|1].max_y);
}
void build(int l,int r,int root)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].max_y=-1;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build(l,mid,root<<1);
    build(mid+1,r,root<<1|1);
}

void update(int x,int root)
{
    if(x==tree[root].l&&x==tree[root].r)
    {
        if(s[x].size())
            tree[root].max_y=*(--s[x].end());
        else tree[root].max_y=-1;
        return;
    }
    int mid=(tree[root].l+tree[root].r)>>1;
    if(x<=mid)
        update(x,root<<1);
    else
        update(x,root<<1|1);
    push_up(root);
}

int query( int x,int y,int root )
{
    if(tree[root].r<=x)
        return -1;
    if(tree[root].max_y<=y)
        return -1;
    if(tree[root].l==tree[root].r)
        return tree[root].l;
    int t=query(x,y,root<<1);
    if(t==-1)
        t=query(x,y,root<<1|1);
    return t;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%s%d%d",a,&e[i].x,&e[i].y);
            if(a[0]=='a')
                e[i].mark=1;
            else if(a[0]=='r')
                e[i].mark=2;
            else
                e[i].mark=3;
            s[i].clear();
            num[i]=e[i].x;
        }
        sort(num+1,num+1+n);
        int m = unique(num+1 , num+1+n)-(num+1);///去重,返回最后一个有效点的下一个迭代器的地址
        build(1,m,1);
        for(int i=1; i<=n; i++)
        {
            int x=upper_bound(num+1,num+1+m,e[i].x)-(num+1);
            int y=e[i].y;
            if(e[i].mark==1)
            {
                s[x].insert(y);
                update(x,1);
            }
            else if(e[i].mark==2)
            {
                s[x].erase(y);
                update(x,1);
            }
            else
            {
                int ans=query(x,y,1);
                if(ans==-1)
                    puts("-1");
                else
                {
                    printf("%d %d\n",num[ans],*s[ans].upper_bound(y));
                }
            }
        }
    }
    return 0;
}


CF19D 线段树+STL各种应用

标签:

原文地址:http://blog.csdn.net/lvshubao1314/article/details/42504393

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!