题目

题意:

  初始给你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. keystone 域中项目、用户、角色的创建

    keystone命令现在全是改成了openstack!!!!!!!!!!!!! Create the service project: $ openstack project create --dom ...

  2. torch7安装的坑

    https://github.com/torch/torch7/issues/1086 sudo su export TORCH_NVCC_FLAGS="-D__CUDA_NO_HALF_O ...

  3. PHP 死锁问题分析

    背景:对于死锁的问题,人们往往想到出现一些关于访问很缓慢,有白页现象,要是测试环境(我就真实遇到测试环境有本文谈及一样的问题)你也就重启一下PHP的php-fpm进程发现又好了,隔一段时间又出类似的问 ...

  4. 百度小程序上传失败 ServerError:30010的原因

    最近通过uniapp编译百度智能小程序后上传遇到了报错,错误码为30010. 原因很简单开发者工具和版本库产生了冲突. 两个解决方案,升级开发者工具,降低发布时的版本库 exit;

  5. c#动态类转json,再由json转xml

    直接上代码了,多说无意了. using System; using System.Collections; using System.Collections.Generic; using System ...

  6. vue封装一个简单的div框选时间的组件

    记录一下我前段时间封装的一个vue组件吧.技术需要积累,有时间我把我之前写的还不错的组件都开源出来.并尝试vue和react 两种方式的组件封装.今天简单写下鼠标框选div选中效果的封装吧. div框 ...

  7. Magic Line(思维+计算几何问题)(2019牛客暑期多校训练营(第三场))

    示例: 输入: 140 1-1 01 00 -1 输出:-1 999000000 1 -999000001 题意:给定平面上一系列的点,求一条以(x1,y1),(x2,y2)两点表示的直线将平面分为包 ...

  8. Jmeter_数据库

    1.准备一个有测试数据表的mysql数据库 2.在测试计划面板点击“浏览..." 按钮,将你的JDBC驱动添加进来.         需要安装插件   mysql-connector-jav ...

  9. 最清晰易懂的Mysql CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP区别

    timestamp数据类型 Mysql数据库中,当字段类型为timestamp(时间戳)时,如果默认值取CURRENT_TIMESTAMP,则在insert一条记录时,此时的值自动设置为系统当前时间, ...

  10. .Net Core WebApi(1)— 入门

    主要讲述利用EF Core的CodeFirst迁移数据库,简单接口增删改查的使用,利用Swagger生成接口文档. 1.新建项目 创建DbContext 和实体模型