[BZOJ1798]AHOI2009 维护序列|线段树|splay

水题,线段树就可以搞定,我又去写splay作死了。。注意的是这里的模数比较大,要开long long,另外要注意标记下放的优先,在打乘法标记的时候加法标记也要乘,要先下放乘法标记。。

#include<iostream>
#include<cstdio>
#include<memory.h>
#define ll long long
#define maxn 100005
using namespace std;
int i,n,m,nd=0,root=0,l,r,t,c[maxn][2],size[maxn],pre[maxn],key[maxn];
ll p,k,num[maxn],mult[maxn],addt[maxn],sum[maxn],a[maxn];
void newnode(int &x,int fa,ll k)
{
	x=++nd;pre[x]=fa;num[x]=sum[x]=k;
	c[x][0]=c[x][1]=addt[x]=0;
	size[x]=mult[x]=1;
}
void mul(int x,ll k)
{
	if (!x) return;
	mult[x]=(mult[x]*k)%p;addt[x]=(addt[x]*k)%p;
	sum[x]=(sum[x]*k)%p;num[x]=(num[x]*k)%p;
}
void add(int x,ll k)
{
	if (!x) return;
	addt[x]=(addt[x]+k)%p;num[x]=(num[x]+k)%p;
	sum[x]=(sum[x]+((ll)size[x]*k)%p)%p;
}
void down(int x)
{
	if (mult[x]!=1) mul(c[x][0],mult[x]),mul(c[x][1],mult[x]),mult[x]=1;
	if (addt[x]) add(c[x][0],addt[x]),add(c[x][1],addt[x]),addt[x]=0;
}
void update(int x)
{
	down(x);
	size[x]=size[c[x][0]]+size[c[x][1]]+1;
	sum[x]=(sum[c[x][0]]+sum[c[x][1]]+num[x])%p;
}
void rot(int x,int kind)
{
	int y=pre[x],z=pre[y];down(y);down(x);
	c[y][!kind]=c[x][kind];pre[c[x][kind]]=y;
	c[x][kind]=y;pre[y]=x;
	pre[x]=z;if (z) c[z][c[z][1]==y]=x;
	update(y);update(x);if (z) update(z);
}
void splay(int x,int goal)
{
	int y,z,kind;
	while (pre[x]!=goal)
	{
		y=pre[x];
		if (pre[y]==goal) rot(x,c[y][0]==x);
		else
		{
			z=pre[y];kind=c[z][0]==y;
			if (c[y][kind]==x) rot(x,!kind);else rot(y,kind);rot(x,kind);
		}
	}
	if (goal==0) root=x;
}
void build(int &x,int fa,int l,int r)
{
	if (l>r) return;
	int mid=(l+r)/2;
	newnode(x,fa,a[mid]);
	if (l==r) return;
	build(c[x][0],x,l,mid-1);build(c[x][1],x,mid+1,r);
	update(x);
}
int findkth(int x,int k)
{
	if (size[c[x][0]]>=k) return findkth(c[x][0],k);
	if (size[c[x][0]]+1==k) return x;
	return findkth(c[x][1],k-size[c[x][0]]-1);
}
void makemul(int l,int r,ll k)
{
	splay(findkth(root,l),0);splay(findkth(root,r+2),root);
	int x=c[c[root][1]][0];
	mul(x,k);
}
void makeadd(int l,int r,ll k)
{
	splay(findkth(root,l),0);splay(findkth(root,r+2),root);
	int x=c[c[root][1]][0];
	add(x,k);
}
void print(int x)
{
	if (!x) return;
	print(c[x][0]);
	printf("%d:pre->%d size->%d c1->%d c2->%d sum->%I64d at->%I64d mt->%I64d num->%I64d\n",x,pre[x],size[x],c[x][0],c[x][1],sum[x],addt[x],mult[x],num[x]);
	print(c[x][1]);
}
ll query(int l,int r)
{
	splay(findkth(root,l),0);splay(findkth(root,r+2),root);
	int x=c[c[root][1]][0];update(x);
	return sum[x];
}
int main()
{
	freopen("1798.in","r",stdin);
	cin>>n>>p;
	for (i=1;i<=n;i++) scanf("%I64d",&a[i]);
	cin>>m;
	newnode(root,0,0);newnode(c[root][1],root,0);build(c[c[root][1]][0],c[root][1],1,n);
	size[0]=pre[0]=num[0]=sum[0]=addt[0]=0;mult[0]=1;
	for (i=1;i<=m;i++)
	{
		scanf("%d%d%d",&t,&l,&r);if (t==1||t==2) scanf("%I64d",&k);
	//	print(root);cout<<endl;
		switch (t)
		{
			case 1:makemul(l,r,k);break;
			case 2:makeadd(l,r,k);break;
			case 3:printf("%I64d\n",query(l,r));break;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值