Description
有一个序列要求支持区间求和,单点加,区间取膜。
Solution
一个数膜比他小的数一定会减小至少一半。
所以就是O(nlog2n)的。
#include <bits/stdc++.h>
using namespace std;
const int N = 101010;
typedef long long ll;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline void read(int &x) {
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
ll sm[N << 2];
int mx[N << 2];
int n, m;
int a[N];
int opt, x, y, z;
inline void Set(int o, int l, int r, int pos, int x) {
if (l == r) {
mx[o] = sm[o] = x; return;
}
int mid = (l + r) >> 1;
if (pos <= mid) Set(o << 1, l, mid, pos, x);
else Set(o << 1 | 1, mid + 1, r, pos, x);
sm[o] = sm[o << 1] + sm[o << 1 | 1];
mx[o] = max(mx[o << 1], mx[o << 1 | 1]);
}
inline void Mod(int o, int l, int r, int L, int R, int x) {
int mid = (l + r) >> 1;
if (mx[o] < x) return;
if (l >= L && r <= R) {
if (l == r) return(void)(sm[o] %= x, mx[o] %= x);
}
if (L <= mid) Mod(o << 1, l, mid, L, R, x);
if (R > mid) Mod(o << 1 | 1, mid + 1, r, L, R, x);
sm[o] = sm[o << 1] + sm[o << 1 | 1];
mx[o] = max(mx[o << 1], mx[o << 1 | 1]);
}
inline ll Query(int o, int l, int r, int L, int R) {
if (l >= L && r <= R) return sm[o];
int mid = (l + r) >> 1;
ll res = 0;
if (L <= mid) res += Query(o << 1, l, mid, L, R);
if (R > mid) res += Query(o << 1 | 1, mid + 1, r, L, R);
return res;
}
inline void Build(int o, int l, int r) {
if (l == r) return (void)(mx[o] = sm[o] = a[l]);
int mid = (l + r) >> 1;
Build(o << 1, l, mid);
Build(o << 1 | 1, mid + 1, r);
sm[o] = sm[o << 1] + sm[o << 1 | 1];
mx[o] = max(mx[o << 1], mx[o << 1 | 1]);
}
int main(void) {
read(n); read(m);
for (int i = 1; i <= n; i++) read(a[i]);
Build(1, 1, n);
for (int i = 1; i <= m; i++) {
read(opt); read(x); read(y);
if (opt == 1) {
printf("%I64d\n", Query(1, 1, n, x, y));
} else if (opt == 2) {
read(z); Mod(1, 1, n, x, y, z);
} else {
Set(1, 1, n, x, y);
}
}
return 0;
}