# NOI模拟（5.21） TJOID1T1 数学计算 （bzoj5334）

5.21 模拟 TJOI2018D1T1

Source:

/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <deque>
#include <iterator>
#include <map>

static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}

// /*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}

template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}

inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}

const int MAXN = 100000 + 10;

int n, x, t, mod, type;
int v[MAXN], tree[MAXN << 2 | 1];

inline void push_down(int k) {
if (tree[k] != 1) {
tree[k << 1] = (long long)tree[k << 1] * tree[k] % mod;
tree[k << 1 | 1] = (long long)tree[k << 1 | 1] * tree[k] % mod;
tree[k] = 1;
}
}

inline void build(int k, int l, int r) {
tree[k] = 1;
if (l == r) return ;
int mid = l + r >> 1;
build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
}

inline void modify(int k, int l, int r, int ql, int qr, int x) {
if (ql <= l && r <= qr)
return (void)(tree[k] = (long long)tree[k] * x % mod);
int mid = l + r >> 1;
push_down(k);
if (ql <= mid) modify(k << 1, l, mid, ql, qr, x);
if (qr > mid) modify(k << 1 | 1, mid + 1, r, ql, qr, x);
}

inline void get_ans(int k, int l, int r) {
if (l == r) return W(tree[k]), write_char('\n');
int mid = l + r >> 1;
push_down(k);
get_ans(k << 1, l, mid), get_ans(k << 1 | 1, mid + 1, r);

}

inline void solve() {
R(n), R(mod), build(1, 1, n);
for (int i = 1; i <= n; ++i) {
R(type), v[i] = 1;
if (type == 1) R(v[i]);
else R(x), modify(1, 1, n, x, i - 1, v[x]), v[x] = 1;
}
for (int i = 1; i <= n; ++i)
if (v[i] != 1) modify(1, 1, n, i, n, v[i]), v[i] = 1;
get_ans(1, 1, n);
}

int main() {
freopen("cal.in", "r", stdin);
freopen("cal.out", "w", stdout);
R(t);
while (t--) solve();
flush();
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120