洛谷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;
}

 

posted @ 2018-10-23 21:30  Ishtar~  阅读(120)  评论(0编辑  收藏  举报