CodeForces 19D Points

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

此题会卡nlog^2n的,选择线段树+平衡树,线段树来定位x的值,平衡树来查找大于y的值。

因为一些原因,我写了三个ac的代码。。

直接调用set的,set底层是红黑树实现的。

#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 2e5 + 50;
int n, x[maxn], y[maxn], f[maxn];
char s[maxn];
int a[maxn], tot, m, b, c;

struct Tree
{
	set<int> f[maxn << 2];
	set<int> ::iterator p;
	int Max[maxn << 2];
	void clear(int x, int l, int r)
	{
		f[x].clear();	Max[x] = -1;
		if (l == r) return;
		int mid = l + r >> 1;
		clear(x << 1, l, mid);
		clear(x << 1 | 1, mid + 1, r);
	}
	void insert(int x, int l, int r, int u, int v, int k)
	{
		if (l == r) { 
			if (k) f[x].insert(v); else f[x].erase(v);
			if (f[x].empty()) Max[x] = -1; else Max[x] = *(--(p = f[x].end()));
			return;
		}
		int mid = l + r >> 1;
		if (u <= mid) insert(x << 1, l, mid, u, v, k);
		else insert(x << 1 | 1, mid + 1, r, u, v, k);
		Max[x] = max(Max[x << 1], Max[x << 1 | 1]);
	}
	void find(int x, int l, int r, int u, int v)
	{
		if (Max[x] <= v) return;
		if (u < l)
		{
			if (l == r)
			{
				b = l;	c = *(f[x].upper_bound(v)); return;
			}
			int mid = l + r >> 1;
			find(x << 1, l, mid, u, v);
			if (b != INF) return;
			find(x << 1 | 1, mid + 1, r, u, v);
		}
		else
		{
			int mid = l + r >> 1;
			if (mid > u) find(x << 1, l, mid, u, v);
			if (b != INF) return;
			find(x << 1 | 1, mid + 1, r, u, v);
		}
	}
}solve;

int main()
{
	while (~scanf("%d", &n))
	{
		for (int i = tot = 0; i < n; i++)
		{
			scanf("%s%d%d", s, &x[i], &y[i]);
			if (s[0] == 'r') f[i] = 0;
			if (s[0] == 'a') f[i] = 1;
			if (s[0] == 'f') f[i] = 2;
			a[tot++] = x[i];
		}
		sort(a, a + tot);	m = unique(a, a + tot) - a;
		solve.clear(1, 0, m);
		for (int i = 0; i < n; i++)
		{
			int X = lower_bound(a, a + m, x[i]) - a;
			if (f[i] == 1) solve.insert(1, 0, m, X, y[i], 1);
			if (f[i] == 0) solve.insert(1, 0, m, X, y[i], 0);
			if (f[i] == 2)
			{
				b = c = INF;
				solve.find(1, 0, m, X, y[i]);
				if (b == INF) printf("-1\n");
				else printf("%d %d\n", a[b], c);
			}
		}
	}
	return 0;
}
数组实现的treap
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int Max = 1e9;
const int low(int x){ return x&-x; }
const int maxn = 4e5 + 50;
int n, x[maxn], y[maxn], f[maxn];
char s[maxn];
int a[maxn], tot, m, b, c;

struct Treaps
{
	const static int maxn = 6e6 + 10;
	int L[maxn], R[maxn], v[maxn], p[maxn], A[maxn], tot;
	void clear(){ A[0] = L[0] = R[0] = 0; tot = 1; }
	int Node(int V, int P){ L[tot] = R[tot] = 0; v[tot] = V; p[tot] = P; A[tot] = 1; return tot++; }
	void rotate_right(int &x)
	{
		int y = L[x]; L[x] = R[y]; R[y] = x; x = y;
	}
	void rotate_left(int &x)
	{
		int y = R[x]; R[x] = L[y]; L[y] = x; x = y;
	}
	void Insert(int& x, int V, int P)
	{
		if (!x) { x = Node(V, P); return; }
		if (v[x] == V) ++A[x];
		else if (V < v[x])
		{
			Insert(L[x], V, P);
			if (p[x]>p[L[x]]) rotate_right(x);
		}
		else
		{
			Insert(R[x], V, P);
			if (p[x] > p[R[x]]) rotate_left(x);
		}
	}
	void add(int &x, int V){ Insert(x, V, rand()); }//外部直接调用,x是树根,v是值
	void Delete(int &x, int V)
	{
		if (!x) return;
		if (V < v[x]) Delete(L[x], V);
		else if (V > v[x]) Delete(R[x], V);
		else if (A[x] > 1) --A[x];
		else if (!L[x] || !R[x]) x = L[x] + R[x];
		else if (p[L[x]] < p[R[x]]){ rotate_right(x); Delete(R[x], V); }
		else { rotate_left(x); Delete(L[x], V); }
	}
	void dec(int &x, int V) { Delete(x, V); }//外部直接调用,x是树根,v是值
	int find(int x, int V)//返回树x中大于V的数字
	{
		int ans = INF;
		for (int i = x; i; i = V < v[i] ? L[i] : R[i])
		{
			if (v[i] > V) ans = min(ans, v[i]);
		}
		return ans;
	}
	int get(int x)
	{
		int ans = -1;
		for (int i = x; i; i = R[i]) ans = v[i];
		return ans;
	}
}treap;

struct Tree
{
	Treaps treap;
	int f[800000], R[800000];
	void clear(int x, int l, int r)
	{
		if (x == 1) treap.clear();
		f[x] = 0;   R[x] = -1;
		if (l == r) return;
		int mid = l + r >> 1;
		clear(x << 1, l, mid);
		clear(x << 1 | 1, mid + 1, r);
	}
	void insert(int x, int l, int r, int u, int v, int k)
	{
		if (l == r) {
			if (k) treap.add(f[x], v); else treap.dec(f[x], v);
			R[x] = treap.get(f[x]);
			return;
		}
		int mid = l + r >> 1;
		if (u <= mid) insert(x << 1, l, mid, u, v, k);
		else insert(x << 1 | 1, mid + 1, r, u, v, k);
		R[x] = max(R[x << 1], R[x << 1 | 1]);
	}
	void find(int x, int l, int r, int u, int v)
	{
		if (R[x] <= v) return;
		if (u < l)
		{
			if (l == r)
			{
				b = l;	c = treap.find(f[x], v); return;
			}
			int mid = l + r >> 1;
			find(x << 1, l, mid, u, v);
			if (b != INF) return;
			find(x << 1 | 1, mid + 1, r, u, v);
		}
		else
		{
			int mid = l + r >> 1;
			if (mid > u) find(x << 1, l, mid, u, v);
			if (b != INF) return;
			find(x << 1 | 1, mid + 1, r, u, v);
		}
	}
}solve;

int main()
{
	while (~scanf("%d", &n))
	{
		for (int i = tot = 0; i < n; i++)
		{
			scanf("%s%d%d", s, &x[i], &y[i]);
			if (s[0] == 'r') f[i] = 0;
			if (s[0] == 'a') f[i] = 1;
			if (s[0] == 'f') f[i] = 2;
			a[tot++] = x[i];
		}
		sort(a, a + tot);	m = unique(a, a + tot) - a;
		solve.clear(1, 0, m);
		for (int i = 0; i < n; i++)
		{
			int X = lower_bound(a, a + m, x[i]) - a;
			if (f[i] == 1) solve.insert(1, 0, m, X, y[i], 1);
			if (f[i] == 0) solve.insert(1, 0, m, X, y[i], 0);
			if (f[i] == 2)
			{
				b = c = INF;
				solve.find(1, 0, m, X, y[i]);
				if (b == INF) printf("-1\n");
				else printf("%d %d\n", a[b], c);
			}
		}
	}
	return 0;
}

动态线段树+treap实现的非离散。
<pre name="code" class="cpp">#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int Max = 1e9;
const int low(int x){ return x&-x; }
const int maxn = 6e6 + 3e5;
int n, x, y, a, b;
char s[maxn];

struct Treaps
{
	int L[maxn], R[maxn], v[maxn], p[maxn], A[maxn], tot;
	void clear(){ A[0] = L[0] = R[0] = 0; tot = 1; }
	int Node(int V, int P){ L[tot] = R[tot] = 0; v[tot] = V; p[tot] = P; A[tot] = 1; return tot++; }
	void rotate_right(int &x)
	{
		int y = L[x]; L[x] = R[y]; R[y] = x; x = y;
	}
	void rotate_left(int &x)
	{
		int y = R[x]; R[x] = L[y]; L[y] = x; x = y;
	}
	void Insert(int& x, int V, int P)
	{
		if (!x) { x = Node(V, P); return; }
		if (v[x] == V) ++A[x];
		else if (V < v[x])
		{
			Insert(L[x], V, P);
			if (p[x]>p[L[x]]) rotate_right(x);
		}
		else
		{
			Insert(R[x], V, P);
			if (p[x] > p[R[x]]) rotate_left(x);
		}
	}
	void add(int &x, int V){ Insert(x, V, rand()); }//外部直接调用,x是树根,v是值
	void Delete(int &x, int V)
	{
		if (!x) return;
		if (V < v[x]) Delete(L[x], V);
		else if (V > v[x]) Delete(R[x], V);
		else if (A[x] > 1) --A[x];
		else if (!L[x] || !R[x]) x = L[x] + R[x];
		else if (p[L[x]] < p[R[x]]){ rotate_right(x); Delete(R[x], V); }
		else { rotate_left(x); Delete(L[x], V); }
	}
	void dec(int &x, int V) { Delete(x, V); }//外部直接调用,x是树根,v是值
	int find(int x, int V)//返回树x中大于V的数字
	{
		int ans = INF;
		for (int i = x; i; i = V < v[i] ? L[i] : R[i])
		{
			if (v[i] > V) ans = min(ans, v[i]);
		}
		return ans;
	}
	int get(int x)
	{
		int ans = -1;
		for (int i = x; i; i = R[i]) ans = v[i];
		return ans;
	}
};

struct Tree
{
	Treaps treap;
	int L[maxn], R[maxn], f[maxn], M[maxn], sz;
	int node(){ L[sz] = R[sz] = f[sz] = 0; M[sz] = -1; return sz++; }
	void clear(){ sz = 0; node(); node(); treap.clear(); }
	void insert(int x, int l, int r, int u, int v, int k)
	{
		if (l == r) {
			if (k) treap.add(f[x], v); else treap.dec(f[x], v);
			M[x] = treap.get(f[x]);
			return;
		}
		int mid = l + r >> 1;
		if (u <= mid)
		{
			if (!L[x]) L[x] = node();
			insert(L[x], l, mid, u, v, k);
		}
		else
		{
			if (!R[x]) R[x] = node();
			insert(R[x], mid + 1, r, u, v, k);
		}
		M[x] = max(M[L[x]], M[R[x]]);
	}
	void find(int x, int l, int r, int u, int v, int &a, int &b)
	{
		if (M[x] <= v) return;
		if (u < l)
		{
			if (l == r)
			{
				a = l;	b = treap.find(f[x], v); return;
			}
			int mid = l + r >> 1;
			find(L[x], l, mid, u, v, a, b);
			if (a != INF) return;
			find(R[x], mid + 1, r, u, v, a, b);
		}
		else
		{
			int mid = l + r >> 1;
			if (mid > u) find(L[x], l, mid, u, v, a, b);
			if (a != INF) return;
			find(R[x], mid + 1, r, u, v, a, b);
		}
	}
}solve;

int main()
{
	while (~scanf("%d", &n))
	{
		solve.clear();
		while (n--)
		{
			scanf("%s%d%d", s, &x, &y);
			if (!strcmp(s, "add")) solve.insert(1, 0, Max, x, y, 1);
			if (!strcmp(s, "remove")) solve.insert(1, 0, Max, x, y, 0);
			if (!strcmp(s, "find"))
			{
				a = b = INF;
				solve.find(1, 0, Max, x, y, a, b);
				if (a == INF) printf("-1\n");
				else printf("%d %d\n", a, b);
			}
		}
	}
	return 0;
}


 
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值