题解 P4588 【[TJOI2018]数学计算】
\color{green}{\text{思维题——洛谷P4588\ \ \ \ \ [TJOI2018]数学计算}}
const int N=1e5+100;
#define ll long long
ll mod;int tot,G[N];
int test_number,q;
struct Segment_tree{
ll sum[N<<2];//记得4倍空间
inline void pushup(int o){
sum[o]=sum[o<<1]*sum[o<<1|1]%mod;
}
inline void build(int o,int l,int r){
if (l==r){sum[o]=1ll;return;}
register int mid=(l+r)>>1;
build(o<<1|1,mid+1,r);
build(o<<1,l,mid);
pushup(o);return;
}
void updata(int o,int l,int r,int p,ll v){
if (l==r){sum[o]=v;return;}
register int mid=(l+r)>>1;
if (p<=mid) updata(o<<1,l,mid,p,v);
else updata(o<<1|1,mid+1,r,p,v);
pushup(o);return;
}
}SGT;
#define gc getchar()
#define g(c) isdigit(c)
inline ll read(){
char c=0;ll x=0;bool f=0;
while (!g(c)) f=c=='-',c=gc;
while (g(c)) x=x*10+c-48,c=gc;
return f?-x:x;
}
namespace fast_write{
void write(ll a,bool b){
if (a==0){
if (b) putchar('0');
}
else{
write(a/10,false);
putchar(a%10+'0');
}
}
void print(ll a,char c){
write(a,true);
putchar(c);
}
}
int main(){
test_number=read();
while (test_number--){
q=read();mod=read();
SGT.build(1,1,q);tot=0;
memset(G,0,sizeof(G));
for(int i=1;i<=q;i++){
int opt=read();ll t=read();
if (opt==2) SGT.updata(1,1,q,G[t],1);
else SGT.updata(1,1,q,G[i]=(++tot),t%mod);
fast_write::print(SGT.sum[1]%mod,'\n');
}
}
return 0;
}
祝笔者和大家都可以 AK IOI!