GSS4 - Can you answer these queries IV(线段树懒操作)
GSS4 - Can you answer these queries IV(线段树懒操作)
标签: 线段树
题目链接
Description
recursion有一个正整数序列a[n]。现在recursion有m次操作:
(A)对于给定的x,y,使所有满足下标i在x,y之间的数a[i]开方下取整。
(B)对于给定的x,y,输出满足下标i在x,y之间的数a[i]的和。
这么简单的题,recursion当然会做啦,但是为了维持她的傲娇属性,她决定考考你。
Input
包含多组数据,文件以EOF结尾。对于每组数据,第一行包含一个正整数n。第二行包含n个正整数,表示a[n]序列。第三行包含一个正整数m。接下来m行,每行包含三个整数i,x,y。i=0表示修改操作,i=1表示询问操作。
Output
对于每组数据,你需要先输出一个"Case #:",然后接下来每行输出一个询问的答案,最后留一个空行。具体见样例。
Sample Input
5
1 2 3 4 5
5
1 2 4
0 2 4
1 2 4
0 4 5
1 1 5
4
10 10 10 10
3
1 1 4
0 2 3
1 1 4
Sample Output
Case #1:
9
4
6
Case #2:
40
26
Hint
n,m<=100000,保证整个序列的和不超过1018
题意:
中文题意就不说了,但是要注意开根号的特点,一般像开根号,求连续数值的gcd这些都是下降非常快的函数,所以可以通过剪枝来优化复杂度,即满足一定条件就不算了
题解:
这个题就是一个普通的线段树加上一个懒操作,即如果当前的区间和正好等于当前的区间长度的话就不再更新这个节点
注意:
这个题要注意查询区间的正确性,l<r
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define mid (l+r>>1)
#define lc (d<<1)
#define rc (d<<1|1)
const int N = 100004;
ll sum[N<<2];
//int idl[N],idr[N];
//ll mp[N];
//int c;
void build(int l, int r, int d)
{
if(l==r)
{
scanf("%lld",&sum[d]);
// printf("%d ",sum[d]);
// idl[d] = idr[d] = c-1;
return;
}
build(l,mid,lc);
build(mid+1,r,rc);
sum[d] = sum[lc]+sum[rc];
// idl[d] = idl[lc];
// idr[d] = idr[rc];
//printf("%d %d %d\n",d,idl[d],idr[d]);
return;
}
void Update(int L, int R, int l, int r, int d)
{
if(sum[d] == r-l+1) return;
if(l==r){
sum[d] = sqrt((double)sum[d]);
//mp[l] = sum[d];
return;
}
if(R <= mid) Update(L,R,l,mid,lc);
else if(L > mid) Update(L,R,mid+1,r,rc);
else {
Update(L,mid,l,mid,lc);
Update(mid+1,R,mid+1,r,rc);
}
sum[d] = sum[lc]+sum[rc];
return;
/*
if(L <= l && R >= r)
{
if(L==R)
{
sum[d] = pow((double)mp[idl[d]],0.5);
return;
}
for(int i = idl[d]; i <= idr[d]; i++)
{
sum[d] = sum[d]+pow((double)mp[i],0.5)-mp[i];
}
Update(L,R,l,mid,lc);
Update(L,R,mid+1,r,rc);
}
if(L <= mid) Update(L,R,l,mid,lc);
if(R > mid) Update(L,R,mid+1,r,rc);
return;
*/
}
ll query(int L, int R, int l, int r, int d)
{
if(L==l&&R==r)
{
return sum[d];
}
else if(R<=mid) return query(L,R,l,mid,lc);
else if(L>mid) return query(L,R,mid+1,r,rc);
ll t1 = query(L,mid,l,mid,lc);
ll t2 = query(mid+1,R,mid+1,r,rc);
return t1+t2;
}
int main()
{
int n,m,cnt;
cnt = 0;
int id, x, y;
while(~scanf("%d",&n))
{
cnt++;
//memset(sum,0,sizeof(sum));
//for(int i = 1; i <= n; i++)
// {
// scanf("%lld",&mp[i]);
// }
//c = 1;
build(1,n,1);
scanf("%d",&m);
printf("Case #%d:\n",cnt);
//for(int i = 1; i <= m; i++)
while(m--)
{
scanf("%d%d%d",&id,&x,&y);
if(x>y) swap(x,y);//这句话很重要。。。不加就超时了
if(id==0)
{
Update(x,y,1,n,1);
}
else if(id==1)
{
ll ans = query(x,y,1,n,1);
printf("%lld\n",ans);
}
}
}
return 0;
}
GSS4 - Can you answer these queries IV(线段树懒操作)的更多相关文章
- SP2713 GSS4 - Can you answer these queries IV(线段树)
传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...
- GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)
GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...
- 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国
SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- SPOJ GSS4 Can you answer these queries IV
Time Limit: 500MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description You are g ...
- SP2713 GSS4 - Can you answer these queries IV
题目大意 \(n\) 个数,和在\(10^{18}\)范围内. 也就是\(\sum~a_i~\leq~10^{18}\) 现在有两种操作 0 x y 把区间[x,y]内的每个数开方,下取整 1 x y ...
- 题解【SP2713】GSS4 - Can you answer these queries IV
题目描述 You are given a sequence \(A\) of \(N(N \leq 100,000)\) positive integers. There sum will be le ...
- 「SP2713」GSS4 - Can you answer these queries IV
传送门 Luogu 解题思路 区间开方以及区间求和. 考虑用线段树来做. 开方操作看似没有任何结合律可言,但这题有另外一个性质: 一个数的初始值不超过 \(10^{18}\) ,而这个数被开方6次左右 ...
- 【SP2713 GSS4 - Can you answer these queries IV】 题解
题目链接:https://www.luogu.org/problemnew/show/SP2713 真暴力啊. 开方你开就是了,开上6次就都没了. #include <cmath> #in ...
随机推荐
- Python的HTTP服务实例
1.前言 今天需要实现一个Pyhton的http服务,与Web的JS进行交换. 2.实例代码 支持HEAD.GET.POST方法,将参数转换为JSON格式,返回结果以JSON字符串返回. import ...
- bzoj 2727: [HNOI2012]双十字
Description 在C 部落,双十字是非常重要的一个部落标志.所谓双十字,如下面两个例子,由两条水平的和一条竖直的"1"线段组成,要求满足以下几个限制: 我们可以找到 5 个 ...
- ecsmart的开发经历
ecsmart是ecshop的产品之一,是2015年发布的一套系统.关于它的介绍请到官网去了解,也可以百度 “商之翼” “ecsmart” 1.ecsmart分别在pc.mobile.app三个方面都 ...
- lesson - 5 课程笔记 which/ type / whereis /locate /pwd / etc/passwd/ shadow/ group / gshadow /useradd /usermod /userdel /passwd / su sudo
一.which 作用: which 命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录, which 命令会在环境变量$PATH 设置的目录里查找符合条件的文件.也 ...
- Integration Services 服务连接失败,拒绝访问以及无法检索数据报错问题
第一个方法比较简单:把域账号添加admin组即可: 第二种方法: 添加域账号到分布式 COM 组 命令提示符下运行 dcomcnfg.exe 下一步 下一步 启动和激活权限 下一步 访问权限 同上设置 ...
- Robot Framework 学习笔记(二)-------第一个脚本
robot Framework环境搭建好之后先来一个简单的脚本跑一下 一.新建项目 二.新建测试套件 三.创建测试用例 四.导入Selenium2Library库 因为RF框架编写基于web 的测试 ...
- java的Xmx是设置什么的?
我们使用java -X可以看到java的-X系列的参数,Xmx和Xms是相对应的.一个是memory max(Xmx) 一个是memory start (Xms). Xmx代表程序最大可以从操作系统中 ...
- Micro Templating源码分析
关于模板,写页面的人们其实一直在用,asp.net , jsp , php, nodejs等等都有他的存在,当然那是服务端的模板. 前端模板,作为前端人员肯定是多少有接触的,Handlebars.js ...
- HDFS Federation
http://hadoop.apache.org/docs/r2.9.0/hadoop-project-dist/hadoop-hdfs/Federation.html Background HDFS ...
- Scala 简介
Scala 特性 面向对象特性 Scala是一种纯面向对象的语言,每个值都是对象.对象的数据类型以及行为由类和特质描述. 类抽象机制的扩展有两种途径:一种途径是子类继承,另一种途径是灵活的混入机制.这 ...