HDU 4027 ( 线段树 -- 成段更新)

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

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 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.
 

 

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
 

题目大意:

给出一排战船的耐久力,操作有两种,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 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值