Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF438D The Child and Sequence

LuoGu: CF438D The Child and Sequence

CF: D. The Child and Sequence

只有取模操作,数会越来越小,维护区间最大值,比模数小就不再操作。

查看代码
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
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n, m;
LL w[N];
struct Node
{
int l, r;
LL v, mx;
} tr[N << 2];
void pushup(int x)
{
tr[x].v = tr[x << 1].v + tr[x << 1 | 1].v;
tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx);
}
void build(int x, int l, int r)
{
tr[x].l = l;
tr[x].r = r;
if (l == r)
{
tr[x].v = tr[x].mx = w[l];
return;
}
int mid = l + r >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
pushup(x);
}
void modify(int x, int l, int r, LL k)
{
if (tr[x].mx < k)
return;
if (tr[x].l == tr[x].r)
{
tr[x].v %= k;
tr[x].mx %= k;
return;
}
int mid = tr[x].l + tr[x].r >> 1;
if (l <= mid)
modify(x << 1, l, r, k);
if (r > mid)
modify(x << 1 | 1, l, r, k);
pushup(x);
}
void modify(int x, int t, LL k)
{
if (tr[x].l == tr[x].r)
{
tr[x].v = tr[x].mx = k;
return;
}
int mid = tr[x].l + tr[x].r >> 1;
if (t <= mid)
modify(x << 1, t, k);
else
modify(x << 1 | 1, t, k);
pushup(x);
}
LL query(int x, int l, int r)
{
if (tr[x].l >= l && tr[x].r <= r)
return tr[x].v;
LL res = 0;
int mid = tr[x].l + tr[x].r >> 1;
if (l <= mid)
res += query(x << 1, l, r);
if (r > mid)
res += query(x << 1 | 1, l, r);
return res;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%lld", &w[i]);
build(1, 1, n);
for (int op; m; m--)
{
scanf("%d", &op);
if (op == 1)
{
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(1, l, r));
}
else if (op == 2)
{
int l, r;
LL x;
scanf("%d%d%lld", &l, &r, &x);
modify(1, l, r, x);
}
else if (op == 3)
{
int k;
LL x;
scanf("%d%lld", &k, &x);
modify(1, k, x);
}
}
return 0;
}