基本势能线段树学习笔记
区间取模
首先我们知道,当
综上所述,一个数
所以只需要维护一个区间最大值,对于一个 w[u].mx < m,若是,直接返回,因为无需修改;反之继续递归至子节点直到递归至叶节点,并暴力修改。
时间复杂度
code :
#include <bits/stdc++.h>
#define ll long long
#define db double
#define vec vector
#define pb push_back
#define pll pair<ll, ll>
#define pr pair
#define mkp make_pair
#define endl "\n"
using namespace std;
const ll mod = 998244353;
namespace fastio {char buf[1 << 21], *p1 = buf, *p2 = buf; const ll getc() {return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;}const ll read() {ll x = 0, f = 1;char ch = getc();while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getc();}while (ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();}return x * f;} const void write(ll x) {if (x < 0) { putchar('-'), x = -x; } ll sta[35], top = 0; do {sta[top++] = x % 10, x /= 10;} while (x); while (top) putchar(sta[--top] + 48); }}
#define rd fastio::read
#define wt fastio::write
#define gc fastio::getc
#define ls (u << 1)
#define rs (u << 1 | 1)
ll n, m, q, k, a[200005]; // string s;
ll opt, l, r, x, y, z, ans = 0;
struct segment {
ll l, r, mx, sum;
} w[800005] ;
void pushup(ll u) {
w[u].sum = w[ls].sum + w[rs].sum;
w[u].mx = max(w[ls].mx, w[rs].mx);
}
void build(ll u ,ll l, ll r) {
w[u].l = l, w[u].r = r;
if (l == r) {
w[u].sum = w[u].mx = a[l];
return ;
}
ll md = l + ((r - l) >> 1);
build(ls, l, md), build(rs, md + 1, r);
pushup(u);
}
void modify(ll u, ll l, ll r, ll x) {
if (w[u].mx < x) return ;
if (w[u].l == w[u].r) {
w[u].sum %= x, w[u].mx %= x;
return ;
}
ll md = w[u].l + ((w[u].r - w[u].l) >> 1);
if (l <= md) modify(ls, l, r, x);
if (r > md) modify(rs, l, r, x);
pushup(u);
}
void update(ll u, ll ps, ll x) {
if (w[u].l == w[u].r && w[u].l == ps) {
w[u].mx = w[u].sum = x; return ;
}
ll md = w[u].l + ((w[u].r - w[u].l) >> 1);
if (ps <= md) update(ls, ps, x);
else update(rs, ps, x);
pushup(u);
}
ll qry(ll u, ll l, ll r) {
if (l <= w[u].l && w[u].r <= r) return w[u].sum;
ll md = w[u].l + ((w[u].r - w[u].l) >> 1), res = 0;
if (l <= md) res += qry(ls, l, r);
if (r > md) res += qry(rs, l, r);
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll i, j, u, v, w, p, q, res = 0, now;
n = rd(), m = rd();
for (i = 1; i <= n; i++) a[i] = rd();
build(1, 1, n);
for (i = 1; i <= m; i++) {
opt = rd(), x = rd(), y = rd();
if (opt == 1) {
wt(qry(1, x, y)), puts("");
} else if (opt == 2) {
k = rd(), modify(1, x, y, k);
} else {
update(1, x, y);
}
}
return 0;
}
附:对我们关于取模所推断结论的测试结果
区间开根号
这部分比较好懂。
假设
只需
所以仍然是维护一个区间最大值,当 w[u].mx <= 1 时返回,反之继续递归并暴力修改。
时间复杂度
code :
#include <bits/stdc++.h>
#define ll long long
#define db double
#define endl "\n"
using namespace std;
ll n, m, a[100005], l, r;
struct segment {
ll l, r, sum, mx;
} w[400005] ;
void pushup(ll u) {
w[u].sum = w[u * 2].sum + w[u * 2 + 1].sum;
w[u].mx = max(w[u * 2].mx, w[u * 2 + 1].mx);
}
void build(ll u, ll l, ll r) {
w[u].l = l, w[u].r = r;
if (l == r) {
w[u].sum = w[u].mx = a[l];
return ;
}
ll mid = l + ((r - l) >> 1);
build(u * 2, l, mid), build(u * 2 + 1, mid + 1, r);
pushup(u);
}
ll query(ll u) {
if (l <= w[u].l && w[u].r <= r) {
return w[u].sum;
}
ll mid = w[u].l + ((w[u].r - w[u].l) >> 1), res = 0;
if (l <= mid) {
res += query(u * 2);
}
if (r > mid) {
res += query(u * 2 + 1);
}
return res;
}
void update(ll u) {
if (w[u].mx <= 1) {
return ;
}
if (w[u].l == w[u].r) {
w[u].sum = sqrt(w[u].sum), w[u].mx = sqrt(w[u].mx);
return ;
}
ll mid = w[u].l + ((w[u].r - w[u].l) >> 1);
if (l <= mid) {
update(u * 2);
}
if (r > mid) {
update(u * 2 + 1);
}
pushup(u);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (ll i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n), cin >> m;
while (m--) {
ll opt; cin >> opt >> l >> r;
if (l > r) swap(l, r);
if (opt == 0) {
update(1);
} else {
cout << query(1) << endl;
}
}
}