首页 > 代码库 > HDU-Can you answer these queries? (线段树+区间修改)
HDU-Can you answer these queries? (线段树+区间修改)
Problem Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
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
思路:
- 这题我一开始还是想用lazy标记做,因为我想每次都单点修改的话应该是会TLE的;
- 但这题实在是没法做标记,因为每个点开方,对于一个区间是无法表示的,不像添加,减少和修改;
- 有意思的地方就是每个点最大2^63,开方最多6、7次就一定可以达到1,1的话再怎么开方都是无用功,因此update的时候加个判断,如果sumv[o] == R - L + 1, 直接返回就行了;
- 但这题最恶心的地方在于输入数据的L居然会比R大!
Code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define INF 0x3f3f3f3f 4 #define M(a, b) memset(a, b, sizeof(a)) 5 #define lson o<<1 6 #define rson o<<1|1 7 const int maxn = 100000 + 10; 8 int qL, qR; 9 __int64 v, _sum; 10 11 struct SegmentTree { 12 __int64 sumv[maxn<<2]; 13 14 void clear() {M(sumv, 0);} 15 16 void build(int o, int L, int R) { 17 if (L == R) { 18 scanf("%I64d", &v); 19 sumv[o] = v; 20 } 21 else { 22 int M = (L + R) >> 1; 23 build(lson, L, M); 24 build(rson, M+1, R); 25 sumv[o] = sumv[lson] + sumv[rson]; 26 } 27 } 28 29 void maintain(int o, int L, int R) { 30 if (L < R) { 31 sumv[o] = sumv[lson] + sumv[rson]; 32 } 33 } 34 35 void update(int o, int L, int R) { 36 if (qL <= L && R <= qR) { 37 if (sumv[o] == R - L + 1) return; 38 } 39 if (L == R) { 40 sumv[o] = (__int64)sqrt(sumv[o]); 41 } 42 else { 43 int M = (L + R) >> 1; 44 if (qL <= M) update(lson, L, M); 45 if (M < qR) update(rson, M+1, R); 46 } 47 maintain(o, L, R); 48 } 49 50 void query(int o, int L, int R) { 51 if (qL <= L && R <= qR) { 52 _sum += sumv[o]; 53 } 54 else { 55 int M = (L + R) >> 1; 56 if (qL <= M) query(lson, L, M); 57 if (M < qR) query(rson, M+1, R); 58 } 59 } 60 61 }; 62 63 SegmentTree T; 64 65 int main() { 66 int n, q, p, kase = 0; 67 while(~scanf("%d", &n)) { 68 printf("Case #%d:\n", ++kase); 69 T.clear(); 70 T.build(1, 1, n); 71 scanf("%d", &q); 72 while(q--) { 73 scanf("%d", &p); 74 if (p) { 75 scanf("%d%d", &qL, &qR); 76 if (qL > qR) {int temp = qL; qL = qR; qR = temp;} 77 _sum = 0; 78 T.query(1, 1, n); 79 printf("%I64d\n", _sum); 80 } 81 else { 82 scanf("%d%d", &qL, &qR); 83 if (qL > qR) {int temp = qL; qL = qR; qR = temp;} 84 T.update(1, 1, n); 85 } 86 } 87 printf("\n"); 88 } 89 90 return 0; 91 }
HDU-Can you answer these queries? (线段树+区间修改)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。