# CF438D The Child and Sequence

### 一、题目大意

• 操作$$1$$
给定区间$$[l,r]$$，对序列中这个区间中每个数字累加求和。

• 操作$$2$$
给定区间$$[l,r]$$$$x$$,对区间每个数字对$$x$$取模。

• 操作$$3$$
给定两个数$$i,k$$，将$$a[i]$$的值修改为$$k$$

### 三、实现代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;

#define ls u << 1
#define rs u << 1 | 1

int n, m;
typedef long long LL;
const int N = 1e5 + 10;
int a[N];

struct Node {
int l, r;
LL sum, max;
} tr[N << 2];

void pushup(int u) {
tr[u].sum = tr[ls].sum + tr[rs].sum;
tr[u].max = max(tr[ls].max, tr[rs].max);
}

void build(int u, int l, int r) {
tr[u] = {l, r, 0, 0};
if (l == r) {
tr[u].max = tr[u].sum = a[l];
return;
}
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);

pushup(u);
}

void modify(int u, int x, int c) {
if (tr[u].l > x || tr[u].r < x) return;
if (tr[u].l == tr[u].r) {
tr[u].sum = tr[u].max = c;
return;
}
modify(ls, x, c), modify(rs, x, c);
pushup(u);
}

void modify(int u, int l, int r, int x) {
if (tr[u].l > r || tr[u].r < l) return;
if (tr[u].max < x) return; //剪枝

if (tr[u].l == tr[u].r) {
tr[u].sum %= x;
tr[u].max = tr[u].sum;
return;
}
modify(ls, l, r, x), modify(rs, l, r, x);
pushup(u);
}

LL query(int u, int l, int r) {
if (tr[u].l > r || tr[u].r < l) return 0;
if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
return query(ls, l, r) + query(rs, l, r);
}

int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];

build(1, 1, n);

int l, r, k, x;
while (m--) {
int op;
cin >> op;
if (op == 1) {
cin >> l >> r;
printf("%lld\n", query(1, l, r));
}
if (op == 2) {
cin >> l >> r >> x;
modify(1, l, r, x);
}
if (op == 3) {
cin >> k >> x;
modify(1, k, x);
}
}
return 0;
}

posted @ 2022-04-28 15:05  糖豆爸爸  阅读(23)  评论(0编辑  收藏  举报
Live2D