fork(1) download
  1. #include<vector>
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. const ll MOD = 998244353;
  7.  
  8. // 計算模逆元的函數
  9. ll modInverse(ll a, ll mod) {
  10. ll m0 = mod, y = 0, x = 1;
  11. if (mod == 1) return 0;
  12. while (a > 1) {
  13. ll q = a / mod;
  14. ll t = mod;
  15. mod = a % mod, a = t;
  16. t = y;
  17. y = x - q * y;
  18. x = t;
  19. }
  20. if (x < 0) x += m0;
  21. return x;
  22. }
  23.  
  24. int main(){
  25. int t;
  26. cin >> t;
  27. while(t--){
  28. ll n, q, ans = 1;
  29. cin >> n >> q;
  30. vector<int> a(n), b(n), c(n), d(n);
  31. for(int i = 0; i < n; i++){
  32. cin >> a[i];
  33. c[i] = a[i];
  34. }
  35. for(int i = 0; i < n; i++){
  36. cin >> b[i];
  37. d[i] = b[i];
  38. }
  39. sort(a.begin(), a.end());
  40. sort(b.begin(), b.end());
  41. for(int i = 0; i < n; i++){
  42. ans = (ans * min(a[i], b[i])) % MOD;
  43. }
  44. cout << ans << " ";
  45. while(q--){
  46. int o, x;
  47. cin >> o >> x;
  48. x--;
  49. if(o == 1){
  50. auto it = upper_bound(a.begin(), a.end(), c[x]) - 1 - a.begin();
  51. c[x]++;
  52. ll index = min(a[it], b[it]);
  53. a[it]++;
  54. ans = (ans * min(a[it], b[it])) % MOD;
  55. ans = (ans * modInverse(index, MOD)) % MOD;
  56. }
  57. if(o == 2){
  58. auto it = upper_bound(b.begin(), b.end(), d[x]) - 1 - b.begin();
  59. d[x]++;
  60. ll index = min(a[it], b[it]);
  61. b[it]++;
  62. ans = (ans * min(a[it], b[it])) % MOD;
  63. ans = (ans * modInverse(index, MOD)) % MOD;
  64. }
  65. cout << ans % MOD << " ";
  66. }
  67. cout << endl;
  68. }
  69. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
3 4
1 1 2
3 2 1
1 3
2 3
1 1
2 1
6 8
1 4 2 7 3 5
7 6 5 6 3 3
2 5
1 6
1 5
1 5
1 5
2 3
2 3
1 6
13 8
7 7 6 6 5 5 5 2 2 3 4 5 1
1 4 1 9 6 6 9 1 5 1 3 8 4
2 2
2 11
2 4
2 4
1 7
1 1
2 12
1 5
5 3
10000000 20000000 30000000 40000000 50000000
10000000 20000000 30000000 40000000 50000000
1 1
2 2
2 1
stdout
2 3 3 6 6 
840 840 1008 1344 1680 2016 2016 2016 2352 
2116800 2646000 3528000 3528000 3528000 4233600 4838400 4838400 4838400 
205272023 205272023 205272023 264129429