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 ...
随机推荐
- JaveScript流程控制(JS知识点归纳四)
01 流程控制 顺序结构: 程序的默认执行方式 条件判断语句:也称之为分支结构,选择结构:如果程序要执行的代码出现了多种情况需要使用 循环结构:当代码需要多次重复执行多次时,使用 02 条件判断语句 ...
- xml文件解析(使用解析器)
一.Xml解析,解析xml并封装到list中的javabean中 OM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在 ...
- GVIM与模板——让FPGA开发变得更简单
还在使用FPGA开发环境自带的代码编辑器?还在逐个字母敲击冗长重复的代码?明德扬至简设计法让你快速提高代码编写效率!利用GVIM这一高效的编辑工具并添加自定义模板,通过简短的脚本命令即可自动生成所有常 ...
- C 函数参数 char **s与char *s[]
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/126 先来看一个小例子 : 编写函数遍历一个整型数组的元素,数组 ...
- lesson - 14 linux系统日常管理3
1. Linux系统服务管理工具ntsysv 类似图形界面管理工具,如果没有该命令使用 yum install -y ntsysv 安装常用服务:crond, iptables, network, s ...
- IT服务(运维)管理实施的几个要点--第二章 人员和组织架构
子曰"没有合适的人" 在流程化的管理模式下,最容易步入的一个误区是按流程设计一个"理想的"组织架构,然后对应于这个架构对人员进行评估.培养,甚至是更换.我见过很 ...
- 妙味课堂:JavaScript初级--第12课:json与数组
1.json数据格式及json语法 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- jQuery基础 (四)——使用jquery-cookie 实现点赞功能
jquery-cookie 下载地址:https://github.com/carhartl/jquery-cookie 直接上代码 html <span class="jieda-z ...
- Windows同时安装Python2、Python3兼容运行
Python2.Python3可以同时安装在windows上,关键是如何有选择的运行Python2还是Python3. 指定运行版本 如果你有一个Python文件叫 hello.py,那么你可以这 ...
- timeline自适应时间轴
近期项目一直有类似QQ空间那样的时间轴,来展示公司新闻动态,或者流程之类的设计UI. 每每出现,不以为然,这次总结了下,精简下 ================= ================== ...