HDU - 5517 Triple 二維樹狀數組

題意:有二元組(a,b),三元組(c,d,e)。當b == e時它們能構成(a,c,d)。 

然後,當不存在 [ (u,v,w)!=(a,b,c)且u>=a,v>=b,w>=c ]時,則是一個better集合裏的元素。 

問這個better集合有幾個元素.

思路:首先能確定的是每一個原來的三元組最多構造一個新的三元組,因爲當b == e時,我們只能選所有(a, b)當中a最大的那個(選其他的沒有意義),然後問題是怎麼判斷一個新的三元組是不是better集合裏的元素,我們可以按照

(a, c, d)的優先級排序,然後逆序逐個將(c,d)插入二維樹狀數組當中,並且每次插入之前更新答案就好了,將(c,d)看成平面座標,那麼二維樹狀數組就可以看成用來維護平面上在一個點右下方的點的數量。

代碼:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int N = 1010;
int bit[N + 10][N + 10];
struct node{
	int x, y, z, num;
	node(){}
	node(int _x, int _y, int _z, int _num) : x(_x), y(_y), z(_z), num(_num) {}
	bool operator < (node a) const
	{
		if(x != a.x) return x < a.x;
		if(y != a.y) return y < a.y;
		return z < a.z;
	}
	bool operator == (node a) const
	{
		return (x == a.x && y == a.y && z == a.z);
	}
}p[MAXN];
int w[MAXN], cnt[MAXN];
int top;
void add(int i, int j, int x)
{
	for(int u = i; u <= N; u += u & -u)
	for(int v = j; v <= N; v += v & -v)
	bit[u][v] += x;
}
int sum(int i, int j)
{
	int res = 0;
	for(int u = i; u; u -= u & -u)
	for(int v = j; v; v -= v & -v)
	res += bit[u][v];
	return res;
}
int query(int x1, int y1, int x2, int y2)
{
	return sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1);
}
int solve()
{
	memset(bit, 0, sizeof(bit));
	int ans = 0;
	for(int i = top; i >= 0; i--) //注意是逆序處理
	{
		if(query(p[i].y, p[i].z, N, N) == 0) ans += p[i].num;
		add(p[i].y, p[i].z, 1);
	}
	return ans;
}
int main()
{
	int T, n, m, a, b, c;
	cin >> T;
	for(int kase = 1; kase <= T; kase++)
	{
		memset(w, 0, sizeof(w));
		memset(cnt, 0, sizeof(cnt));
		scanf("%d %d", &n, &m);
		for(int i = 1; i <= n; i++)
		{
			scanf("%d %d", &a, &b);
			if(w[b] < a) w[b] = a, cnt[b] = 1;
			else if(w[b] == a) cnt[b]++;
		}
		top = 0;
		for(int i = 1; i <= m; i++)
		{
			scanf("%d %d %d", &a, &b, &c);
			if(w[c]) p[top++] = node(w[c], a, b, cnt[c]);
		}
		sort(p, p + top);
		int tmp = top; top = 0;
		for(int i = 1; i < tmp; i++)
		{
			if(p[top] == p[i]) p[top].num += p[i].num;
			else p[++top] = p[i];
		}
		printf("Case #%d: %d\n", kase, solve());
	}
 	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章