CodeForces 19D Points(线段树好题)

这题想了挺久的,虽然一直有线段树的感觉,但就是不知道怎么维护,网上的文章真的是千篇一律,都是一个大佬的模板,解释也就胡乱说一通骗个阅读量,有的题解真的是牛头不对马嘴。只能靠反复去解读代码来获得思路。现在想对这题的思路做个具体讲解,算是给自己提个醒,也让能看到的人少走点弯路吧。

题目链接:CodeForces 19D Points

D. Points

Time Limit: 1 Sec Memory Limit: 256 MB

  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.

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

题目大意:

给你n组查询,一次查询分别有一个字符串辨认操作,两个数字x和y代表坐标,add代表添加该坐标,remove删除该坐标,find询问目前x和y都大于该点的最近坐标,也就是在第一象限,且不存在于x轴和y轴的坐标。距离定义为优先x最近,x相同时查找最近的y,若没有输出-1。

思路:

这题有人最开始应该会想到二分查找啊,这的确有些涉及,但不是主角,题目里有个很明显的问题,就是添加,删除。这时候差不多就要往线段树方面去考虑了。

首先离线存储所有的询问,存储过程中额外将所有出现过的x点加入Treeset,目的是去重+离散化。

然后我将去重+离散完x后的Treeset跑了一遍存入一个数组,我们先称这个数组为sz[]吧。这步操作是因为我待会需要二分查找一个数的索引值,来更新线段树,不过似乎set也有类库能够实现。

我们按照操作完后的x来构建线段树,我们默认值为-1。也就是没有出现过以该x为坐标的点,而线段数维护的是区间内y的最大值。同时我们要建一个set数组,每个set数组的下标对应x的下标,存储该x轴中所有坐标的y值。

这里节点维护的是y的最大值的目的是之后查找树时能确定存在符合要求的点,这句话具体可以配合下面阅读。

下面我们称当前询问的x为fx,y为fy。

当我们每次add时,添加set数组的值,然后维护一遍线段树,remove也一样操作,具体操作查看代码。查找是这样做的,查找fx在sz[]中的下标+1到末尾这块区间,如果不存在大于fy的值,则返还-1,输出NO。否则,按照根-左-右的顺序往下查找,返还区间内最靠左的符合要求的x下标,我们叫做Q吧。之后只需要遍历下标为Q的set数组,找到最小的Y坐标就可以了。
如果还有不懂的,我代码写了注释,可以配合代码仔细阅读。

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;


public class Main
{
	static int tree[],sz[];
	static TreeSet<Integer>[] p;
	static void add(int l,int r,int k,int x)
	{
		if (l==r)
		{
			tree[k]=p[x].size()==0 ? -1 : p[l].last();//如果该x轴不存在坐标,写为-1,否则取set中最大的y
			return;
		}
		int m=(l+r)>>1;
		if (x<=m)add(l,m,k<<1,x);
		else add(m+1,r,k<<1|1,x);
		tree[k]=Math.max(tree[k<<1], tree[k<<1|1]);
	}
	static int find(int l,int r,int k,int a,int b,int value)
	{
		if (tree[k]<=value || a>b)return -1;//a可能会大于b,仔细思考一下就知道
		if (l==r)return l;//返还索引
		int m=(l+r)>>1;
		int t=-1;
		if (a<=m)t=find(l,m,k<<1,a,b,value);
		if (t!=-1)return t;//如果你左边这块区间已经找到了,那么就不用搜右边了,因为最符合要求的x一定在左边
		return find(m+1,r,k<<1|1,a,b,value);//不然就向右查找
	}
	static int fen(int value)//二分查找该值在sz[]中的下标
	{
		int l=1,r=o;
		while (l<r)
		{
			int m=(l+r)>>1;
			if (sz[m]>=value)
			{
				r=m;
			}
			else l=m+1;
		}
		return l;
	}
	static int o;
	public static void main(String[] args) throws IOException
	{
		Scanner sc=new Scanner(System.in);
		int n=ini();
		String s[]=new String [n+1];
		int x[]=new int [n+1];
		int y[]=new int [n+1];
		TreeSet<Integer> set=new TreeSet<>();
		sz=new int [n+1];
		o=0;
		for (int i=1;i<=n;i++)
		{
			s[i]=ins();
			x[i]=ini();
			y[i]=ini();
			set.add(x[i]);
		}
		for (Integer w:set)
		{
			sz[++o]=w;
		}//对x去重+离散,存入sz[]
		p=new TreeSet [o+1];//存每个x轴中的所有坐标的y点
		for (int i=1;i<=o;i++)
		{
			p[i]=new TreeSet<Integer>();
		}
		tree=new int [o<<2];
		Arrays.fill(tree, -1);//初始化全没有
		for (int i=1;i<=n;i++)
		{
			int l=fen(x[i]);//该x在sz[]中的索引
			if (s[i].equals("add"))
			{
				p[l].add(y[i]);//该x轴中添加y
				add(1,o,1,l);//维护一遍线段树
			}
			else if (s[i].equals("remove"))
			{
				p[l].remove(y[i]);
				add(1,o,1,l);
			}
			else
			{
				int q=find(1,o,1,l+1,o,y[i]);//查找符合要求的最小的x下标,不存在输出-1;
				if (q==-1)
				{
					out.println(-1);
					continue;
				}
				out.print(sz[q]+" ");
				for (Integer w:p[q])//遍历q轴所有的y,找到最符合要求的一个
				{
					if (w>y[i])
					{
						out.println(w);
						break;
					}
				}
				
			}
		}
		
		
		out.flush();
	}

/*

7
add 1 1
add 3 4
find 0 0
remove 1 1
find 0 0
add 1 1
find 0 0

*/
	
	static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	static int ini() throws IOException
	{
		in.nextToken();
		return (int)in.nval;
	}
	static String ins() throws IOException
	{
		in.nextToken();
		return in.sval;
	}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值