$\large \color{blue} Describe$

这题只有在数据随机的情况下4操作的复杂度才是正确的,因为数据随机,因此我们可以考虑平衡
树维护相同值域连续段,然后重点就是拆区间,我们考虑把在找节点时把一个连续段拆成3段x、y、z,把z作为y的后继,y作为x的后继,再返回y即可,其他思路都非常简单,主要考察代码能力。

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
#define poi(x) (rs(fa(x)) == (x))
#define reverse(x) (swap(ls(x), rs(x)), t[x].filp ^= 1)
const int p = 1e9 + 7;
stack<int> st;
int rt, cnt, a[300005], len[300005];
struct node {
int fa, child[2], tot, sum, w, size, len;
bool filp, tag;
} t[3000005];
inl void upd(int x) {
t[x].tot = ((t[ls(x)].tot + t[rs(x)].tot) % p + (1ll * t[x].w * t[x].len)) % p;
t[x].size = t[ls(x)].size + t[rs(x)].size + t[x].len;
}
inl int newn(int w, int len) {
re x;
if (!st.empty())
x = st.top(), st.pop();
else
x = ++cnt;
t[x].size = t[x].tot = fa(x) = ls(x) = rs(x) = t[x].filp = t[x].sum = 0;
t[x].w = w, t[x].len = len, upd(x);
return x;
}
inl void rotate(int x) {
re f = fa(x), gf = fa(f), fs = poi(x), gfs = poi(f), s = son(x, fs ^ 1);
if (gf) son(gf, gfs) = x;
son(f, fs) = s, son(x, fs ^ 1) = f;
if (s) fa(s) = f;
fa(f) = x, fa(x) = gf, upd(f);
}
inl void sum(int x, int w) { t[x].tot = (t[x].tot + 1ll * w * t[x].size) % p, t[x].w = (t[x].w + w) % p, t[x].sum = (t[x].sum + w) % p; }
inl void pushdown(int x) {
if (t[x].sum) {
if (ls(x)) sum(ls(x), t[x].sum);
if (rs(x)) sum(rs(x), t[x].sum);
t[x].sum = 0;
}
if (t[x].filp) {
if (ls(x)) reverse(ls(x));
if (rs(x)) reverse(rs(x));
t[x].filp = 0;
}
}
inl void push(int x) {
if (fa(x)) push(fa(x));
pushdown(x);
}
inl void splay(int x, int to = 0) {
push(x);
while (fa(x) != to) {
if (fa(fa(x)) != to) poi(x) == poi(fa(x)) ? rotate(fa(x)) : rotate(x);
rotate(x);
}
if (!to) rt = x;
upd(x);
}
inl int split(int x, int k) {
re y = newn(t[x].w, t[x].len - k);
t[x].len = k;
if (!rs(x))fa(rs(x) = y) = x;
else {
pushdown(x);
re o = rs(x); pushdown(o);
while (ls(o))pushdown(o = ls(o));
fa(ls(o) = y) = o;
while (o != x) upd(o), o = fa(o);
}
splay(y);
return y;
}
inl void find(int k) {
re x = rt;
while (x) {
pushdown(x);
if (t[ls(x)].size >= k) x = ls(x);
else {
k -= t[ls(x)].size;
if (t[x].len >= k) {
if (k != t[x].len)split(x, k);
if (k != 1)x = split(x, k - 1);
return splay(x);
}
else k -= t[x].len, x = rs(x);
}
}
}
inl int nxt(int k, bool f) {
find(k);
re x = rt;
x = son(x, f);
while (son(x, f ^ 1))pushdown(x), x = son(x, f ^ 1);
return x;
}
inl void build(int &k, int l, int r) {
if (l > r) return;
re mid = l + r >> 1;
k = newn(a[mid], len[mid]);
if (a[mid] == inf)t[k].tag = 1, t[k].w = 0, upd(k);
build(ls(k), l, mid - 1), build(rs(k), mid + 1, r);
if (ls(k))fa(ls(k)) = k; if (rs(k)) fa(rs(k)) = k;
upd(k);
}
int num;
inl void travel(int x) {
pushdown(x);
if (ls(x)) travel(ls(x));
a[++num] = t[x].w, len[num] = t[x].len;
if (rs(x)) travel(rs(x));
}
inl void erase(int x) {
if (ls(x)) erase(ls(x));
st.push(x);
if (rs(x)) erase(rs(x));
}
inl void change(int x, int w) {
if (w == 1) {
t[x].w = w, upd(x);
return;
}
erase(x);
re k = fa(x);
ls(k) = fa(x) = 0;
fa(ls(k) = newn(w, t[x].size)) = k;
splay(ls(k));
}
inl void print(int x) {
pushdown(x);
if (ls(x))print(ls(x));
if (!t[x].tag)for (re i = 1; i <= t[x].len; i++)writesp(t[x].w);
if (rs(x))print(rs(x));
}
inl int cut(int a, int b) {
re l, r;
find(a), l = rt, find(b + 2), r = rt;
splay(l), splay(r, l);
return ls(r);
}
signed main() {
re n = read<int>(), m = read<int>(), l, r, op, l1, r1, w, tmp1, tmp2, f;
a[++num] = inf, len[num] = 1;
for (re i = 1; i <= n; i++) a[++num] = read<int>() % p, len[num] = 1;
a[++num] = inf, len[num] = 1;
build(rt, 1, num);
while (m--) {
op = read<int>(), l = read<int>(), r = read<int>();
switch (op) {
case 1: {
writeln(t[cut(l, r)].tot);
break;
}
case 2: {
w = read<int>() % p;
change(cut(l, r), w);
break;
}
case 3: {
w = read<int>() % p;
sum(cut(l, r), w);
break;
}
case 4: {
l1 = read<int>(), r1 = read<int>();
num = 0, travel(cut(l, r));
build(tmp1, 1, num);
erase(tmp2 = cut(l1, r1));
ls(fa(tmp2)) = 0, f = fa(tmp2), fa(tmp2) = 0;
fa(ls(f) = tmp1) = f;
splay(ls(f));
break;
}
case 5: {
l1 = read<int>(), r1 = read<int>();
num = 0, travel(cut(l, r));
build(tmp1, 1, num);
tmp2 = cut(l1, r1);
ls(fa(tmp2)) = 0, f = fa(tmp2), fa(tmp2) = 0;
fa(ls(f) = tmp1) = f;
splay(ls(f));
f = cut(l, r);
ls(fa(f)) = tmp2, fa(tmp2) = fa(f), fa(f) = 0;
erase(f);
break;
}
case 6: {
f = cut(l, r);
reverse(f);
break;
}
}
}
print(rt);
}