线段树
线段树维护每次操作的数,撤销就把那个位置变成1就好了。
代码:
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
#define F inline
using namespace std;
typedef long long LL;
struct tree{ int l,r; LL x; }t[N<<2];
int q; LL p;
F char readc(){
static char buf[100000],*l=buf,*r=buf;
if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
return l==r?EOF:*l++;
}
F LL _read(){
LL x=0; char ch=readc();
while (!isdigit(ch)) ch=readc();
while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
return x;
}
F void pshp(int x){ t[x].x=t[x<<1].x*t[x<<1|1].x%p; }
F void build(int x,int l,int r){
t[x]=(tree){l,r,1};
if (l==r) return; int mid=l+r>>1;
build(x<<1,l,mid),build(x<<1|1,mid+1,r);
}
F void mdfy(int x,int p,LL w){
if (t[x].l==t[x].r) return void(t[x].x=w);
mdfy(x<<1|(p>t[x].l+t[x].r>>1),p,w),pshp(x);
}
int main(){
for (int T=_read();T;T--){
q=_read(),p=_read(),build(1,1,q);
for (int i=1;i<=q;i++){
int f=_read(),x=_read();
f==1?mdfy(1,i,x):mdfy(1,x,1);
printf("%lld\n",t[1].x);
}
}
return 0;
}