题目描述
思路
- 0 和 1 开方之后不会改变,对 0, 1 节点打上标记
- 如果孩子节点都打上标记了,说明这些孩子节点的和不会改变,那么父节点也打上标记
- 对于打上标记的点,change 的时候直接 return
代码
#include <cstdio>
#include <cmath>
int n, m;
long long arr[100005 << 2], at[100005];
bool flag[100005 << 2];
inline int read() {
int s = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s;
}
void build(int k, int l, int r) {
if (l == r) {
arr[k] = at[l];
if (arr[k] <= 1) flag[k] = 1;
return;
}
int mid = l + r >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
arr[k] = arr[k << 1] + arr[k << 1 | 1];
if (flag[k << 1] && flag[k << 1| 1]) flag[k] = 1;
}
void change(int k, int l, int r, int x, int y) {
if (flag[k]) return;
if (l == r) {
arr[k] = sqrt(arr[k]);
if (arr[k] <= 1) flag[k] = 1;
return;
}
int mid = (l + r) >> 1;
if (x <= mid) change(k << 1, l, mid, x, y);
if (y > mid) change(k << 1 | 1, mid + 1, r, x, y);
arr[k] = arr[k << 1] + arr[k << 1 | 1];
if (flag[k << 1] && flag[k << 1| 1]) flag[k] = 1;
}
long long query(int k, int l, int r, int x, int y) {
if (x <= l && r <= y) return arr[k];
int mid = (l + r) >> 1;
long long res = 0;
if (x <= mid) res = query(k << 1, l, mid, x, y);
if (y > mid) res += query(k << 1 | 1, mid + 1, r, x, y);
return res;
}
int main() {
n = read();
for (int i = 1, j; i <= n; ++i) at[i] = read();
m = read();
build(1, 1, n);
for (int i = 1, a, b, c; i <= m; ++i) {
a = read(), b = read(), c = read();
if (a == 1) {
printf("%lld
", query(1, 1, n, b, c));
} else {
change(1, 1, n, b, c);
}
}
return 0;
}