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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| #include <cstdio> #include <vector> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; typedef double db;
inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; }
const int MAXN = 40010, MOD = 1e9 + 7;
int n, m; vector<pair<int, int>> queryList;
struct Query { int op, l, r; LL a[3]; } query[MAXN];
class SegTree { private: int lbound[MAXN << 4], rbound[MAXN << 4]; LL sumVal[MAXN << 4][3], addVal[MAXN << 4][3], mulVal[MAXN << 4]; int addAxis[MAXN << 4], sumAxis[MAXN << 4]; inline int adjust_axis(int i, int o) { return (i + sumAxis[o]) % 3; } void push_down(int o) { int lc = o << 1, rc = (o << 1) | 1; if (addAxis[o]) { addAxis[lc] = (addAxis[lc] + addAxis[o]) % 3; addAxis[rc] = (addAxis[rc] + addAxis[o]) % 3; sumAxis[lc] = (sumAxis[lc] + addAxis[o]) % 3; sumAxis[rc] = (sumAxis[rc] + addAxis[o]) % 3; addAxis[o] = 0; } if (mulVal[o] != 1) { mulVal[lc] = (mulVal[lc] * mulVal[o]) % MOD; mulVal[rc] = (mulVal[rc] * mulVal[o]) % MOD; for (int i = 0; i < 3; ++i) { addVal[lc][i] = addVal[lc][i] * mulVal[o] % MOD; addVal[rc][i] = addVal[rc][i] * mulVal[o] % MOD; sumVal[lc][i] = (sumVal[lc][i] * mulVal[o]) % MOD; sumVal[rc][i] = (sumVal[rc][i] * mulVal[o]) % MOD; } mulVal[o] = 1; } for (int i = 0; i < 3; ++i) { if (addVal[o][adjust_axis(i, o)]) { addVal[lc][adjust_axis(i, lc)] = (addVal[lc][adjust_axis(i, lc)] + addVal[o][adjust_axis(i, o)]) % MOD; addVal[rc][adjust_axis(i, rc)] = (addVal[rc][adjust_axis(i, rc)] + addVal[o][adjust_axis(i, o)]) % MOD; sumVal[lc][adjust_axis(i, lc)] = (sumVal[lc][adjust_axis(i, lc)] + (rbound[lc] - lbound[lc] + 1) * addVal[o][adjust_axis(i, o)] % MOD) % MOD; sumVal[rc][adjust_axis(i, rc)] = (sumVal[rc][adjust_axis(i, rc)] + (rbound[rc] - lbound[rc] + 1) * addVal[o][adjust_axis(i, o)] % MOD) % MOD; addVal[o][adjust_axis(i, o)] = 0; } } }
public: struct Ans { LL sum[3]; Ans(LL x = 0, LL y = 0, LL z = 0) { sum[0] = x; sum[1] = y; sum[2] = z; } const LL &operator[](const int &x) const { return sum[x]; } LL &operator[](const int &x) { return sum[x]; } Ans operator+(const Ans &b) const { return Ans((sum[0] + b[0]) % MOD, (sum[1] + b[1]) % MOD, (sum[2] + b[2]) % MOD); } }; void build(int o, int L, int R) { int lc = o << 1, rc = (o << 1) | 1, M = (L + R) >> 1; mulVal[o] = 1; lbound[o] = queryList[L - 1].first; rbound[o] = queryList[R - 1].second; if (L == R) return; build(lc, L, M); build(rc, M + 1, R); } void update_val(int o, const int &queryID) { int lc = o << 1, rc = (o << 1) | 1; if (query[queryID].l <= lbound[o] && query[queryID].r >= rbound[o]) { if (query[queryID].op == 1) { for (int i = 0; i < 3; ++i) { addVal[o][adjust_axis(i, o)] = (addVal[o][adjust_axis(i, o)] + query[queryID].a[i]) % MOD; sumVal[o][adjust_axis(i, o)] = (sumVal[o][adjust_axis(i, o)] + query[queryID].a[i] * (rbound[o] - lbound[o] + 1) % MOD) % MOD; } } else if (query[queryID].op == 2) { for (int i = 0; i < 3; ++i) { addVal[o][i] = addVal[o][i] * query[queryID].a[0] % MOD; sumVal[o][i] = sumVal[o][i] * query[queryID].a[0] % MOD; } mulVal[o] = mulVal[o] * query[queryID].a[0] % MOD; } else { addAxis[o] = (addAxis[o] + 1) % 3; sumAxis[o] = (sumAxis[o] + 1) % 3; } return; } push_down(o); if (query[queryID].l <= rbound[lc]) update_val(lc, queryID); if (query[queryID].r >= lbound[rc]) update_val(rc, queryID); for (int i = 0; i < 3; ++i) sumVal[o][adjust_axis(i, o)] = (sumVal[lc][adjust_axis(i, lc)] + sumVal[rc][adjust_axis(i, rc)]) % MOD; } Ans query_sum(int o, const int &queryID) { int lc = o << 1, rc = (o << 1) | 1; if (query[queryID].l <= lbound[o] && query[queryID].r >= rbound[o]) return (Ans){sumVal[o][adjust_axis(0, o)], sumVal[o][adjust_axis(1, o)], sumVal[o][adjust_axis(2, o)]}; push_down(o); Ans ans; if (query[queryID].l <= rbound[lc]) ans = query_sum(lc, queryID); if (query[queryID].r >= lbound[rc]) ans = ans + query_sum(rc, queryID); for (int i = 0; i < 3; ++i) sumVal[o][adjust_axis(i, o)] = (sumVal[lc][adjust_axis(i, lc)] + sumVal[rc][adjust_axis(i, rc)]) % MOD; return ans; } } Tree;
int main() { n = read(); m = read(); vector<int> nodeList; nodeList.push_back(1); nodeList.push_back(n); for (int i = 1; i <= m; ++i) { query[i].op = read(); query[i].l = read(); query[i].r = read(); if (query[i].op == 1) { for (int j = 0; j < 3; ++j) query[i].a[j] = read(); } else if (query[i].op == 2) query[i].a[0] = read(); nodeList.push_back(query[i].l); nodeList.push_back(query[i].r); } sort(nodeList.begin(), nodeList.end()); nodeList.resize(unique(nodeList.begin(), nodeList.end()) - nodeList.begin()); for (int i = 0; i < nodeList.size(); ++i) { if (i != 0) if (nodeList[i - 1] < nodeList[i] - 1) queryList.emplace_back(nodeList[i - 1] + 1, nodeList[i] - 1); queryList.emplace_back(nodeList[i], nodeList[i]); } Tree.build(1, 1, queryList.size()); for (int i = 1; i <= m; ++i) { if (query[i].op <= 3) Tree.update_val(1, i); else { auto ans = Tree.query_sum(1, i); printf("%lld\n", ((ans[0] * ans[0] % MOD + ans[1] * ans[1] % MOD) % MOD + ans[2] * ans[2] % MOD) % MOD); } } return 0; }
|