题目大意:有一个数$x$和取模的数$mod$,初始为$1$,有两个操作:
- $m:x=x imes m$并输出$x\% mod$
- $pos:x=x/第pos次操作乘的数$(保证合法),并输出$x\%mod$
题解:对时间建一棵线段树,记录区间积就可以了
卡点:无
C++ Code:
#include <cstdio> #define maxn 100010 int Tim, n, mod; long long V[maxn << 2]; void build(int rt, int l, int r) { V[rt] = 1; if (l == r) return ; int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); } void add(int rt, int l, int r, int p, int num) { if (l == r) { V[rt] = num % mod; return ; } int mid = l + r >> 1; if (p <= mid) add(rt << 1, l, mid, p, num); else add(rt << 1 | 1, mid + 1, r, p, num); V[rt] = V[rt << 1] * V[rt << 1 | 1] % mod; } int main() { scanf("%d", &Tim); while (Tim --> 0) { scanf("%d%d", &n, &mod); build(1, 1, n); for (int i = 1; i <= n; i++) { int op, x; scanf("%d%d", &op, &x); if (op == 1) add(1, 1, n, i, x); else add(1, 1, n, x, 1); printf("%lld ", V[1]); } } return 0; }