【BZOJ1269】[AHOI2006]文本编辑器editor Splay

本文详细解析了Splay模板题,并提供了一个水化版的代码实现。注意每次移动光标不需要直接Splay到根节点,而是在进行其他操作时再进行Splay。此外,文章还对代码进行了优化,删减了不必要的操作,如第三个操作和第六个询问的代码。

仍然是Splay模板题,水化版 1500 。让我们重写一下 1500 的主函数,然后尽情地往下删除该死的第三个操作和第六个询问的恶心代码!!!

需要注意的是,每一次移动光标并不需要将此处Splay到根节点,在进行其他操作时再Splay即可。

【顺便吐槽此题抄袭NOI2003题严重,不仅题目一样,连题目描述的表格甚至每步操作的名称都基本相同~~】

[AHOI2006]文本编辑器editor C++代码实现:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 2100000
int n;
char a[N],b[10];
struct node
{
	node *ch[2],*fa;
	char v;int s,rev;
	inline void pushdown()
	{
		if(rev)
			ch[0]->reverse(),
			ch[1]->reverse();
		rev=0;
	}
	inline void maintain()
	{
		pushdown();
		s=ch[0]->s+ch[1]->s+1;
	}
	inline bool check()
	{
		return fa->ch[1]==this;
	}
	inline void combine(node *a,int d)
	{
		ch[d]=a;
		a->fa=this;
	}
	inline void reverse()
	{
		rev^=1;
		swap(ch[0],ch[1]);
	}
};
node *null=new node();
node *root=null;
inline node *newnode(node *f,char val)
{
	node *re=new node();
	re->s=1;
	re->v=val;
	re->rev=0;
	re->ch[0]=re->ch[1]=null;
	re->fa=f;
	return re;
}
inline void rotate(node *x,int d)
{
	node *k=x->fa;
	k->ch[d^1]=x->ch[d];
	k->ch[d^1]->fa=k;
	x->ch[d]=k;
	x->fa=k->fa;
	k->fa->ch[k->fa->ch[0]==k?0:1]=x;
	k->fa=x;
	k->maintain();
	x->maintain();
	if(root==k) root=x;
}
inline void splay(node *x,node *aim)
{
	while(x->fa!=aim)
	{
		if(x->fa->fa==aim)
			rotate(x,x->check()?0:1);
		else if(!x->fa->check())
			x->check()?rotate(x,0):rotate(x->fa,1),
			rotate(x,1);
		else
			x->check()?rotate(x->fa,0):rotate(x,1),
			rotate(x,0);
		x->maintain();
	}
}
node *build(int l,int r)
{
	if(l>r) return null;
	int mid=(l+r)>>1;
	node *re=newnode(re,a[mid]);
	re->combine(build(l,mid-1),0);
	re->combine(build(mid+1,r),1);
	re->maintain();
	return re;
}
void del(node* &x)
{
	if(x->ch[0]!=null)
		del(x->ch[0]);
	if(x->ch[1]!=null)
		del(x->ch[1]);
	delete x;
}
node *kth(node *x,int k)
{
	x->pushdown();
	if(k<=x->ch[0]->s)
		return kth(x->ch[0],k);
	k-=x->ch[0]->s;
	if(k==1) return x;
	return kth(x->ch[1],k-1);
}
int main()
{
	cin>>n;
	root=build(0,1);
	root->fa=null;
	for(int x,t=0,i=1;i<=n;i++)
	{
		scanf("%s",b);
		if(b[0]=='M')
			scanf("%d",&t);
		else if(b[0]=='I')
		{
			scanf("%d",&x);
			while((a[0]=getchar())=='\n'||a[0]=='\r');
			if(x>1) gets(a+1);
			splay(kth(root,t+1),null);
			splay(kth(root,t+2),root);
			root->ch[1]->combine(build(0,x-1),0);
			root->ch[1]->maintain();
			root->maintain();
		}
		else if(b[0]=='D')
		{
			scanf("%d",&x);
			splay(kth(root,t+1),null);
			splay(kth(root,t+x+2),root);
			del(root->ch[1]->ch[0]);
			root->ch[1]->ch[0]=null;
			root->ch[1]->maintain();
			root->maintain();
		}
		else if(b[0]=='R')
		{
			scanf("%d",&x);
			splay(kth(root,t+1),null);
			splay(kth(root,t+x+2),root);
			root->ch[1]->ch[0]->reverse();
			root->ch[1]->maintain();
			root->maintain();
		}
		else if(b[0]=='G')
		{
			splay(kth(root,t+2),null);
			printf("%c\n",root->v);
		}
		else if(b[0]=='P') t--;
		else t++;
	}
}


### 题目内容 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体。现在被困在了这个n维球体中,仅知道球面上n + 1个点的坐标,需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁这个球形空间产生器[^1][^2]。 ### 解题思路 - **原理依据**:根据球心的定义,球心到球面上任意一点距离都相等。设球心坐标为\((x_1,x_2,\cdots,x_n)\),球面上一点坐标为\((a_1,a_2,\cdots,a_n)\),两点距离公式为\(dist = \sqrt{(x_1 - a_1)^2+(x_2 - a_2)^2+\cdots+(x_n - a_n)^2}\)。 - **构建方程**:设球面上\(n + 1\)个点的坐标分别为\((a_{i1},a_{i2},\cdots,a_{in})\),\(i = 1,2,\cdots,n + 1\)。以第一个点和其他点为例,根据球心到各点距离相等,可得: \((x_1 - a_{11})^2+(x_2 - a_{12})^2+\cdots+(x_n - a_{1n})^2=(x_1 - a_{21})^2+(x_2 - a_{22})^2+\cdots+(x_n - a_{2n})^2\) 展开式子: \(x_1^2 - 2a_{11}x_1+a_{11}^2+x_2^2 - 2a_{12}x_2+a_{12}^2+\cdots+x_n^2 - 2a_{1n}x_n+a_{1n}^2=x_1^2 - 2a_{21}x_1+a_{21}^2+x_2^2 - 2a_{22}x_2+a_{22}^2+\cdots+x_n^2 - 2a_{2n}x_n+a_{2n}^2\) 消去\(x_1^2,x_2^2,\cdots,x_n^2\)后可得: \(2(a_{21}-a_{11})x_1 + 2(a_{22}-a_{12})x_2+\cdots+2(a_{2n}-a_{1n})x_n=a_{21}^2 - a_{11}^2+a_{22}^2 - a_{12}^2+\cdots+a_{2n}^2 - a_{1n}^2\) 同理,用第一个点和第\(i\)个点\((i = 3,\cdots,n + 1)\)可得到\(n\)个线性方程,构成一个\(n\)元一次方程组。 - **求解方程组**:使用高斯消元法求解这个\(n\)元一次方程组,得到的解就是球心的\(n\)维坐标。 ### 代码实现 ```python n = int(input()) points = [] for _ in range(n + 1): points.append(list(map(float, input().split()))) # 构建方程组的系数矩阵和常数项 a = [[0] * (n + 1) for _ in range(n)] b = [0] * n for i in range(n): for j in range(n): a[i][j] = 2 * (points[i + 1][j] - points[0][j]) b[i] += points[i + 1][j] ** 2 - points[0][j] ** 2 # 高斯消元 for i in range(n): # 选主元 max_row = i for j in range(i + 1, n): if abs(a[j][i]) > abs(a[max_row][i]): max_row = j a[i], a[max_row] = a[max_row], a[i] b[i], b[max_row] = b[max_row], b[i] # 消元 for j in range(i + 1, n): factor = a[j][i] / a[i][i] for k in range(i, n): a[j][k] -= factor * a[i][k] b[j] -= factor * b[i] # 回代求解 x = [0] * n for i in range(n - 1, -1, -1): s = 0 for j in range(i + 1, n): s += a[i][j] * x[j] x[i] = (b[i] - s) / a[i][i] # 输出结果 print(" ".join(map(lambda num: "{:.3f}".format(num), x))) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值