洛谷4588 数学计算(线段树)

QAQ

【题目分析】

思路妙妙妙啊。

现在我们有q个叶子节点,权值为1的线段树,对于每次1操作,相当于将第i个叶子节点的值改为val,每次2操作就是将之前某叶节点权值改为1,最后输出根节点的权值即可。

其实还算比较氵的题了。。。。

【代码~】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN=1e5+10;

LL T;
LL n,mod;
struct Tree{
	LL l,r;
	LL mul;
}tr[MAXN<<2];

void push_up(LL root)
{
	tr[root].mul=tr[root<<1].mul*tr[root<<1|1].mul%mod;
}

void build(LL root,LL l,LL r)
{
	tr[root].l=l;
	tr[root].r=r;
	tr[root].mul=1;
	if(l==r)
	  return ;
	LL mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
}

void update1(LL root,LL l,LL r,LL x,LL key)
{
	if(l==r)
	{
		tr[root].mul=key;
		return ;
	}
	LL mid=l+r>>1;
	if(x<=mid)
	  update1(root<<1,l,mid,x,key);
	else
	  update1(root<<1|1,mid+1,r,x,key);
	push_up(root);
}

void update2(LL root,LL l,LL r,LL x)
{
	if(l==r)
	{
	    tr[root].mul=1;
	    return ;
	}
	LL mid=l+r>>1;
	if(x<=mid)
	  update2(root<<1,l,mid,x);
	else
	  update2(root<<1|1,mid+1,r,x);
	push_up(root);
}

LL Read()
{
	LL i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void sc(LL x)
{
	if(x>=10)
	  sc(x/10);
	putchar(x%10+48);
}

int main()
{
	T=Read();
	while(T--)
	{
		n=Read(),mod=Read();
		build(1,1,n);
		for(LL i=1;i<=n;++i)
		{
			LL cz=Read();
			if(cz==1)
			{
				LL val=Read();
			    update1(1,1,n,i,val);
			}
			else
			{
				LL id=Read();
				update2(1,1,n,id);
			}
			sc(tr[1].mul),puts("");
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值