[HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027
RT,该题要求每次更新是更新所有节点,分别求平方根,查询是求和。昨晚思前想后找有没有一个数学上的开平方的和等于和的开平方之类的规律。但是想了想发现这不就是小的时候,如果你这么想那老师就会骂死你的那个- -!
所以显然这个题是无法按套路成段更新了,懒惰标记也是没有用了,我们应该去按照区间更新每一个节点。结果TLE了一发,这说明这题不是这么搞,一定还有规律的。注意到题目给数据规模是2^63以及题目红字所述:开根号全都取下整,除了考虑用longlong以外,我们其实还可以想一下对一个longlong的数据开平方,最终都会变成1。那么在更新这个节点至1的那次update里,更新结束后一定会从叶子rt向上更新父亲,如果rt的兄弟节点也是1,那么说明rt的父亲已经不再需要更新了(因为1开平方还是1),这时候rt的父亲存的结果是1+1=2。也就是(rt+1-rt+1)。推广到更高的节点,换成区间来表示就是(r-l+1)。我们遇到这种节点就不需要更新了。
特别需要注意的是题目中x和y的大小…会出现x>y的情况……以及,每个case最后都要有一个额外的\n…………(PE到死WA到死)
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; #define fr first
#define sc second
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%I64d", &a)
#define Rs(a) scanf("%s", a)
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(LL i = 0; i < (len); i++)
#define For(i, a, len) for(LL i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a)) typedef long long LL;
#define lrt rt << 1
#define rrt rt << 1 | 1
const LL maxn = ;
LL sum[maxn<<];
LL n, q; void pushUP(LL rt) {
sum[rt] = sum[lrt] + sum[rrt];
} void build(LL l, LL r, LL rt) {
if(l == r) {
Rll(sum[rt]);
return;
}
LL m = (l + r) >> ;
build(l, m, lrt);
build(m+, r, rrt);
pushUP(rt);
} void update(LL L, LL R, LL l, LL r, LL rt) {
if(sum[rt] == r - l + ) return;
if(l == r) {
sum[rt] = LL(double(sqrt(sum[rt])));
return;
}
LL m = (l + r) >> ;
if(m >= L) update(L, R, l, m, lrt);
if(m < R) update(L,R, m+, r, rrt);
pushUP(rt);
} LL query(LL L, LL R, LL l, LL r, LL rt) {
if(l >= L && R >= r) return sum[rt];
LL m = (l + r) >> ;
LL ret = ;
if(m >= L) ret += query(L, R, l, m, lrt);
if(m < R) ret += query(L, R, m+, r, rrt);
return ret;
} int main() {
// FRead();
LL orz = ;
LL a, b, c;
while(~Rll(n)) {
Cls(sum);
printf("Case #%I64d:\n", orz++);
build(, n, );
Rll(q);
while(q--) {
Rll(a); Rll(b); Rll(c);
if(b > c) swap(b, c);
if(a == ) update(b, c, , n, );
if(a == ) cout << query(b, c, , n, ) << endl;
}
printf("\n");
}
return ;
}
[HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)的更多相关文章
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- 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 ...
- 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 ...
- HDU-4027-Can you answer these queries?线段树+区间根号+剪枝
传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- HDU4027 Can you answer these queries? 线段树
思路:http://www.cnblogs.com/gufeiyang/p/4182565.html 写写线段树 #include <stdio.h> #include <strin ...
- 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 ...
- HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
- hdu4027Can you answer these queries?(线段树)
链接 算是裸线段树了,因为没个数最多开63次 ,开到不能再看就标记.查询时,如果某段区间被标记直接返回结果,否则继续向儿子节点更新. 注意用——int64 注意L会大于R 这点我很纠结..您出题人故意 ...
随机推荐
- 常用汉字的Unicode码表
\u96d5\u864e\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u77 ...
- iOS10和Xcode8适配
1 Xib文件的注意事项 使用Xcode8打开xib文件后,会出现下图的提示. 大家选择Choose Device即可. 之后大家会发现布局啊,frame乱了,只需要更新一下frame即可.如下图 注 ...
- HTML+CSS入门
<strong>加粗</strong> <em>斜体</em> <p>段落</p> <span>设置单独样式< ...
- 1561:The more, The Better - hdu
Problem DescriptionACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些 ...
- c++ g++3.4.5 g++4.8.2 由编译器引起的编译异常
#include <memory> #include <string> #include <iostream> class Student { public: St ...
- PHP对XML文件操作类讲解
<?phpclass XML{ private $dom; function __construct () { $this->dom = new D ...
- Web Server 和 HTTP 协议
https://toutiao.io/posts/xm2fr/preview 一直在找实习,有点什么东西直接就在evernote里面记了,也没时间来更新到这里.找实习真是个蛋疼的事,一直找的是困难模式 ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- sql中时间的比较方法
--------------------------------------------------------------------1. 当前系统日期.时间select getdate() 2. ...
- Javascript操作元素属性方法总结
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...