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 87
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <cmath>
#define FILE_IN(__fname) freopen(__fname, "r", stdin) #define FILE_OUT(__fname) freopen(__fname, "w", stdout) #define rap(a,s,t,i) for (int a = s; a <= t; a += i) #define down(a,t,s,i) for (int a = t; a >= s; a -= i) #define IMPROVE_IO() std::ios::sync_with_stdio(false)
using std::cin; using std::cout; using std::endl;
typedef long long int lli;
int getint() { int x; scanf("%d", &x); return x; } lli getll() { long long int x; scanf("%lld", &x); return x; }
const int MAXN = 1e5 + 10;
int n, m; lli osq[MAXN];
namespace SegmentTree { #define lson (root << 1) #define rson (root << 1 | 1)
lli sum[MAXN << 2], max[MAXN << 2];
void update(int root) { sum[root] = sum[lson] + sum[rson]; max[root] = std::max(max[lson], max[rson]); } void buildTree(int root = 1, int l = 1, int r = n) { if (l == r) return (void) (sum[root] = max[root] = osq[l]); int mid = (l + r) >> 1; buildTree(lson, l, mid); buildTree(rson, mid + 1, r); update(root); } void modify(int ll, int rr, int root = 1, int l = 1, int r = n) { if (l == r) { sum[root] = max[root] = sqrt(sum[root]); return; } int mid = (l + r) >> 1; if (ll <= mid && max[lson] > 1) modify(ll, rr, lson, l, mid); if (mid + 1 <= rr && max[rson] > 1) modify(ll, rr, rson, mid + 1, r); update(root); } lli querySum(int ll, int rr, int root = 1, int l = 1, int r = n) { if (ll <= l && r <= rr) return sum[root]; lli rt = 0; int mid = (l + r) >> 1; if (ll <= mid) rt += querySum(ll, rr, lson, l, mid); if (mid + 1 <= rr) rt += querySum(ll, rr, rson, mid + 1, r); return rt; } }
int main() { n = getint(); rap (i, 1, n, 1) osq[i] = getll(); SegmentTree::buildTree(); m = getint(); rap (i, 1, m, 1) { int cmd = getint(); int l = getint(); int r = getint(); if (l > r) std::swap(l, r); if (!cmd) SegmentTree::modify(l, r); else printf("%lld\n", SegmentTree::querySum(l, r)); } return 0; }
|