【BZOJ1862】[Zjoi2006]GameZ游戏排名系统【Splay】【Hash】

7 篇文章 0 订阅

【题目链接】

【BZOJ1862】

/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 1000005;
const LL inf = 1LL << 60;

int son[maxn][2], pre[maxn], size[maxn];
string id[maxn];
LL val[maxn];
int tot1, tot2, sta[maxn], root;
map<string, int> no;

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void newnode(int &x, int f, LL c, string &s) {
	x = tot2 ? sta[tot2--] : ++tot1;
	son[x][0] = son[x][1] = 0;
	pre[x] = f;
	size[x] = 1;
	val[x] = c;
	id[x] = s;
	no[s] = x;
}

inline void pushup(int x) {
	int l = son[x][0], r = son[x][1];
	size[x] = size[l] + size[r] + 1;
}

inline void init() {
	tot1 = tot2 = root = 0;
	son[0][0] = son[0][1] = pre[0] = size[0] = val[0] = 0;
	string s;
	newnode(root, 0, -inf, s);
	newnode(son[root][1], root, inf, s);
	pushup(son[root][1]); pushup(root);
}

inline void rotate(int x) {
	int y = pre[x], z = pre[y], type = son[y][1] == x;
	pre[son[y][type] = son[x][!type]] = y;
	pre[x] = z;
	if(z) son[z][son[z][1] == y] = x;
	pre[son[x][!type] = y] = x;
	pushup(y); pushup(x);
}

inline void splay(int x, int goal) {
	while(pre[x] ^ goal) {
		int y = pre[x], z = pre[y];
		if(z == goal) rotate(x);
		else if(son[z][1] == y ^ son[y][1] == x) rotate(x), rotate(x);
		else rotate(y), rotate(x);
	}
	if(!goal) root = x;
}

inline int getpre(int x) {
	for(; son[x][1]; x = son[x][1]);
	return x;
}

inline int getsuf(int x) {
	for(; son[x][0]; x = son[x][0]);
	return x;
}

inline int find(int k) {
	int x = root;
	while(x) {
		if(k == size[son[x][0]]) return x;
		else if(k < size[son[x][0]]) x = son[x][0];	
		else k -= size[son[x][0]] + 1, x = son[x][1];
	}
}

inline void del(int x) {
	splay(x, 0);
	int a = getpre(son[x][0]), b = getsuf(son[x][1]);
	splay(a, 0); splay(b, a);
	sta[++tot2] = son[b][0];
	pre[son[b][0]] = son[b][0] = 0;
	pushup(b); pushup(a);
}

inline void insert(string &s, LL c) {
	int x = root;
	for(; son[x][c > val[x]]; x = son[x][c > val[x]]);
	newnode(son[x][c > val[x]], x, c, s);
	splay(son[x][c > val[x]], 0);
}

inline int query(string &s) {
	int t = no[s];
	splay(t, 0);
	return size[son[t][1]];
}

int cnt;

inline void dfs(int x) {
	if(!x) return;
	dfs(son[x][1]);
	cnt--;
	cout << id[x];
	if(cnt) cout << " ";
	dfs(son[x][0]);
}

inline void print(int st) {
	st = size[root] - 2 - st + 1;
	int ed = max(st - 9, 1);
	swap(st, ed);
	int x = find(st - 1), y = find(ed + 1);
	splay(x, 0); splay(y, x);
	cnt = ed - st + 1;
	dfs(son[y][0]);
	printf("\n");
}

int main() {
	init();
	for(int T = iread(); T; T--) {
		char opt[20]; scanf("%s", opt);
		if(opt[0] == '+') {
			LL c; scanf("%lld", &c);
			string s = opt + 1;
			int t = no[s];
			if(t) del(t);
			insert(s, c);
		}
		else if(opt[0] == '?' && opt[1] >= 'A' && opt[1] <= 'Z') {
			string s = opt + 1;
			printf("%d\n", query(s));
		}
		else if(opt[0] == '?' && opt[1] >= '0' && opt[1] <= '9') {
			int st; sscanf(opt + 1, "%d", &st);
			print(st);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 有一个 $n$ 个点的棋盘,每个点上有一个数字 $a_i$,你需要从 $(1,1)$ 走到 $(n,n)$,每次只能往右或往下走,每个格子只能经过一次,路径上的数字和为 $S$。定义一个点 $(x,y)$ 的权值为 $a_x+a_y$,求所有满足条件的路径中,所有点的权值和的最小值。 输入格式 第一行一个整数 $n$。 接下来 $n$ 行,每行 $n$ 个整数,表示棋盘上每个点的数字。 输出格式 输出一个整数,表示所有满足条件的路径中,所有点的权值和的最小值。 数据范围 $1\leq n\leq 300$ 输入样例 3 1 2 3 4 5 6 7 8 9 输出样例 25 算法1 (树形dp) $O(n^3)$ 我们可以先将所有点的权值求出来,然后将其看作是一个有权值的图,问题就转化为了在这个图中求从 $(1,1)$ 到 $(n,n)$ 的所有路径中,所有点的权值和的最小值。 我们可以使用树形dp来解决这个问题,具体来说,我们可以将这个图看作是一棵树,每个点的父节点是它的前驱或者后继,然后我们从根节点开始,依次向下遍历,对于每个节点,我们可以考虑它的两个儿子,如果它的两个儿子都被遍历过了,那么我们就可以计算出从它的左儿子到它的右儿子的路径中,所有点的权值和的最小值,然后再将这个值加上当前节点的权值,就可以得到从根节点到当前节点的路径中,所有点的权值和的最小值。 时间复杂度 树形dp的时间复杂度是 $O(n^3)$。 C++ 代码 算法2 (动态规划) $O(n^3)$ 我们可以使用动态规划来解决这个问题,具体来说,我们可以定义 $f(i,j,s)$ 表示从 $(1,1)$ 到 $(i,j)$ 的所有路径中,所有点的权值和为 $s$ 的最小值,那么我们就可以得到如下的状态转移方程: $$ f(i,j,s)=\min\{f(i-1,j,s-a_{i,j}),f(i,j-1,s-a_{i,j})\} $$ 其中 $a_{i,j}$ 表示点 $(i,j)$ 的权值。 时间复杂度 动态规划的时间复杂度是 $O(n^3)$。 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值