【题目链接】
【算法】
线段树维护区间乘积
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXQ 100010 struct SegmentTree { int l,r; long long sum; } Tree[MAXQ<<2]; int T,i,q,op,pos; long long p,m; inline void build(int index,int l,int r) { int mid; Tree[index].l = l; Tree[index].r = r; Tree[index].sum = 1; if (l == r) return; mid = (l + r) >> 1; build(index<<1,l,mid); build(index<<1|1,mid+1,r); } inline void update(int index) { Tree[index].sum = (Tree[index<<1].sum * Tree[index<<1|1].sum) % p; } inline void modify(int index,int pos,long long val) { int mid; if (Tree[index].l == Tree[index].r) { Tree[index].sum = val % p; return; } mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= pos) modify(index<<1,pos,val); else modify(index<<1|1,pos,val); update(index); } int main() { scanf("%d",&T); while (T--) { scanf("%d%d",&q,&p); build(1,1,q); for (i = 1; i <= q; i++) { scanf("%d",&op); if (op == 1) { scanf("%lld",&m); modify(1,i,m); printf("%lld ",Tree[1].sum); } else { scanf("%d",&pos); modify(1,pos,1); printf("%lld ",Tree[1].sum); } } } return 0; }