[关闭]
@M1saki 2017-08-21T03:01:16.000000Z 字数 2458 阅读 853

hdu6155

acm hdu


  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define INF 0x7fffffff
  4. #define mem0(x) memset(x, 0, sizeof(x))
  5. #define mem1(x) memset(x, -1, sizeof(x))
  6. #define all(x) x.begin(), x.end()
  7. #define sz(x) x.size()
  8. #define pb push_back
  9. #define mp make_pair
  10. #define fi first
  11. #define se second
  12. #define de(x) cout << #x << "=" << x << endl;
  13. #define rep(i, a, b) for (int i = a; i < b; i++)
  14. #define per(i, a, b) for (int i = a; i > b; i--)
  15. #define setI freopen("input.txt", "r", stdin);
  16. #define setIO(x) freopen(x".in", "r", stdin);freopen(x".out","w", stdout);
  17. typedef long long LL;
  18. typedef pair<int, int> pii;
  19. typedef vector<int> vi;
  20. //---------------------------------------------------------//
  21. const int N = 2e5 + 5;
  22. const LL MOD = 1e9 + 7;
  23. int n, q;
  24. char str[N];
  25. struct Matrix
  26. {
  27. LL m[3][3];
  28. Matrix(){ mem0(m); }
  29. void init(){ rep(i, 0, 3) rep(j, 0, 3) m[i][j] = (i == j); }
  30. Matrix operator * (const Matrix& r) const
  31. {
  32. Matrix ret;
  33. rep(i, 0, 3) rep(j, 0, 3)
  34. {
  35. ret.m[i][j] = 0;
  36. rep(k, 0, 3) ret.m[i][j] += m[i][k] * r.m[k][j];
  37. ret.m[i][j] %= MOD;
  38. }
  39. return ret;
  40. }
  41. void print()
  42. {
  43. rep(i, 0, 3) rep(j, 0, 3) printf("%lld%c", m[i][j], " \n"[j==2]);
  44. }
  45. } M0, M1;
  46. struct SegTree
  47. {
  48. #define lson t << 1, l, mid
  49. #define rson t << 1 | 1, mid + 1, r
  50. Matrix tree[N * 4 + 5];
  51. bool col[N * 4 + 5];
  52. void pushup(int t)
  53. {
  54. tree[t] = tree[t << 1] * tree[t << 1 | 1];
  55. }
  56. void flip(Matrix &r)
  57. {
  58. swap(r.m[0][0], r.m[0][1]);
  59. swap(r.m[1][0], r.m[1][1]);
  60. swap(r.m[2][0], r.m[2][1]);
  61. swap(r.m[0][0], r.m[1][0]);
  62. swap(r.m[0][1], r.m[1][1]);
  63. swap(r.m[0][2], r.m[1][2]);
  64. }
  65. void pushdown(int t, int l, int r)
  66. {
  67. if (l == r) return ;
  68. int m = r - l + 1;
  69. if (col[t])
  70. {
  71. col[t << 1] ^= col[t];
  72. col[t << 1 | 1] ^= col[t];
  73. flip(tree[t << 1]);
  74. flip(tree[t << 1 | 1]);
  75. col[t] = 0;
  76. }
  77. }
  78. void build(int t, int l, int r)
  79. {
  80. col[t] = 0;
  81. if (l == r)
  82. {
  83. tree[t] = (str[l] == '0' ? M0 : M1);
  84. return ;
  85. }
  86. int mid = l + r >> 1;
  87. build(lson);
  88. build(rson);
  89. pushup(t);
  90. }
  91. void change(int t, int l, int r, int a, int b)
  92. {
  93. if (a == l && b == r)
  94. {
  95. col[t] ^= 1;
  96. flip(tree[t]);
  97. return ;
  98. }
  99. pushdown(t, l, r);
  100. int mid = l + r >> 1;
  101. if (b <= mid) change(lson, a, b);
  102. else if (a > mid) change(rson, a, b);
  103. else
  104. {
  105. change(lson, a, mid);
  106. change(rson, mid + 1, b);
  107. }
  108. pushup(t);
  109. }
  110. Matrix query(int t, int l, int r, int a, int b)
  111. {
  112. if (a == l && b == r) return tree[t];
  113. pushdown(t, l, r);
  114. int mid = l + r >> 1;
  115. if (b <= mid) return query(lson, a, b);
  116. else if (a > mid) return query(rson, a, b);
  117. else return query(lson, a, mid) * query(rson, mid + 1, b);
  118. }
  119. } T;
  120. void init()
  121. {
  122. M0.m[0][0] = M0.m[1][0] = M0.m[1][1] = M0.m[2][0] = M0.m[2][2] = 1;
  123. M1.m[0][0] = M1.m[0][1] = M1.m[1][1] = M1.m[2][1] = M1.m[2][2] = 1;
  124. }
  125. int main()
  126. {
  127. #ifdef M1saki
  128. setI;
  129. #endif
  130. // ios::sync_with_stdio(false);
  131. init();
  132. int Case; scanf("%d", &Case);
  133. while (Case--)
  134. {
  135. scanf("%d%d%s", &n, &q, str + 1);
  136. T.build(1, 1, n);
  137. while (q--)
  138. {
  139. int op, x, y; scanf("%d%d%d", &op, &x, &y);
  140. if (op == 1)
  141. T.change(1, 1, n, x, y);
  142. else
  143. {
  144. Matrix ans = T.query(1, 1, n, x, y);
  145. printf("%lld\n", (ans.m[2][0] + ans.m[2][1]) % MOD);
  146. }
  147. }
  148. }
  149. return 0;
  150. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注