1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; typedef long long LL; const int N = 1e5 + 10; int n, m; LL w[N]; struct Node { int l, r; LL mx, sum; }tr[N << 2]; void pushup (int x) { tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx); tr[x].sum = tr[x << 1].sum + tr[x << 1 | 1].sum; } void build (int x, int l, int r) { tr[x].l = l; tr[x].r = r; if (l == r) { tr[x].mx = tr[x].sum = w[l]; return; } int mid = l + r >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); pushup(x); } LL query (int x, int l, int r) { if (tr[x].l >= l && tr[x].r <= r) return tr[x].sum; int mid = tr[x].l + tr[x].r >> 1; LL res = 0; if (l <= mid) res += query(x << 1, l, r); if (r > mid) res += query(x << 1 | 1, l, r); return res; } void dfs (int x, int l, int r) { if (tr[x].mx <= 1) return; if (tr[x].l == tr[x].r) { tr[x].mx = tr[x].sum = sqrt(tr[x].mx); return; } int mid = tr[x].l + tr[x].r >> 1; if (l <= mid) dfs(x << 1, l, r); if (r > mid) dfs(x << 1 | 1, l, r); pushup(x); } int main () { int cas = 1; while (~scanf("%d", &n)) { printf("Case #%d:\n", cas++); memset(tr, 0, sizeof tr); for (int i = 1; i <= n; i++) scanf("%lld", &w[i]); build(1, 1, n); scanf("%d", &m); for (int k, l, r; m; m--) { scanf("%d%d%d", &k, &l, &r); if (l > r) swap(l, r); if (k) printf("%lld\n", query(1, l, r)); else dfs(1, l, r); } puts(""); } return 0; }
|