HDU 4027 ( 线段树 -- 成段更新)
2013-04-10 21:00
281 查看
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6038 Accepted Submission(s): 1389
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case #1: 19 7 6
题目大意:
给出一排战船的耐久力,操作有两种,T,X,Y,若T==0,则将编号为X到Y的每一只船的耐久力开根;若T==1,则询问编号X到Y的船的耐久力之和。
思路:
由于船的最大耐久力为2^63,注意到该数值最多可以开根7次,然后变成1,1开根后还是1,这里便可以在线段树结构体里引入一个bool flag标记,若该区间所有船的耐久力都为1,则不用再更新下去了。
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #define MAX 100005 7 8 using namespace std; 9 10 struct node 11 { 12 int left,right; 13 __int64 sum; 14 bool flag; 15 }; 16 17 int N,M; 18 __int64 init[MAX]; 19 20 node seg[MAX<<2]; 21 22 void create(int l,int r,int idx) 23 { 24 seg[idx].left=l; 25 seg[idx].right=r; 26 seg[idx].flag=false; 27 if(l == r) 28 { 29 seg[idx].sum=init[seg[idx].left]; 30 if(seg[idx].sum<=1) //标记优化 31 seg[idx].flag=true; 32 return; 33 } 34 int mid=(l+r)>>1; 35 create(l,mid,2*idx); 36 create(mid+1,r,2*idx+1); 37 seg[idx].sum = seg[2*idx].sum + seg[2*idx+1].sum; //向上更新 38 if(seg[2*idx].flag && seg[2*idx+1].flag) //标记 39 seg[idx].flag = true; 40 } 41 42 void update(int l,int r,int idx) 43 { 44 if(seg[idx].flag) //该区间已经都为1了,不用再更新下去了 45 return ; 46 if(seg[idx].left>=l && seg[idx].right<=r && seg[idx].left == seg[idx].right) 47 { 48 seg[idx].sum=(__int64)sqrt(1.0*seg[idx].sum); 49 if(seg[idx].sum<=1) 50 seg[idx].flag = true; 51 return ; 52 } 53 int mid=(seg[idx].left + seg[idx].right)>>1; 54 if(l<=mid) 55 update(l,r,2*idx); 56 if(r>mid) 57 update(l,r,2*idx+1); 58 seg[idx].sum = seg[2*idx].sum + seg[2*idx+1].sum; 59 if(seg[2*idx].flag && seg[2*idx+1].flag) //标记 60 seg[idx].flag = true; 61 } 62 63 __int64 query(int l,int r,int idx) 64 { 65 if(seg[idx].left>=l && seg[idx].right<=r) 66 return seg[idx].sum; 67 int mid=(seg[idx].left + seg[idx].right)>>1; 68 __int64 ret = 0; 69 if(l<=mid) 70 ret += query(l,r,2*idx); 71 if(r>mid) 72 ret += query(l,r,2*idx+1); 73 return ret; 74 } 75 76 int main() 77 { 78 int Case=0; 79 while(~scanf("%d",&N)) 80 { 81 Case ++; 82 for(int i=1;i<=N;i++) 83 scanf("%I64d",&init[i]); 84 create(1,N,1); 85 scanf("%d",&M); 86 printf("Case #%d:\n",Case); 87 while(M--) 88 { 89 int oper,l,r; 90 scanf("%d%d%d",&oper,&l,&r); 91 if(l>r) 92 { 93 int temp=l; 94 l=r; 95 r=temp; 96 } 97 if(oper == 0) 98 update(l,r,1); 99 else 100 { 101 __int64 ans = query(l,r,1); 102 printf("%I64d\n",ans); 103 } 104 } 105 printf("\n"); 106 } 107 return 0; 108 }
相关文章推荐
- HDU4027——线段树成段更新——Can you answer these queries?
- HDU 4027 Can you answer these queries(线段树 成段更新)
- HDU-1698 Just a Hook 线段树成段更新
- HDU 3397 Sequence operation(线段树:成段更新,查询连续目标子串长度)
- hdu 4893(线段树 成段更新+区间合并)
- hdu 1698 Just a Hook(线段树 成段更新)
- HDU 6203 ping ping ping lca 线段树成段更新
- HDU 4027 Can you answer these queries? (线段树)
- HDU-4027-Can you answer these queries? (线段树,区间开平方,区间求和)
- HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】
- Can you answer these queries? HDU - 4027 线段树 开方和
- [ACM] hdu 1698 Just a Hook (线段树,成段更新,懒惰标记)
- hdu 4027 Can you answer these queries? 求区间N次根的和 线段树
- HDU 4027 Can you answer these queries? (线段树)
- hdu 1542线段树(成段更新+离散化)
- Can you answer these queries? (HDU - 4027)(线段树区间更新-区间查询)
- HDU 1698 Just a Hook(线段树,成段更新)
- hdu 1698 Just a Hook(线段树之 成段更新)
- hdu 1698 线段树成段更新
- HDU 4027 <线段树,区间√>