Can you answer these queries? HDU 4027 线段树

题意

是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来值开根号下的值。有两种操作,第一种就是上面描述的那种,第二种就是询问某个区间内的船的战斗值的总和。

解题思路

使用线段树就不用多说了,关键是如果不优化的话会超时,因为每次修改都是需要递归到叶子节点,很麻烦,但是我们发现,如果一个叶子节点的值已经是1的话,那个再开方它也是1,不变,这样我们就只需要判断父节点的值是不是区间的长度就可以了。

具体就在update函数里面。

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<cmath>
# define ls (rt<<1)
# define rs (rt<<1|1)
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
struct node{
int l, r;
ll sum;
}t[maxn<<2];
ll num[maxn];
int n, m;
void up(int rt)
{
t[rt].sum=t[ls].sum+t[rs].sum;
}
void build(int rt, int l, int r)
{
t[rt].l=l;
t[rt].r=r;
if(l==r)
{
t[rt].sum=num[l];
return ;
}
int mid=(l+r)>>1;
build(ls, l, mid);
build(rs, mid+1, r);
up(rt);
}
void update(int rt, int l, int r)
{
if(t[rt].sum==t[rt].r - t[rt].l+1) //如果发现父节点的值等于它的区间长度的话就可直接返回了,不用再进行递归。
{
return ;
}
if(t[rt].l==t[rt].r )
{
t[rt].sum=(ll)sqrt(t[rt].sum);
return ;
}
int mid=(t[rt].l+t[rt].r)>>1;
if(l<=mid) update(ls, l, r);
if(r>mid) update(rs, l, r);
up(rt);
}
ll query(int rt, int l, int r)
{
if(l <= t[rt].l && t[rt].r <= r)
{
return t[rt].sum;
}
ll ans=0;
int mid=(t[rt].l+t[rt].r)>>1;
if(l<=mid) ans+=query(ls, l, r);
if(r>mid) ans+=query(rs, l, r);
return ans;
}
int main()
{
int ca=1;
while(scanf("%d", &n)!=EOF)
{
for(int i=1; i<=n; i++)
{
scanf("%lld", &num[i]);
}
build(1, 1, n);
int op;
int x, y;
printf("Case #%d:\n", ca++);
scanf("%d", &m);
for(int i=1; i<=m; i++)
{
scanf("%d%d%d", &op, &x, &y);
if(op==1)
{
if(x>y) swap(x, y);
printf("%lld\n", query(1, x, y));
}
else
{
if(x>y) swap(x, y);
update(1, x, y);
}
}
printf("\n");
}
return 0;
}

Can you answer these queries? HDU 4027 线段树的更多相关文章

  1. V - Can you answer these queries? HDU - 4027 线段树 暴力

    V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...

  2. Can you answer these queries? HDU - 4027 (线段树,区间开平方,区间求和)

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

  3. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  4. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

  5. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  6. Can you answer these queries III(线段树)

    Can you answer these queries III(luogu) Description 维护一个长度为n的序列A,进行q次询问或操作 0 x y:把Ax改为y 1 x y:询问区间[l ...

  7. hdu4027Can you answer these queries?【线段树】

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

  8. 2018.10.16 spoj Can you answer these queries V(线段树)

    传送门 线段树经典题. 就是让你求左端点在[l1,r1][l1,r1][l1,r1]之间,右端点在[l2,r2][l2,r2][l2,r2]之间且满足l1≤l2,r1≤r2l1\le l2,r1 \l ...

  9. GSS1 - Can you answer these queries I(线段树)

    前言 线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了. Solution 考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相 ...

随机推荐

  1. tensorflow打印pb、ckpt模型的参数以及在tensorboard里显示图结构

    打印pb模型参数及可视化结构import tensorflow as tf from tensorflow.python.framework import graph_util tf.reset_de ...

  2. Arduino-原理图标识

    VCC    电源正极    VDD GNG    电源负极     VSS vin        表示输入电源 TXD RXD 是主板串口通信用的接口,TXD表示发送数据,RXD表示接收数据,还有简 ...

  3. CSS3——PC以及移动端页面适配方法(响应布局)

    响应布局就是不同宽度应用不同的样式块,每个样式块对应的是该宽度下的布局方式,从而使页面适应不同宽度. <!DOCTYPE html> <html lang="en" ...

  4. git初步研究2

    $git init Git 使用 git init 命令来初始化一个 Git 仓库,Git 的很多命令都需要在 Git 的仓库中运行,所以 git init 是使用 Git 的第一个命令. 在执行完成 ...

  5. Spark--wordcount(词频降序)

    import org.apache.spark.{SparkConf, SparkContext} object wc2 { def main(args: Array[String]): Unit = ...

  6. SQL 查询表字段长度, 名称, 类型, 存储过程创建和修改时间

    获取存储过程的修改时间和创建时间查询建立时间 --表 select * from sysobjects where id=object_id(N'表名') and xtype='U' --表的结构 s ...

  7. [CF1177B]Digits Sequence (Hard Edition)题解

    一个简单的模拟,首先先计算当前是几位数,然后根据几位数推断当前的数是什么,然后求出该位即可 #include <cstdio> int main(){ long long k; scanf ...

  8. 【bzoj2821】作诗(Poetize)

    *题目描述: 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次,每次只阅 ...

  9. WIN10系统和压缩内存占用磁盘过高的解决方案(亲测有效)

    系统和压缩内存磁盘占用过高的解决方案 最近一段时间玩游戏看视频经常莫名的卡顿,观察发现电脑卡的时候,WIN10进程中的"系统和压缩内存"一项占用了近100%的磁盘空间. 百度搜索到 ...

  10. Codeforces Round #369 (Div. 2) A. Bus to Udayland (水题)

    Bus to Udayland 题目链接: http://codeforces.com/contest/711/problem/A Description ZS the Coder and Chris ...