题目链接:传送门
区间求和,单点修改,区间取模
线段树里维护一个max,取模的时候如果区间最大值小于max直接return就可以
不然的话递归到叶子节点再取模
有效降低复杂度
不开long long好像只能过两个样例
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define
using namespace std;
typedef long long ll;
#define
struct node {
int l, r, w, mx;
}tree[A];
int n, m, a, b, c, aa[A];
void up(int k) {
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
tree[k].mx = max(tree[k << 1].mx, tree[k << 1 | 1].mx);
}
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {
tree[k].w = tree[k].mx = aa[l];
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
up(k);
}
void change(int k, int pos, int val) {
if (tree[k].l == tree[k].r) {
tree[k].w = tree[k].mx = val;
return;
}
int m = (tree[k].l + tree[k].r) >> 1;
if (pos <= m) change(k << 1, pos, val);
else change(k << 1 | 1, pos, val);
up(k);
}
int ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
int m = (tree[k].l + tree[k].r) >> 1, ans = 0;
if (l <= m) ans += ask(k << 1, l, r);
if (r > m) ans += ask(k << 1 | 1, l, r);
return ans;
}
void ccc(int k, int l, int r, int mod) {
if (tree[k].mx < mod) return;
if (tree[k].l == tree[k].r) {
tree[k].w %= mod;
tree[k].mx %= mod;
return;
}
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) ccc(k << 1, l, r, mod);
if (r > m) ccc(k << 1 | 1, l, r, mod);
up(k);
}
signed main(signed argc, char const *argv[]) {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> aa[i];
build(1, 1, n); int opt;
while (m--) {
cin >> opt;
if (opt == 1) {
cin >> a >> b;
cout << ask(1, a, b) << endl;
}
if (opt == 2) {
cin >> a >> b >> c;
ccc(1, a, b, c);
}
if (opt == 3) {
cin >> a >> b;
change(1, a, b);
}
}
return 0;
}