Can you answer these queries? (线段树
题意:
初始给你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? (线段树的更多相关文章
- 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 ...
- 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 ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- HDU-4027-Can you answer these queries?线段树+区间根号+剪枝
传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- HDU4027 Can you answer these queries? 线段树
思路:http://www.cnblogs.com/gufeiyang/p/4182565.html 写写线段树 #include <stdio.h> #include <strin ...
- 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 ...
- HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
- hdu4027Can you answer these queries?(线段树)
链接 算是裸线段树了,因为没个数最多开63次 ,开到不能再看就标记.查询时,如果某段区间被标记直接返回结果,否则继续向儿子节点更新. 注意用——int64 注意L会大于R 这点我很纠结..您出题人故意 ...
随机推荐
- Java语言和JVM的使用说明书
Java Language and Virtual Machine Specifications 详细介绍了Java语言各个版本的功能/JVM的工作原理 建议详读/深度 各个版本,在线看或者pdf下载
- java.sql.SQLException: Zero date value prohibited
今天使用mybatis出现了异常 java.sql.SQLException: Zero date value prohibited 查了下原因 mysql文档上写着 Datetimes with a ...
- H5混合开发中android终端和ios终端常见的兼容问题2
转自 https://www.cnblogs.com/stoneniqiu/p/6077112.html 1.ios键盘挡住输入框. setInterval(function () { if (doc ...
- 百练1724 ROADS
总时间限制: 1000ms 内存限制: 65536kB 描述 N cities named with numbers 1 ... N are connected with one-way roa ...
- 为什么Redis单线程却能支撑高并发?
作者:Draveness 原文链接:draveness.me/redis-io-multiplexing 最近在看 UNIX 网络编程并研究了一下 Redis 的实现,感觉 Redis 的源代码十分适 ...
- java笔记4—继承
继承: 作用: 提高了代码的复用性. 让类与类之间产生了关系,为多态提供了前提 继承中成员变量的特点: 继承中成员函数的特点: 方法重写: 注意:重写方法必须和被重写的方法具有相同的方法名,参数列表和 ...
- Matplotlib:绘图和可视化
Matplotlib:绘图和可视化 简介 简单绘制线形图 plot函数 支持图类型 保存图表 一 .简介 Matplotlib是一个强大的Python绘图和数据可视化的工具包.数据可视化也是我们数据分 ...
- Numpy学习笔记(上篇)
目录 Numpy学习笔记(上篇) 一.Jupyter Notebook的基本使用 二.Jpuyter Notebook的魔法命令 1.%run 2.%timeit & %%timeit 3.% ...
- IDEA远程DEBUG Tomcat配置
IDEA远程DEBUG Tomcat配置 IDEA远程DEBUG Tomcat很简单,配置如下: 1.修改tomcat服务器配置 打开tomcat/bin/catalina.sh 在空白处添加如下参数 ...
- python ID3决策树实现
环境:ubuntu 16.04 python 3.6 数据来源:UCI wine_data(比较经典的酒数据) 决策树要点: 1. 如何确定分裂点(CART ID3 C4.5算法有着对应的分裂计算方式 ...