题目

题意:

  初始给你n个数,通过m个操作,  操作0是使区间范围内的每一个a[i]都变成 根号a[i] ,操作1是查询区间范围内数字的和。

思路:

  如果一个节点sum[rt]是1的话,根号1还是1,重复遍历这个节点会大大增加计算次数。n和区间左右端点的范围都 <=1e5,所以一个节点最多遍历不超过10次。

  如果这个节点sum[rt]是1,那么标记这个节点vis[rt]=1,说明这个节点以后不用往下遍历了。如果一个节点的左右子节点vis[rt<<1]=1, vis[rt<<1|1]==1,那么vis[rt]=1。注意longlong 以及输入要多空一行。

#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const ll mod=1e9+;
const int INF= 0x3f3f3f3f;
const int N=1e5+; ll add[N<<],sum[N<<];
ll a[N<<];
int vis[N<<]; /*
struct node
{
int v,p;
}b[N<<2]; bool cmp(node x,node y)
{
return x.v<y.v;
}*/ void push_up(int rt)
{
sum[rt] = sum[rt<<] + sum[rt<<|] ;
vis[rt] = vis[rt<<] & vis[rt<<|] ; //左右子节点的vis都是1 父节点才是1
}
/*
void push_down(int rt,int ln ,int rn)
{
if(add[rt])
{
add[rt<<1]=add[rt<<1|1]=add[rt];
sum[rt<<1]=add[rt]*ln;
sum[rt<<1|1]=add[rt]*rn;
add[rt]=0;
}
}*/
void Built(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=a[l];
if(sum[rt]<=) vis[rt]=;
return;
}
int m=l+r>>;
Built(l,m,rt<<);
Built(m+,r,rt<<|);
push_up(rt);
} void update(int x,int y,int l,int r,int rt)
{
if(l==r)
{
sum[rt]= 1LL*sqrt(sum[rt]*1.0);
if( sum[rt]<= ) vis[rt]=;
return;
}
int m=l+r>>;
//push_down(rt,m-l+1,r-m);
if(x<=m && !vis[rt<<]) update(x,y,l,m,rt<<);
if(m<y && !vis[rt<<|]) update(x,y,m+,r,rt<<|);
push_up(rt);
} ll Query(int x,int y, int l,int r,int rt)
{
if(x<=l && r<=y)
{
return sum[rt];
}
int m=l+r>>;
//push_down(rt,m-l+1,r-m);
ll ans=;
if(x<=m) ans+=Query(x,y,l,m,rt<<);
if(m<y) ans+=Query(x,y,m+,r,rt<<|);
return ans;
}
int main()
{
int x,y,c=,n,m,q;
while(~scanf("%d",&n) )
{
/*for(int i=1;i<=n;i++) scanf("%d",&b[i].v),b[i].p=i;
sort(b+1,b+1+n,cmp);
int cnt=0;
for(int i=1;i<=n;i++)
{
if(b[i].v != b[i-1].v)
cnt++;
a[b[i].p]=cnt;
}//离散化完毕 */
for(int i=;i<=n;i++) scanf("%lld",&a[i]);
mem(vis,);
mem(sum,);
Built(,n,);
scanf("%d",&m);
printf("Case #%d:\n",++c);
while(m--)
{
scanf("%d%d%d",&q,&x,&y);
if(x>y) swap(x,y);
if(q==)
update(x,y,,n,);
else
printf("%lld\n",Query(x,y,,n,) );
}
cout<<endl;
} }

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

  1. HDU 4027 Can you answer these queries? (线段树区间修改查询)

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

  2. hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

    Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  3. HDU4027 Can you answer these queries? —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...

  4. HDU-4027-Can you answer these queries?线段树+区间根号+剪枝

    传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...

  5. HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

    题目 线段树 简单题意: 区间(单点?)更新,区间求和  更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...

  6. hdu 4027 Can you answer these queries? 线段树

    线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...

  7. HDU4027 Can you answer these queries? 线段树

    思路:http://www.cnblogs.com/gufeiyang/p/4182565.html 写写线段树 #include <stdio.h> #include <strin ...

  8. HDU4027 Can you answer these queries?(线段树 单点修改)

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

  9. HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)

    题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...

  10. hdu4027Can you answer these queries?(线段树)

    链接 算是裸线段树了,因为没个数最多开63次 ,开到不能再看就标记.查询时,如果某段区间被标记直接返回结果,否则继续向儿子节点更新. 注意用——int64 注意L会大于R 这点我很纠结..您出题人故意 ...

随机推荐

  1. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  2. ECS与EDAS什么意思?

    1.ECS 英文:Elastic Compute Service  简称云服务器 2.EDAS英文:Enterprise Distributed Application Service   企业级分布 ...

  3. sql 查找所有已经分配部门的员工

    查找所有已经分配部门的员工的last_name和first_nameCREATE TABLE `dept_emp` (`emp_no` int(11) NOT NULL,`dept_no` char( ...

  4. was8.5调用HttpPost使用httpClient-4.5.1.jar与was原生自带jar包冲突

    一.更换jar方法. 1.将httpClient4.5.1.jar包去掉,更换使用commons-httpclient-3.1.jar. 2.更换方法,将HttpPost类转换为PostMethod类 ...

  5. 微服务架构下使用Spring Cloud Zuul作为网关将多个微服务整合到一个Swagger服务上

    注意: 如果你正在研究微服务,那必然少不了服务之间的相互调用,哪么服务之间的接口以及api就必须生成系统的管理文档了.如果你希望更好的管理你的API,你希望有一个工具能一站式地解决API相关的所有事情 ...

  6. 倒数第K个结点

    typedef struct Lnode{ int data; struct Lnode *next; }Lnode,*Link; Link fi(Link head,int k){ Link fa ...

  7. PHP计算两个坐标之间的距离

    <?php /** * 计算两点之间的距离 * @param $lng1 经度1 * @param $lat1 纬度1 * @param $lng2 经度2 * @param $lat2 纬度2 ...

  8. 【LeetCode】寻找两个有序数组的中位数【性质分析+二分】

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...

  9. 从Asp .net到Asp core (第二篇)《Asp Core 的生命周期》

    前面一篇文章简单回顾了Asp .net的生命周期,也简单提到了Asp .net与Asp Core 的区别,我们说Asp Core不在使用Asp.netRuntime,所以它也没有了web程序生命周期中 ...

  10. [转帖]《吊打面试官》系列-Redis基础

    <吊打面试官>系列-Redis基础 https://www.cnblogs.com/aobing/archive/2019/11/07/11811194.html   你知道的越多,你不知 ...