hdu 4027 Can you answer these queries?
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4027
Can you answer these queries?
Description
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.
Input
The 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 263.
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.
Output
For 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
线段树,区间开方求和。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define lc root<<1 8 #define rc root<<1|1 9 #define mid ((l+r)>>1) 10 typedef unsigned long long ull; 11 const int Max_N = 100010; 12 struct Node { ull val; bool flag; }; 13 struct SegTree { 14 Node seg[Max_N << 2]; 15 inline void push_up(int root) { 16 seg[root].val = seg[lc].val + seg[rc].val; 17 } 18 inline void built(int root, int l, int r) { 19 seg[root].flag = false; 20 if (l == r) { 21 scanf("%lld", &seg[root].val); 22 return; 23 } 24 built(lc, l, mid); 25 built(rc, mid + 1, r); 26 push_up(root); 27 } 28 inline void update(int root, int l, int r, int x, int y) { 29 if (x > r || y < l || seg[root].flag) return; 30 if (l == r) { 31 seg[root].val = (ull)sqrt((double)seg[root].val); 32 if (1 == seg[root].val) seg[root].flag = true; 33 return; 34 } 35 update(lc, l, mid, x, y); 36 update(rc, mid + 1, r, x, y); 37 push_up(root); 38 seg[root].flag = seg[lc].flag && seg[rc].flag; 39 } 40 inline ull query(int root, int l, int r, int x, int y) { 41 if (x > r || y < l) return 0; 42 if (x <= l && y >= r) return seg[root].val; 43 ull v1 = query(lc, l, mid, x, y); 44 ull v2 = query(rc, mid + 1, r, x, y); 45 return v1 + v2; 46 } 47 }seg; 48 int main() { 49 #ifdef LOCAL 50 freopen("in.txt", "r", stdin); 51 freopen("out.txt", "w+", stdout); 52 #endif 53 int n, q, a, b, c, k = 1; 54 while (~scanf("%d", &n)) { 55 seg.built(1, 1, n); 56 scanf("%d", &q); 57 printf("Case #%d:\n", k++); 58 while (q--) { 59 scanf("%d %d %d", &a, &b, &c); 60 if (b > c) b ^= c ^= b ^= c; 61 if (!a) seg.update(1, 1, n, b, c); 62 else printf("%lld\n", seg.query(1, 1, n, b, c)); 63 } 64 printf("\n"); 65 } 66 return 0; 67 }