hdu 4031 Attack (splay tree)

本文介绍了一个使用Splay Tree(伸展树)解决特定问题的例子。通过实战演示如何进行区间更新和查询操作,深入探讨了旋转、维护平衡等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很容易想到   成功攻击次数 = 总攻击次数 - 防御次数。  然后把这两个分开处理就OK了

拿过来练习splay tree

#include <cstdio>
#include <cstring>

using namespace std;

const int N = 20007;
#define KT ch[ch[root][1]][0]

int root, ch[N][2], fa[N], sz[N];
int sum[N], num[N], pos[N], def[N], al[N], ar[N];

inline void pushup(int x)
{
	sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
}

inline void pushdown(int x)
{
	if(sum[x])
	{
		sum[ch[x][0]] += sum[x];
		sum[ch[x][1]] += sum[x];
		num[ch[x][0]] += sum[x];
		num[ch[x][1]] += sum[x];
		sum[x] = 0;
	}
}

void build(int l, int r, int f)
{
	if(l > r)
		return;
	int m = (l + r) >> 1;
	fa[m] = f;
	ch[m][0] = m - 1 >= l? (m - 1 + l) >> 1: 0;
	ch[m][1] = r >= m + 1? (r + m + 1) >> 1: 0;
	build(l, m - 1, m);
	build(m + 1, r, m);
	pushup(m);
}

void init(int ans)
{
	memset(num, 0, sizeof(num));
	memset(sum, 0, sizeof(sum));
	sz[0] = 0;
	root = (ans + 1) >> 1;
	build(1, ans, 0);
}

inline void rotate(int x, bool f)
{
	int y = fa[x];
	int z = fa[y];
	pushdown(y);
	pushdown(x);
	ch[y][!f] = ch[x][f];
	fa[ch[x][f]] = y;
	fa[x] = z;
	if(z)
		ch[z][ch[z][1] == y] = x;
	ch[x][f] = y;
	fa[y] = x;
	pushup(y);
}

void splay(int x, int g)
{
	int y = fa[x];
	pushdown(x);
	while(y != g)
	{
		int z = fa[y];
		bool f = (ch[y][0] == x);
		if(z != g && f == (ch[z][0] == y))
			rotate(y, f);
		rotate(x, f);
		y = fa[x];
	}
	pushup(x);
	if(g == 0)
		root = x;
}

void rotateto(int k, int g)
{
	int x = root;
	pushdown(x);
	while(sz[ch[x][0]] != k)
	{
		if(k < sz[ch[x][0]])
			x = ch[x][0];
		else
		{
			k -= sz[ch[x][0]] + 1;
			x = ch[x][1];
		}
		pushdown(x);
	}
	splay(x, g);
}

void update(int a, int b)
{
	rotateto(a - 1, 0);
	rotateto(b + 1, root);
	num[KT] += 1;
	sum[KT] += 1;
}

int que(int a)
{
	rotateto(a - 1, 0);
	rotateto(a + 1, root);
	return num[KT];
}

int main()
{
//	freopen("in.txt", "r", stdin);
	int T, C = 1;
	scanf("%d", &T);
	while(T--)
	{
		int n, m, t;
		scanf("%d%d%d", &n, &m, &t);
		init(n + 2);
		printf("Case %d:\n", C++);
		int ans = 0;
		memset(def, 0, sizeof(def));
		memset(pos, 0 ,sizeof(pos));
		while(m--)
		{
			char s[10];
			int a, b;
			scanf("%s", s);
			if(s[0] == 'A')
			{
				scanf("%d%d", &a, &b);
				update(a, b);
				al[ans] = a;
				ar[ans++] = b;
			}
			else
			{
				scanf("%d", &a);
				if(t == 1)
				{
					puts("0");
					continue;
				}
				for(int i = pos[a]; i < ans; i++)
					if(a >= al[i] && a <= ar[i])
					{
						def[a]++;
						pos[a] = i + t;
						i += t - 1;
					}
				printf("%d\n", que(a) - def[a]);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值