Can you answer these queries? HDU 4027 线段树
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 线段树的更多相关文章
- V - Can you answer these queries? HDU - 4027 线段树 暴力
V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...
- 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 ...
- 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 ...
- SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...
- 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 ...
- 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 ...
- hdu4027Can you answer these queries?【线段树】
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- 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 ...
- GSS1 - Can you answer these queries I(线段树)
前言 线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了. Solution 考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相 ...
随机推荐
- jquery每次动态加载dom,绑定事件会多一次,
jquery绑定事件,每次动态加载dom,绑定的事件会加1,比如动态加载dom5次,点那个点击事件会弹出5次 解决办法就是在每次绑定之前解绑定. $('.seek-footer .btn1').off ...
- java 生成透明背景图片
//开始绘图 graphics2d.setBackground(Color.WHITE); graphics2d.clearRect(0, 0, width, height); graphics2d. ...
- 6362. 【NOIP2019模拟2019.9.18】数星星
题目描述 题解 一种好想/好写/跑得比**记者还快的做法: 对所有询问排序,按照R递增的顺序来处理 维护每个点最后一次被覆盖的时间,显然当前右端点为R时的答案为所有时间≥L的点的权值之和 LCT随便覆 ...
- React Native 之ScrollView
import React, { Component } from 'react' import { Text, StyleSheet, View, Button ,TouchableOpacity,A ...
- 1. Spring Security 框架简介
官网:https://projects.spring.io/spring-security/Spring Security 是强大的,且容易定制的实现认证,与授权的基于 Spring 开发的框架.Sp ...
- CF1019E Raining season
https://www.luogu.org/problemnew/show/CF1019E 题解 \[ dis=day*a+b \] \[ b=-day*a+dis \] 然后就变成了斜率优化. 考虑 ...
- Knapsack Cryptosystem
Knapsack Cryptosystem 超大背包 折半查找 #include<bits/stdc++.h> using namespace std; typedef long long ...
- Csharp随机生成序列码的方式Guid方法
主要用于邮箱激活,加密等用处 Guid.NewGuid().ToString()得几种格式显示 .Guid.NewGuid().ToString("N") 结果为: 38bddf4 ...
- win7下面安装flex和bison
通过Cygwin工具进行安装 下载地址cygwin 安装截图: 搜索flex,选择相应的版本进行安装 搜索bison 然后下一步 安装完成后,将D:\cygwin64\bin路径添加到PATH环境变量 ...
- ubuntu命令整理中
系统变量文件: ~/.bashrc 打印系统变量:echo $ANDROID_NDK 查看磁盘空间:df -h 解压缩: .tar.gz 解压:tar zxvf FileName.tar.gz 压缩: ...