HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
线段树
简单题意:
区间(单点?)更新,区间求和
这道题不用延迟操作
//注意:
//1:查询时的区间端点可能前面的比后面的大;
//2:优化:因为每次更新都是开平方,同一个数更新有限次数就一直是1了,所以可以这样优化
#include <stdio.h>
#include<math.h>
#define N 100010 #define LL __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 LL sum[N<<]; void PushUP(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
} void Build(int l,int r,int rt)
{
if(l==r)
{
scanf("%I64d",&sum[rt]);
return;
}
int m=(l+r)>>;
Build(lson);
Build(rson);
PushUP(rt);
} void Update(int L,int R,int l,int r,int rt)
{ if(l==r)
{
sum[rt]=(__int64)sqrt((double)sum[rt]);
return;
}
int m=(l+r)>>;
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 sum[rt];
int m=(l+r)>>;
LL ret=;
if(L<=m) ret+=Query(L,R,lson);
if(R>m) ret+=Query(L,R,rson);
return ret;
} int main()
{
int m,n,id=;
LL anss;
while(scanf("%d",&n)!=EOF)
{
Build(,n,);
scanf("%d",&m);
printf("Case #%d:\n",id++);
while(m--)
{
int q,a,b,c;
scanf("%d%d%d",&q,&a,&b);
if(a>b)
{
c=a;
a=b;
b=c;
} anss=Query(a,b,,n,);
if(q==)
{
printf("%I64d\n",anss);
}
else
{
if(anss != b-a+)
Update(a,b,,n,);
}
}
printf("\n");
}
return ;
}
虽然我自己不会写线段树,但是以后遇到 区间(单点?)更新,区间求和,这可以当底板修改一下就可以用了,毕竟这里的用法还是蛮好理解的
HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)的更多相关文章
- 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? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
随机推荐
- [转载]--类unix系统如何初始化shell
Shell的配置文件 当我们在linux中打开一个shell的时候,系统会读取相关的配置文件来初始化shell(其他unix-like OS也一样).配置文件包括以下这些: 1. 全局配置(对所有用户 ...
- 内核堆分配函数brk()源码分析
Evernote公开链接:http://www.evernote.com/shard/s133/sh/5b8d3b26-0e53-4c61-aa43-66f6e87bbcb7/a44096dd557f ...
- SQL语句中各种数据类型转换方法总结
1.Access 每个函数都可以强制将一个表达式转换成某种特定数据类型. 语法 CBool(expression) CByte(expression) CCur(expression) CDate(e ...
- Ubuntu14.04 Objective-C hello world
1. Install GNUstep sudo apt-get install gnustep gnustep-devel 2. Write hello world program, and save ...
- ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解
通过终端安装程序sudo apt-get install xxx时出错: E: Could not get lock /var/lib/dpkg/lock - open (11: Reso ...
- 条款9:不要在构造和析构过程中调用virtual函数
如下是一个股票交易的例子: class Transaction // 交易的基类 { public: Transaction(); ; // 用于记录交易日志 }; Transaction::Tran ...
- [转]webrtc学习: 部署stun和turn服务器
[转]webrtc学习: 部署stun和turn服务器 http://www.cnblogs.com/lingdhox/p/4209659.html webrtc的P2P穿透部分是由libjingle ...
- SqlParameter[] 添加
protected void Button1_Click(object sender, EventArgs e) { string strSql="insert into Invoice_R ...
- 华硕电脑安装ubuntu出现问题及决方案
问 题 一:华硕电脑安装ubuntu时无线网络禁用解决方案:打开终端(Ctrl+alt+t)运行命令sudo rmmod acer-wmi,然后开启无线,连接上后便可以上网(附上ubuntu论坛上讨论 ...
- 33.allegro中Autosilk top, Silkscreen top 和Assembly top三个什么区别(转)
Autosilk top, Silkscreen top 和Assembly top Autosilk top:最后出gerber的时候,自动生成的丝印层.会自动调整丝印位置,以及碰到阻焊开窗的地方, ...