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
|
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " is " << x << endl
typedef pair<int, int> P;
typedef long long ll;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 4e5 + 5;
struct Q {
int op, l, r, k, a, b, c;
}q[N];
vector<int> sca;
struct Node {
int l, r, k;
int a[3], b[3], s[3];
int get_len() { return sca[r] - sca[l-1]; }
}tr[N<<2];
int n, m;
int get_id(int x) {
return lower_bound(sca.begin(), sca.end(), x) - sca.begin();
}
void push_up(int u) {
auto ls = tr[u<<1].s, rs = tr[u<<1|1].s;
for (int i = 0; i < 3; ++i)
tr[u].s[i] = (ls[i] + rs[i]) % MOD;
}
void rotate(int a[]) {
int t = a[0];
a[0] = a[1], a[1] = a[2], a[2] = t;
}
void eval(int u, int k) {
k %= 3;
for (int i = 0; i < k; ++i) {
rotate(tr[u].a); rotate(tr[u].b); rotate(tr[u].s);
}
tr[u].k += k;
}
void eval(int u, int a[], int b[]) {
for (int i = 0; i < 3; ++i) {
tr[u].s[i] = (1ll * tr[u].s[i] * a[i] + 1ll * tr[u].get_len() * b[i]) % MOD;
int c = 1ll * tr[u].a[i] * a[i] % MOD;
int d = (1ll * tr[u].b[i] * a[i] + b[i]) % MOD;
tr[u].a[i] = c; tr[u].b[i] = d;
}
}
void push_down(int u) {
eval(u<<1, tr[u].k); eval(u<<1|1, tr[u].k);
tr[u].k = 0;
eval(u<<1, tr[u].a, tr[u].b); eval(u<<1|1, tr[u].a, tr[u].b);
for (int i = 0; i < 3; ++i)
tr[u].a[i] = 1, tr[u].b[i] = 0;
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r;
for (int i = 0; i < 3; ++i) tr[i].a[i] = 1;
if (l == r) return;
int mid = (l + r) >> 1;
build(u<<1, l, mid);
build(u<<1|1, mid + 1, r);
}
void update(int u, int l, int r, int k, int a[], int b[]) {
if (tr[u].l >= l && tr[u].r <= r) {
eval(u, k), eval(u, a, b);
return;
}
push_down(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid) update(u<<1, l, r, k, a, b);
if (r > mid) update(u<<1|1, l, r, k, a, b);
push_up(u);
}
vector<int> query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return { tr[u].s[0], tr[u].s[1], tr[u].s[2] };
push_down(u);
int mid = (tr[u].l + tr[u].r) >> 1;
vector<int> res(3);
if (l <= mid) res = query(u<<1, l, r);
if (r > mid) {
auto t = query(u<<1|1, l, r);
for (int i = 0; i < 3; ++i)
res[i] = (res[i] + t[i]) % MOD;
}
return res;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
q[i].k = 1;
cin >> q[i].op >> q[i].l >> q[i].r;
if (q[i].op == 1) cin >> q[i].a >> q[i].b >> q[i].c;
else if (q[i].op == 2) cin >> q[i].k;
sca.push_back(q[i].l - 1); sca.push_back(q[i].r);
}
sort(sca.begin(), sca.end());
sca.erase(unique(sca.begin(), sca.end()), sca.end());
build(1, 0, sca.size() - 1);
for (int i = 1; i <= m; ++i) {
int op = q[i].op, l = get_id(q[i].l - 1) + 1, r = get_id(q[i].r);
int a[] = {q[i].k, q[i].k, q[i].k}, b[] = {q[i].a, q[i].b, q[i].c};
if (op == 1 || op == 2) update(1, l, r, 0, a, b);
else if (op == 3) update(1, l, r, 1, a, b);
else {
auto t = query(1, l, r);
ll sum = 0;
for (int j = 0; j < 3; ++j)
sum += 1ll * t[j] * t[j];
cout << sum % MOD << '\n';
}
}
return 0;
}
|