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. linux ab 压测

    https://www.cnblogs.com/shenshangzz/p/8340640.html https://www.cnblogs.com/shenshangzz/p/8340640.htm ...

  2. NOIP2019(CSP2019) 游记

    NOIP2019(CSP2019) 游记 近一年的似乎也就是感觉比别的学校的同学水平低的不止一点,到现在也没有搞清楚大概应该怎么科学有效的练习,并不会思考"为什么想不到"和&quo ...

  3. easyuUI实现客户分页显示逻辑分析

    页面 前端 前端easyUI,自带分页功能,添加pagination属性 前端会传给后端两个属性: page:当前页码 rows:每页显示记录数 后端 接收page和rows参数 根据参数分页查询 获 ...

  4. sharepoint 2010 创建自定义的ASP.NET Web Service (上)

    项目背景 根据客户需求在SharePoint 2010 中创建自定义的ASP.NET Web Service可以分为3种方式(我所知道的).废话少说,下面一一列举: 创建方式 MSDN 官方博客自己的 ...

  5. axios 各种请求方式传递参数

    get delete 方法较为不同 注意:每个方法的传参格式不同,具体用法看下方 get请求方式将需要入参的数据作为 params 属性的值,最后整体作为参数传递 delete请求方式将将需要入参的数 ...

  6. 对Nuxt的研究

    Nuxt就是基于Vue的一个应用框架,采用服务端渲染,让你的SPA应用(Vue)也可以拥有SEO Nuxt的生命周期有些在服务端(Node),客户端,甚至两边都在: 1.其他之前都不存在Window对 ...

  7. 关于反射和动态代理和AOP

    package Exercise.reflect; /** * 反射把java中所有的东西都当做对象,甚至是类的本身也作为一种对象,并把它作为Class的对象的实例: * 反射是把类.类的属性.方法都 ...

  8. JavaScript三元运算符以及运算符顺序

    三目运算符(三元运算符) 三目运算符:运算符需要三个操作 语法:表达式1?表达式2:表达式3 表达式1是一个条件,值为Boolean类型 若表达式1的值为true,则执行表达式2的操作,并且以表达式2 ...

  9. 好用的jQuery分页插件

    插件源代码: (function ($) { $.fn.extend({ smartpaginator: function (options) { var settings = $.extend({ ...

  10. kohana orm巧用字段备注支持扩展

    1.SELECT * FROM `bota_language` WHERE `type` = 'order_type'; id  key     value      type        ---- ...