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. Django【第16篇】:Django之Form组件自定义验证规则

    自定义验证规则以及中间件简单介绍 1.python2和python3中的区别 对于python2内置的字符串类型有str和unicode 比如:"abc"是字符串,u"你 ...

  2. CentOS8 安装部署Apache+Php+MariaDB(pdo扩展)

    使用新的CentOS8系统架设PHP服务器,因现在主流数据库mysql已闭源了,所以现在改为使用MariaDB.而php7以后不支持mysqli链接,只有pdo方式,为了安装pdo扩展,所以重新编译安 ...

  3. CF543B Destroying Roads 枚举 + 思维 + BFS

    Code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s".in", ...

  4. 货币系统 Money Systems

    母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对货币的数值感到好奇. 传统地,一个货币系统是由1,,, 或 ,, 和 100的单位面值组成的. 母牛想知道有 ...

  5. 170815-关于Session知识点

    Cookie:实际上就是一个头.               服务器会创建Cookie,并且将Cookie以一个响应头的形式发送给浏览器               浏览器收到Cookie以后,会保存 ...

  6. [CSP-S模拟测试]:老司机的狂欢(LIS+LCA)

    题目背景 光阴荏苒.不过,两个人还在,两支车队还在,熟悉的道路.熟悉的风景,也都还在.只是,这一次,没有了你死我活的博弈,似乎和谐了许多.然而在机房是不允许游戏的,所以班长$XZY$对游戏界面进行了降 ...

  7. 使用eclipse导入新项目时中文出现乱码问题

    有时候在github上看到别人不错的项目想要拉下来学习学习的时候,总会出现这样的情况,实在蛋疼. 一般出现这种问题,会有三个地方需要改动: 在项目上右键选择 properties 将 text fil ...

  8. 百度地图 API 及使用

    如果我们想使用地图的功能,我们就得使用别人的接口,百度地图无疑是个不错的选择 百度地图的网址:http://lbsyun.baidu.com/ 我们想使用里面的功能,就必须要获取密钥 如果时第一次使用 ...

  9. CentOS yum 安装历史版本 java

    1.以1.6为例,找到对应版本 $ yum --showduplicate list java* |grep 1.6 java--openjdk.x86_64 :1.6.0.41-1.13.13.1. ...

  10. ijkplayer阅读笔记系列<转>

    http://blog.csdn.net/peckjerry/article/details/47663275