You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
InputThe input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2
63.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
OutputFor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.Sample Input
10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
Sample Output
Case #1: 19 7 6
题解:注意到了开方最多的次数为7次,就是不知道怎么用。。。结果暴力更新,更新多次的不更新,否则直接更新到叶节点。
1 #pragma warning(disable:4996) 2 #include<cstdio> 3 #include<cmath> 4 #include<string> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 10 #define lson root<<1 11 #define rson root<<1|1 12 #define ll long long 13 14 const int maxn = 100005; 15 16 int n, q; 17 ll sum[4 * maxn]; 18 19 void Pushup(int root) { 20 sum[root] = sum[lson] + sum[rson]; 21 } 22 23 void Build(int l, int r, int root) { 24 if (l == r) { 25 scanf("%lld", &sum[root]); 26 return; 27 } 28 int mid = (l + r) >> 1; 29 Build(l, mid, lson); 30 Build(mid + 1, r, rson); 31 Pushup(root); 32 } 33 34 void Update(int L, int R, int l, int r, int root) { 35 if (l > R || r < L) return; 36 if (L <= l && r <= R && sum[root] == (r - l + 1)) return; //判断是否更新了多次 37 if (l == r) { 38 sum[root] = sqrt(sum[root]); return; 39 } 40 int mid = (l + r) >> 1; 41 Update(L, R, l, mid, lson); 42 Update(L, R, mid + 1, r, rson); 43 Pushup(root); 44 } 45 46 ll Query(int L, int R, int l, int r, int root) { 47 if (l > R || r < L) return 0; 48 if (L <= l && r <= R) return sum[root]; 49 int mid = (l + r) >> 1; 50 ll ans = 0; 51 ans += Query(L, R, l, mid, lson); 52 ans += Query(L, R, mid + 1, r, rson); 53 return ans; 54 } 55 56 int main() 57 { 58 int kase = 0; 59 while (scanf("%d", &n) != EOF) { 60 Build(1, n, 1); 61 scanf("%d", &q); 62 printf("Case #%d: ", ++kase); 63 for (int i = 1; i <= q; i++) { 64 int op, x, y; 65 scanf("%d%d%d", &op, &x, &y); 66 if (x > y) swap(x, y); //坑点!!!!!!! 67 if (op == 0) Update(x, y, 1, n, 1); 68 else printf("%lld ", Query(x, y, 1, n, 1)); 69 } 70 printf(" "); 71 } 72 return 0; 73 }