洛谷P4588 [TJOI2018]数学计算(线段树)
程序员文章站
2023-12-26 18:14:45
题意 "题目链接" Sol ~~TJOI怎么全是板子题~~ 对时间开个线段树,然后就随便做了。。。。 cpp include using namespace std; const int MAXN = 1e6 + 10; int mod; template inline A mul(A x, B y ......
题意
sol
tjoi怎么全是板子题
对时间开个线段树,然后就随便做了。。。。
#include<bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int mod; template<typename a, typename b> inline a mul(a x, b y) { return 1ll * x * y % mod; } inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int q, a[maxn]; int sum[maxn], val[maxn]; #define ls k << 1 #define rs k << 1 | 1 #define lson ls, l, mid #define rson rs, mid + 1, r void update(int k) { sum[k] = mul(sum[ls], sum[rs]); } void build(int k, int l, int r) { sum[k] = 1; if(l == r) return ; int mid = l + r >> 1; build(lson); build(rson); update(k); } void modify(int k, int l, int r, int p, int v) { if(l == r) {sum[k] = v; return ;} int mid = l + r >> 1; if(p <= mid) modify(lson, p, v); else modify(rson, p, v); update(k); } int query(int k, int l, int r, int ql, int qr) { if(ql > qr) return 1; if(ql <= l && r <= qr) return sum[k]; int mid = l + r >> 1; if(ql > mid) return query(rson, ql, qr); else if(qr <= mid) return query(lson, ql, qr); else return mul(query(lson, ql, qr), query(rson, ql, qr)); } void solve() { q = read(); mod = read(); build(1, 1, q); int x = 1; val[0] = 1; for(int i = 1; i <= q; i++) { int opt = read(); a[i] = read(); if(opt == 1) x = mul(x, a[i]), modify(1, 1, q, i, a[i]); else { modify(1, 1, q, a[i], 1); x = query(1, 1, q, 1, i); } cout << x << '\n'; } } signed main() { // freopen("a.in", "r", stdin); for(int t = read(); t--; solve()); return 0; }