bzoj3211 花神游历各国
给一个序列,支持以下两种操作:
- 区间开方
- 区间和
\(n\leq10^5,\ m\leq2\times10^5\)
线段树/树状数组+并查集
势能线段树,,不多说了
主要记录一下第二种做法的写法
时间复杂度 \(O(m\log n)\)
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int n, m, a[maxn], par[maxn]; ll c[maxn];
int find(int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
void add(int pos, int x) {
for (; pos <= n; pos += pos & -pos) {
c[pos] += x;
}
}
ll query(int pos) {
ll res = 0;
for (; pos; pos &= pos - 1) {
res += c[pos];
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
add(i, a[i]), par[i] = i;
}
par[n + 1] = n + 1;
scanf("%d", &m);
while (m--) {
int op, l, r;
scanf("%d %d %d", &op, &l, &r);
if (op == 1) {
printf("%lld\n", query(r) - query(l - 1));
} else {
for (int i = find(l); i <= r; i = find(i + 1)) {
int t = sqrt(a[i]);
add(i, t - a[i]), a[i] = t;
if (a[i] < 2) par[i] = find(i + 1);
}
}
}
return 0;
}