HDU 4027 Can you answer these queries?
阿新 • • 發佈:2017-08-06
date 全部 swe shanghai man nts less mina query
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.
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.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4027
解題思路:線段樹區間更新,查詢。主要問題在於如何解決快速對一個區間所有數據開根號然後求和 ----- 實際上是根本不用關心這個問題。2^64 在開根號7次之後已經變成1了,繼續開根號沒意義。
因此,對於每次更新,如果這個區間長度和值一樣(全部為1)直接返回,否則更新到點然後更新父節點。
查詢就和普通區間查詢一樣即可。
需要註意X Y的大小關系,加個if(x > y) swap(x, y)即可
代碼:
1 const int maxn = 1e5 + 5; 2 ll A[maxn], tree[maxn * 4]; 3 int n, m; 4 5 void build(int l, int r, int k){ 6 if(l == r){ 7 tree[k] = A[l]; 8 return; 9 } 10 int mid = (l + r) / 2; 11 build(l, mid, k << 1); 12 build(mid + 1, r, k << 1 | 1); 13 tree[k] = tree[k << 1] + tree[k << 1 | 1];14 } 15 void update(int ul, int ur, int l, int r, int k){ 16 if(tree[k] == (r - l + 1)) return; 17 if(ul > r || ur < l) return; 18 if(l == r && tree[k] != 1) { 19 tree[k] = floor(sqrt(tree[k])); 20 return; 21 } 22 23 int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1; 24 update(ul, ur, l, mid, lc); 25 update(ul, ur, mid + 1, r, rc); 26 tree[k] = tree[lc] + tree[rc]; 27 } 28 ll query(int ql, int qr, int l, int r, int k){ 29 if(ql <= l && qr >= r) return tree[k]; 30 if(ql > r || qr < l) return 0; 31 32 int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1; 33 ll ans1 = query(ql, qr, l, mid, lc); 34 ll ans2 = query(ql, qr, mid + 1, r, rc); 35 return ans1 + ans2; 36 } 37 38 int main(){ 39 int t = 1; 40 while(scanf("%d", &n) != EOF){ 41 printf("Case #%d:\n", t++); 42 for(int i = 1; i <= n; i++) 43 scanf("%lld", &A[i]); 44 build(1, n, 1); 45 int m; 46 scanf("%d", &m); 47 while(m--){ 48 int a, b, c; 49 scanf("%d %d %d", &a, &b, &c); 50 if(b > c) swap(b, c); 51 if(a == 0){ 52 update(b, c, 1, n, 1); 53 } 54 else{ 55 printf("%lld\n", query(b, c, 1, n, 1)); 56 } 57 } 58 puts(""); 59 } 60 }
題目:
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 17803 Accepted Submission(s): 4176
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
Source The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
HDU 4027 Can you answer these queries?