Points - CodeForces 19 D 线段树

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


题意:每次find找到x和y均严格大于给定数值的点,优先找到x最小的,然后优先找到y最小的,如果没有这样的点就输出-1。

思路:首先离散化排序,使得x,y小的在前面(优先x),然后对于每个节点,记录其中存在的点的y的最大值,以及子节点的x的范围,最后查询。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
map<int,int> match;
char s[10];
int op[100010][2],f[100010];
struct node
{
    int num,l,r;
    ll M[6];
}tree[400010];
void build(int o,int l,int r)
{
    tree[o].l=l;
    tree[o].r=r;
    tree[o].num=0;
    memset(tree[o].M,0,sizeof(tree[o].M));
    if(l==r)
      return;
    int mi=(l+r)/2;
    build(o*2,l,mi);
    build(o*2+1,mi+1,r);
}
void update(int o,int pos,int val,int f)
{
    if(tree[o].l==pos && tree[o].r==pos)
    {
        if(f==1)
        {
            tree[o].num=1;
            tree[o].M[1]=val;
        }
        else
        {
            tree[o].num=0;
            tree[o].M[1]=0;
        }
        return;
    }
    int mi=(tree[o].l+tree[o].r)/2;
    if(pos<=mi)
      update(o*2,pos,val,f);
    else
      update(o*2+1,pos,val,f);
    int i;
    for(i=0;i<5;i++)
       tree[o].M[i]=tree[o*2].M[i]+tree[o*2+1].M[(i-tree[o*2].num%5+5)%5];
    tree[o].num=tree[o*2].num+tree[o*2+1].num;
}
int main()
{
    int n,m,i,j,k;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            if(s[0]=='s')
              op[i][0]=0;
            else
            {
                if(s[0]=='a')
                  op[i][0]=1;
                else
                  op[i][0]=-1;
                scanf("%d",&k);
                op[i][1]=k;
                f[i]=k;
            }
        }
        sort(f+1,f+1+n);
        m=0;
        match.clear();
        for(i=1;i<=n;i++)
           if(f[i]!=f[i-1])
           {
               m++;
               match[f[i]]=m;
           }
        build(1,1,m);
        for(i=1;i<=n;i++)
        {
            if(op[i][0]==0)
              printf("%I64d\n",tree[1].M[3]);
            else
              update(1,match[op[i][1]],op[i][1],op[i][0]);
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值