Can You answer these queries?

HDOJ-4027

  • 这道题目和前面做的题目略有不同。以前的题目区间更新的时候都是统一更新的,也就是更新相同的值。但是这里不一样,这里更新的每个叶子结点改变不同。
  • 考虑到数字最大也就64位,所以就算加上开根号的操作,也就最多开7次,所以这里可以转移到update函数中,每次走到叶子结点的时候进行开根号的操作。
  • 在update函数中还有一个细节需要注意,那就是当当前结点所包含的所有叶子结点都是1的时候,就可以不用往下更新了,直接返回。
  • query函数还是和以前的一样,没有新的东西。
  • 针对这题,题目的题干还需要注意,输出的格式。还有就是x,y没有明确说是小于关系,故需要排序。
//区间修改(不同于统一修改,这里是区间中每一项修改的值都不一样)和区间查询
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100005;
int n,m;
long long dur[maxn];
long long sums[maxn<<2];
void pushup(int id,int l,int r){
int lc=id<<1;
int rc=id<<1|1;
sums[id]=sums[lc]+sums[rc];
}
void build(int id,int l,int r){
if(l==r){
sums[id]=dur[l];
return;
}
int mid=(l+r)>>1;
int lc=id<<1;
int rc=id<<1|1;
build(lc,l,mid);
build(rc,mid+1,r);
pushup(id,l,r);
}
void update(int id,int l,int r,int p,int q){
if(sums[id]==r-l+1){//当结点覆盖的叶子结点全是1,也就是说sum等于覆盖的长度时返回,不用继续计算。
return;
}
if(l==r){
sums[id]=sqrt(sums[id]);
return;
}
int mid=(l+r)>>1;
if(p<=mid){
update(id<<1,l,mid,p,q);
}
if(q>mid){
update(id<<1|1,mid+1,r,p,q);
}
pushup(id,l,r);
}
long long query(int id,int l,int r,int p,int q){
long long sum=0;
if(p<=l&&q>=r){
return sum=sums[id];
}
int mid=(l+r)>>1;
int lc=id<<1;
int rc=id<<1|1;
if(p<=mid){
sum+=query(lc,l,mid,p,q);
}
if(q>mid){
sum+=query(rc,mid+1,r,p,q);
}
return sum;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int k=0;
while(cin>>n){
for(int i=1;i<=n;i++){
cin>>dur[i];
}
build(1,1,n);
cin>>m;
cout<<"Case #"<<++k<<":"<<endl;
for(int i=0;i<m;i++){
int t,p,q;
cin>>t>>p>>q;//不保证p<q
int temp=p;
p=min(p,q);
q=max(temp,q);
if(t==0){//update
update(1,1,n,p,q);
}else{//query
cout<<query(1,1,n,p,q)<<endl;
}
}
cout<<endl;
}
return 0;
}

HDOJ-4027(线段树+区间更新(每个节点更新的值不同))的更多相关文章

  1. 线段树&&线段树的创建线段树的查询&&单节点更新&&区间更新

    目录 线段树 什么是线段树? 线段树的创建 线段树的查询 单节点更新 区间更新 未完待续 线段树 实现问题:常用于求数组区间最小值 时间复杂度:(1).建树复杂度:nlogn.(2).线段树算法复杂度 ...

  2. hdoj1754 I Hate It【线段树区间最大值维护+单点更新】

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  4. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  5. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  6. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  7. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  8. HDU5039--Hilarity DFS序+线段树区间更新 14年北京网络赛

    题意:n个点的树,每个条边权值为0或者1, q次操作 Q 路径边权抑或和为1的点对数, (u, v)(v, u)算2个. M i修改第i条边的权值 如果是0则变成1, 否则变成0 作法: 我们可以求出 ...

  9. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  10. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

随机推荐

  1. HDU 3488-Tour KM

    为什么可以这样拆点在 这道题 都已经证明过 代码: 1 //题目上面说了"The only exception is that the first and the last city sho ...

  2. .net中swagger忽略某些字段

    需要忽略的字段上用特性 [System.Text.Json.Serialization.JsonIgnore] 例如:

  3. ucosIII学习笔记——钩子函数

    一开始听见钩子函数感觉很莫名其妙,更不知道它有何作用,这是第一篇博客,也是学习ucosIII操作系统的一个开始吧. 在系统中有开发者自己创建的任务也有系统内部任务 ,UCOSIII中有五个系统任务,分 ...

  4. OpenStack Train版-6.安装nova计算服务(计算节点)

    安装nova计算服务(computel01计算节点 192.168.0.20)安装软件包 yum install centos-release-openstack-train -y yum insta ...

  5. mybatis(三)配置mapper.xml 的基本操作

    参考:https://www.cnblogs.com/wuzhenzhao/p/11101555.html XML 映射文件 本文参考mybatis中文官网进行学习总结:http://www.myba ...

  6. Web 前端如何一键开启上帝模式

    Web 前端如何一键开启上帝模式 God Mode document.designMode = `on`; refs https://www.cnblogs.com/xgqfrms/tag/desig ...

  7. DB-Engines Ranking : Redis, MongoDB, MySQL

    DB-Engines Ranking http://db-engines.com/en/ranking The DB-Engines Ranking ranks database management ...

  8. js 构造函数 & 静态方法 & 原型 & 实例方法

    js 构造函数 & 静态方法 & 原型 & 实例方法 ES5 "use strict"; /** * * @author xgqfrms * @licens ...

  9. how to install GitLab on Raspberry Pi OS

    how to install GitLab on Raspberry Pi OS GitLab & Raspberry Pi refs https://about.gitlab.com/ins ...

  10. leetcode solution cracked tutorial

    leetcode solution cracked tutorial problemset https://leetcode.com/problemset/all/ Top Interview Que ...