博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4027 Can you answer these queries?
阅读量:4519 次
发布时间:2019-06-08

本文共 1994 字,大约阅读时间需要 6 分钟。

题目:

 

大意:先给你n个数。然后m次操作。。1:访问给定区间的和

0:把给定区间的每个数都开根号

 

注意一个特点。。。。。。总和最多2^63。。。如果只有1个数。。。。也就最多开7次根号。。。。。

即开着开着就恒为1。。。。。。所以,当该区间的和等于区间长度(r-l)+1的时候,该区间的值都不会再更新了。。。

 

通过这题知道:线段树提高效率的关键在于寻找合适的lazy标记,到满足一定条件的时候就不继续更新到点。

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; //Constant Declaration /*--------------------------*/ //#define LL long long #define LL __int64 const int M=100010;// const int INF=1<<30; const double EPS = 1e-11; const double PI = acos(-1.0); /*--------------------------*/ // some essential funtion /*----------------------------------*/ void Swap(int &a,int &b){ int t=a;a=b;b=t; } int Max(int a,int b){ return a>b?a:b; } int Min(int a,int b){ return a
> 1; BuildTree(lson); BuildTree(rson); PushUP(rt); } void Update(int L,int R, int l,int r,int rt) { if (T[rt] == r - l + 1)//lazy { return; } if (l == r) { T[rt] = (LL)sqrt((double)T[rt]);//只能到底层改该点的值。。。 return ; } int m = (l + r) >> 1; if (L <= m) { Update(L, R , lson); } if (R > m) { Update(L, R , rson); } PushUP(rt); } LL Query(int L,int R,int l,int r,int rt) { if (L <= l && r <= R) { return T[rt]; } int m = (l + r) >> 1; LL ret = 0; if (L <= m) ret += Query(L , R , lson); if (m < R) ret += Query(L , R , rson); return ret; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t, case1 = 0; //scanf("%d", &t); int n, m; int i, j; //scanf("%d%d", &n, &m); while (~scanf("%d", &n)) { printf("Case #%d:\n", ++case1); BuildTree(1, n, 1); scanf("%d", &m); while (m--) { int l, r, a; scanf("%d%d%d", &a, &l, &r); if (l > r) { l ^= r, r ^= l, l ^= r;//l要小于等于r } if (a == 0) { Update(l, r, 1, n, 1); } else { printf("%I64d\n", Query(l, r, 1, n, 1)); } } puts(""); } return 0; }

 

转载于:https://www.cnblogs.com/qiufeihai/archive/2012/04/05/2433073.html

你可能感兴趣的文章
爬虫入门【10】Pyspider框架简介及安装说明
查看>>
android面试(4)---文件存储
查看>>
(转载) 标准C中的字符串操作函数
查看>>
如何提高android串口kernel log等级
查看>>
Docker快速配置指南
查看>>
Python基础---OS模块 (二)
查看>>
【JS点滴】substring和substr以及slice和splice的用法和区别。
查看>>
awk多模式匹配
查看>>
线段树
查看>>
[javascript]实现登陆界面拖动窗口
查看>>
a span等行内元素加margin属性后无效果解决方案
查看>>
傻瓜式硬盘重装win7系统图文加视频教程
查看>>
BZOJ 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头:统计 + 筛法【调和级数】
查看>>
如果一个人请优雅的活着。
查看>>
验证码
查看>>
Django缓存配置
查看>>
Ubuntu下安装 Mysql
查看>>
LeetCode--Reverse Integer
查看>>
PHP_APC+Ajax实现的监视进度条的文件上传
查看>>
计算机网络课堂笔记3.29
查看>>