题目链接:https://vjudge.net/problem/HDU-4027

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.

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

题解:

1.因为对区间的操作是:对每个数进行开根,所以不能像以前加减操作那样,也直接对区间的和进行操作(因为:a+b=sum 不能推出 根号(a)+根号(b) = 根号(a+b))。

2.根据上一点,所以在线段树中,我们只能对一段区间一直更新到每一个元素,然后再push_up求和。但是,如果每个操作都如此,那复杂度得多大?直接用数组维护比线段树更快,那要线段树何用?

3.再回看题目,数据最大为2^64,然后操作是对其开根,我们可以知道对1开根还是1,即当一个值是1时,我们不需要对其进行操作了。推广到一个区间:如果这个区间的所有元素都为1,那么我们也不需要再对这个区间进行操作。那么我们对2^64逐次开根:2^32 2^16 2^8 2^8 2^4 2^2 2^1 1,可以发现,一个数最多只需开7次根就会变成1,操作量很小了。所以:利用线段树进行维护,当一段区间的所有元素都为1,那么我们直接返回;否则更新至叶子结点(深入到每个元素)。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; LL sum[MAXN<<]; void push_up(int u)
{
sum[u] = sum[u*] + sum[u*+];
} void build(int u, int l, int r)
{
if(l==r)
{
scanf("%lld", &sum[u]);
return;
} int mid = (l+r)>>;
build(u*, l, mid);
build(u*+, mid+, r);
push_up(u);
} void attack(int u, int l, int r, int x, int y)
{
if(l==r)
{
sum[u] = (LL)sqrt(sum[u]);
return;
}
//如果这段区域的每个值都为1,那么就无需再执行操作了
if(x<=l && r<=y && sum[u]==1LL*(r-l+)) return; int mid = (l+r)>>;
if(x<=mid) attack(u*, l, mid, x, y);
if(y>=mid+) attack(u*+, mid+, r, x, y);
push_up(u);
} LL query(int u, int l, int r, int x, int y)
{
if(x<=l && r<=y)
return sum[u]; LL ret = ;
int mid = (l+r)>>;
if(x<=mid) ret += query(u*, l, mid, x, y);
if(y>=mid+) ret += query(u*+, mid+, r, x, y);
return ret;
} int main()
{
int n, m, kase = ;
while(scanf("%d", &n)!=EOF)
{
build(, , n);
scanf("%d", &m);
printf("Case #%d:\n", ++kase);
for(int i = ; i<=m; i++)
{
LL op, x, y;
scanf("%lld%lld%lld", &op, &x, &y);
int xx = min(x, y), yy = max(x, y);
if(op==) attack(, , n, xx, yy );
else printf("%lld\n", query(, , n, xx, yy) );
}
printf("\n");
}
}

HDU4027 Can you answer these queries? —— 线段树 区间修改的更多相关文章

  1. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

  2. HDU4027 Can you answer these queries?(线段树 单点修改)

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  3. HDU4027 Can you answer these queries? 线段树

    思路:http://www.cnblogs.com/gufeiyang/p/4182565.html 写写线段树 #include <stdio.h> #include <strin ...

  4. hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

    Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  5. HDU-4027-Can you answer these queries?线段树+区间根号+剪枝

    传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...

  6. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  7. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  8. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  9. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

随机推荐

  1. 创建ArrayList集合对象并添加元素

    ArrayListDemo.java import java.util.ArrayList; /* * 为什么出现集合类: * 我们学习的是面向对象编程语言,而面向对象编程语言对事物的描述都是通过对象 ...

  2. js的几个可能不清晰的问题

    一、关于全局变量的 var test=function(){ var a=1; setTimeout(function(){ console.log(a); a=2; },1000); a=3; se ...

  3. 大数据学习——hadoop安装

    上传centOS6.7-hadoop-2.6.4.tar.gz 解压 tar -zxvf centOS6.7-hadoop-2.6.4.tar.gz hadoop相关修改配置 1 修改 /root/a ...

  4. Leetcode 274.H指数

    H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...

  5. POJ 2420 A Star not a Tree?【爬山法】

    题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...

  6. hdu 1565 状态压缩dp

    #include<stdio.h> #include<string.h> int Max(int a,int b) { return a>b?a:b; } int dp] ...

  7. github新建本地仓库,再同步远程仓库基本用法

    github新建本地仓库,再同步远程仓库基本用法 1 mkdir gitRepo 2 cd gitRepo 3 git init  #初始化本地仓库 4 git add xxx  #添加要push到远 ...

  8. Session保存用户名到Session域对象中

    Session保存用户名 1.构造登录界面 用户名: 密   码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE html> < ...

  9. CDN是什么与CDN加速的原理

    CDN是什么 CDN全称:Content Delivery Network或Content Ddistribute Network,即内容分发网络 CDN设计思路 避让:尽可能避开互联网上有可能影响数 ...

  10. poj——3118 Arbiter

      Arbiter 题目描述:      “仲裁者”是<星际争霸>科幻系列中的一种太空船.仲裁者级太空船是神族的战船,专门提供精神力支援.不像其他战船的人员主要是战士阶级,仲裁者所承载的都 ...