Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 16260    Accepted Submission(s): 3809

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
 
 
题目链接:HDU 4027
一开始用分块做的,可惜无限超时,这题如果用分块写,更新用lazy标记可以做到$sqrt(N)$,但是求和的复杂度不是$sqrt(N)$,因为区间开方之和不等于和的开方。只能用线段树了,显然一个数被向下取整开方太多次只能为0或者1,因此记录一下一个区间被开方的次数即可,小于$2^{63}$的非负数数开7次一定为0或者1,因此如果一个区间被开方次数大于等于7次就不用再继续往下更新了,当然前提是每一次都更新到叶子节点,因为区间只是记录了开方次数,sum得更新到叶子才算更新
代码:
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <numeric>
  7. #include <cstring>
  8. #include <bitset>
  9. #include <string>
  10. #include <deque>
  11. #include <stack>
  12. #include <cmath>
  13. #include <queue>
  14. #include <set>
  15. #include <map>
  16. using namespace std;
  17. #define INF 0x3f3f3f3f
  18. #define LC(x) (x<<1)
  19. #define RC(x) ((x<<1)+1)
  20. #define MID(x,y) ((x+y)>>1)
  21. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  22. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  23. typedef pair<int, int> pii;
  24. typedef long long LL;
  25. const double PI = acos(-1.0);
  26. const int N = 100010;
  27. struct seg
  28. {
  29. int l, mid, r;
  30. LL sum;
  31. int cnt;
  32. };
  33. seg T[N << 2];
  34. LL arr[N];
  35.  
  36. void pushup(int k)
  37. {
  38. T[k].sum = T[LC(k)].sum + T[RC(k)].sum;
  39. }
  40. void build(int k, int l, int r)
  41. {
  42. T[k].l = l;
  43. T[k].r = r;
  44. T[k].mid = MID(l, r);
  45. T[k].sum = T[k].cnt = 0;
  46. if (l == r)
  47. T[k].sum = arr[l];
  48. else
  49. {
  50. build(LC(k), l, T[k].mid);
  51. build(RC(k), T[k].mid + 1, r);
  52. pushup(k);
  53. }
  54. }
  55. void SQ(int k, int l, int r)
  56. {
  57. if (l <= T[k].l && T[k].r <= r)
  58. ++T[k].cnt;
  59. if (T[k].cnt >= 7)
  60. return;
  61. if (T[k].l == T[k].r)
  62. {
  63. T[k].sum = sqrt(T[k].sum);
  64. return ;
  65. }
  66. if (r <= T[k].mid)
  67. SQ(LC(k), l, r);
  68. else if (l > T[k].mid)
  69. SQ(RC(k), l, r);
  70. else
  71. SQ(LC(k), l, T[k].mid), SQ(RC(k), T[k].mid + 1, r);
  72. pushup(k);
  73. }
  74. LL query(int k, int l, int r)
  75. {
  76. if (l <= T[k].l && T[k].r <= r)
  77. return T[k].sum;
  78. else
  79. {
  80. if (r <= T[k].mid)
  81. return query(LC(k), l, r);
  82. else if (l > T[k].mid)
  83. return query(RC(k), l, r);
  84. else
  85. return query(LC(k), l, T[k].mid) + query(RC(k), T[k].mid + 1, r);
  86. }
  87. }
  88. int main(void)
  89. {
  90. int i;
  91. int tcase = 1;
  92. int n, m;
  93. while (~scanf("%d", &n))
  94. {
  95. for (i = 1; i <= n; ++i)
  96. scanf("%I64d", &arr[i]);
  97. build(1, 1, n);
  98. scanf("%d", &m);
  99. printf("Case #%d:\n", tcase++);
  100. while (m--)
  101. {
  102. int l, r, ops;
  103. scanf("%d%d%d", &ops, &l, &r);
  104. if (l > r)
  105. swap(l, r);
  106. if (!ops)
  107. SQ(1, l, r);
  108. else
  109. printf("%I64d\n", query(1, l, r));
  110. }
  111. puts("");
  112. }
  113. return 0;
  114. }

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

  3. HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

    题目 线段树 简单题意: 区间(单点?)更新,区间求和  更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...

  4. hdu 4027 Can you answer these queries? 线段树

    线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...

  5. HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)

    题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...

  6. HDU4027 Can you answer these queries? —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...

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

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

  8. hdu 4027 Can you answer these queries?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...

  9. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

随机推荐

  1. 【转】iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法

    iOS上面对键盘的处理很不人性化,所以这些功能都需要自己来实现, 首先是点击return和屏幕隐藏键盘 这个首先引用双子座的博客 http://my.oschina.net/plumsoft/blog ...

  2. http 调用错误处理

    1. http code 在使用Nginx时,经常会碰到502 Bad Gateway和504 Gateway Time-out错误,下面以Nginx+PHP-FPM来分析下这两种常见错误的原因和解决 ...

  3. java算法面试题:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个, 如“我ABC”,4,应该截取“我AB”,输入“我ABC汉DEF”,6,应该输出“我ABC”,而不是“我ABC+汉的半个”。

    package com.swift; import java.util.Scanner; public class Hanzi_jiequ { public static void main(Stri ...

  4. utf8、ansii、unicode编码之间的转换

    #include "stdafx.h"#include "windows.h"#include <iostream>#include <str ...

  5. 深入浅出:了解JavaScript的六种继承

    了解继承前我们需要了解函数的构造,方便我们理解. 常见六种继承方式: 1.原型继承call和apply: 2.原型拷贝:循环父函数protype的key值=子函数prototype的key值: 3.原 ...

  6. BZOJ2287: 【POJ Challenge】消失之物(背包dp)

    题意 ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢?” ...

  7. 爬虫学习(五)——使用handler管理器对象进行数据爬取的步骤

    # 使用管理器对象进行爬取数据的步骤 import urllib.requesturl = "https://www.baidu.com/"# 创建handler的管理器对象han ...

  8. 二、Shell 变量

    Shell 变量 定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的 ...

  9. Linux-git安装

    基本操作 安装yum install git 生成SSH KEY :先cd ~/.ssh,在这个目录下输入ssh-keygen,一直回车就可以了,这个时候就会出现id_rsd.pub公钥和id_rsa ...

  10. DevOps - 部署系统 - Cobbler

    Cobbler简介 Cobbler由python语言开发,是对PXE和Kickstart以及DHCP的封装.融合很多特性,提供了CLI和Web的管理形式.更加方便的实行网络安装.适用场景:需要大批量的 ...