1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10;
struct opt { int f, x; }o[N];
int block[1000 + 5][2000 + 5], p[1000 + 5], ans[1000 + 5]; int arr[N]; int n, q, m, lenBlock;
int id(int x) { return lower_bound(arr, arr + q, x) - arr; }
void build() { memset(p, 0, sizeof p); for (int i = 0; i < n; i++) ans[i] = 1; }
void process(int x) { ans[x] = 1; for (int i = 0; i < p[x]; i++) ans[x] = ans[x] * 1ll * block[x][i] % m; }
int getAns() { int res = 1; for (int i = 0; i < n; i++) res = res * 1ll * ans[i] % m; return res; }
void add(int x) { int pos = id(x), i = pos / lenBlock; block[i][p[i]++] = x; ans[i] = ans[i] * 1ll * x % m; }
void del(int x) { int pos = id(x), i = pos / lenBlock; for (int j = 0; j < p[i]; j++) if (block[i][j] == x){ block[i][j] = 1; break; } process(i); }
int main() { int T; cin >> T; for (int kase = 1; kase <= T; kase++) { cin >> q >> m; for (int i = 0; i < q; i++) { scanf("%d %d", &o[i].f, &o[i].x); arr[i] = o[i].x; } sort(arr, arr + q); lenBlock = sqrt(q + 0.5); n = ceil((q + 0.0) / lenBlock); build(); printf("Case #%d:\n", kase); int res = 1; for (int i = 0; i < q; i++) { if (o[i].f == 1) add(o[i].x), res = res * 1ll * o[i].x % m; else del(o[o[i].x - 1].x), res = getAns(); printf("%d\n", res); } } }
|