题目链接
https://codeforces.com/problemset/problem/438/D
思路分析
- 从题意我们可以看出这个题对线段树有三个操作:区间求和,区间取模以及单点修改。
- 首先,对于区间求和和单点修改就不需要说了,这是最经典的线段树操作。
- 对于区间取模,我们可以这样想,我们保存一个区间最大值,如果这个最大值都比模数要小,那么我们也就不需要取模了,这个时间复杂度也是在题目的接受范围的。因此我们在每次操作的时候都应该将区间最大值保存起来。
代码片段
- 建树
void bulid(ll s,ll t,ll p)
{
tree[p].l = s, tree[p].r = t;
if (s == t)
{
tree[p].mx = tree[p].sum = a[s];
return;
}
ll mid = (l + r) >> 1;
build(s, mid, p << 1);
bulid(mid + 1, t, p << 1 | 1);
pushup(p);
//更新上层
}
- 更新上层操作
void pushup(int p)
{
tree[p].mx = max(tree[p << 1].mx,tree[p << 1 | 1].mx);
tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum;
}
- 区间求和
ll query(ll s, ll t, ll p)
{
if (tree[p].l >= s && tree[p].r <= t)
return tree[p].sum;
ll mid = (l + r) >> 1;
ll sum = 0;
if (s <= mid) ans += query(s, t, p << 1);
if (t > mid) ans += query(s, t, p << 1 | 1);
return sum;
}
- 单点修改
void update(ll p, ll k, ll x)
{
if (tree[p].l == tree[p].r)
{
tree[p].sum = tree[p].mx = x;
return;
}
ll mid = (tree[p].l + tree[p].r) >> 1;
if (k <= mid)
update(p << 1, k, x);
else
update(p << 1 | 1, k, x);
//找单点,二分
pushup(p);
}
- 区间取模
void mod(ll s, ll t, ll p, ll x)
{
if (x > tree[p].mx) return;
if (tree[p].l == tree[p].r)
{
tree[p].sum %= x;
tree[p].mx = tree[p].sum;
return;
}//单点时取模
ll mid = (tree[p].l + tree[p].r) >> 1;
if (s <= mid)
mod(p << 1, s, t, x); // note: please use [s, t] for all functions
if (t > mid)
mod(p << 1 | 1, s, t, x);
pushup(p);
}
完整代码
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 1e5 + 10;
ll a[MAXN];
struct node
{
ll l, r, mx, sum;
} tree[MAXN << 2];
void pushup(ll p)
{
tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum;
tree[p].mx = max(tree[p << 1].mx, tree[p << 1 | 1].mx);
}
void build(ll s, ll t, ll p)
{
tree[p].l = s;
tree[p].r = t;
if (s == t)
{
tree[p].mx = tree[p].sum = a[s];
return;
}
ll mid = (s + t) >> 1;
build(s, mid, p << 1), build(mid + 1, t, p << 1 | 1);
pushup(p);
}
ll query(ll p, ll s, ll t)
{
if (tree[p].l >= s && tree[p].r <= t)
return tree[p].sum;
ll mid = (tree[p].l + tree[p].r) >> 1;
ll sum = 0;
if (s <= mid)
sum = query(p << 1, s, t);
if (t > mid)
sum += query(p << 1 | 1, s, t);
return sum;
}
void mod(ll p, ll s, ll t, ll x)
{
if (x > tree[p].mx)
return;
if (tree[p].l == tree[p].r)
{
tree[p].sum %= x;
tree[p].mx = tree[p].sum;
return;
}
ll mid = (tree[p].l + tree[p].r) >> 1;
if (s <= mid)
mod(p << 1, s, t, x); // note: please use [s, t] for all functions
if (t > mid)
mod(p << 1 | 1, s, t, x);
pushup(p);
}
void update(ll p, ll k, ll x)
{
if (tree[p].l == tree[p].r)
{
tree[p].sum = tree[p].mx = x;
return;
}
ll mid = (tree[p].l + tree[p].r) >> 1;
if (k <= mid)
update(p << 1, k, x);
else
update(p << 1 | 1, k, x);
pushup(p);
}
ll n, m;
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
build(1, n, 1);
while (m--)
{
ll type;
cin >> type;
if (type == 1)
{
ll l, r;
cin >> l >> r;
cout << query(1, l, r) << endl;
}
else if (type == 2)
{
ll l, r, x;
cin >> l >> r >> x;
mod(1, l, r, x);
}
else
{
ll k, x;
cin >> k >> x;
update(1, k, x);
}
}
return 0;
}