vfk大大讲过的 = = 记录一个最大值然后暴力搞就行了。。。
最开始WA了一次是因为Modify没更改最大值。。
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int Nmax = 1e5 + 5;
int N, M;
int a[Nmax];
#define lc (u << 1)
#define rc (u << 1 | 1)
namespace SegmentTree {
long long sum[Nmax << 2], mmax[Nmax << 2];
inline long long Max(long long a, long long b) { return a > b ? a : b; }
inline void Push_up(int u)
{
sum[u] = sum[lc] + sum[rc];
mmax[u] = Max(mmax[lc], mmax[rc]);
}
inline void build(int u, int l, int r)
{
if (l == r) {
sum[u] = mmax[u] = a[l];
return;
}
int mid = (l + r) >> 1;
build(lc, l, mid); build(rc, mid + 1, r);
Push_up(u);
}
inline long long Query(int u, int l, int r, int L, int R)
{
if (l == L && r == R) return sum[u];
int mid = (l + r) >> 1;
if (R <= mid) return Query(lc, l, mid, L, R);
else if (L > mid) return Query(rc, mid + 1, r, L, R);
return Query(lc, l, mid, L, mid) + Query(rc, mid + 1, r, mid + 1, R);
}
inline void Mod(int u, int l, int r, int L, int R, int x)
{
if (mmax[u] < x) return;
if (l == r) { sum[u] = mmax[u] = sum[u] % x; return; }
int mid = (l + r) >> 1;
if (R <= mid) Mod(lc, l, mid, L, R, x);
else if (L > mid) Mod(rc, mid + 1, r, L, R, x);
else {
Mod(lc, l, mid, L, mid, x);
Mod(rc, mid + 1, r, mid + 1, R, x);
}
Push_up(u);
}
inline void Modify(int u, int l, int r, int pos, int x)
{
if (l == r) { mmax[u] = sum[u] = x; return; }
int mid = (l + r) >> 1;
if (pos <= mid) Modify(lc, l, mid, pos, x);
else Modify(rc, mid + 1, r, pos, x);
Push_up(u);
}
}
int main()
{
ios :: sync_with_stdio(false);
cin >> N >> M;
for (int i = 1; i <= N; ++i) cin >> a[i];
using namespace SegmentTree;
build(1, 1, N);
for (int sign, l, r, x; M --; ) {
cin >> sign >> l >> r;
if (sign == 1) cout << Query(1, 1, N, l, r) << endl;
else if (sign == 2) {
cin >> x;
Mod(1, 1, N, l, r, x);
}
else Modify(1, 1, N, l, r);
}
return 0;
}