cunzai_zsy0531

关注我

P4588 [TJOI2018]数学计算 题解

题面

考虑把操作时间当作下标建立线段树,每次操作要么是单点修改为 \(x\),要么是修改为 \(1\)。输出答案就是输出线段树根节点的答案。

点击查看代码
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+13;
int mod,pos[N];
struct SegTree{int l,r,mul;}t[N<<2];
#define ls p<<1
#define rs p<<1|1
#define mid ((t[p].l+t[p].r)>>1)
inline void refresh(int p){t[p].mul=1ll*t[ls].mul*t[rs].mul%mod;}
void build(int p,int l,int r){
	t[p].l=l,t[p].r=r,t[p].mul=1;
	if(l==r) return;
	build(ls,l,mid),build(rs,mid+1,r);
}
void update(int p,int x,int k){
	if(t[p].l==t[p].r) return t[p].mul=k,void();
	x<=mid?update(ls,x,k):update(rs,x,k);
	refresh(p);
}
int main(){
	int T;scanf("%d",&T);
	while(T--){
		int cnt=0,q,op,x;
		scanf("%d%d",&q,&mod);
		build(1,1,q);
		for(int i=1;i<=q;++i){
			scanf("%d%d",&op,&x);x%=mod;
			if(op==1) update(1,pos[i]=++cnt,x);
			else update(1,pos[x],1);
			printf("%d\n",t[1].mul);
		}
	}
	return 0;
}
posted @ 2022-05-19 16:24  cunzai_zsy0531  阅读(14)  评论(0编辑  收藏  举报