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(线段树懒操作)的更多相关文章

  1. SP2713 GSS4 - Can you answer these queries IV(线段树)

    传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...

  2. GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)

    GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...

  3. 线段树 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 ...

  4. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  5. SPOJ GSS4 Can you answer these queries IV

    Time Limit: 500MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

  6. 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 ...

  7. 题解【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 ...

  8. 「SP2713」GSS4 - Can you answer these queries IV

    传送门 Luogu 解题思路 区间开方以及区间求和. 考虑用线段树来做. 开方操作看似没有任何结合律可言,但这题有另外一个性质: 一个数的初始值不超过 \(10^{18}\) ,而这个数被开方6次左右 ...

  9. 【SP2713 GSS4 - Can you answer these queries IV】 题解

    题目链接:https://www.luogu.org/problemnew/show/SP2713 真暴力啊. 开方你开就是了,开上6次就都没了. #include <cmath> #in ...

随机推荐

  1. Oracle初级——续续篇

    逝者如斯夫,不舍昼夜 所有的SQL都经过测试,可粘贴,可复制,有问题请各位大神指出...... --约束 与表一起使用 约束不合理的数据,使其不能进入表中? ','李小龙','一班','该学生成天练武 ...

  2. listbox控件使用

    1. 属性列表: SelectionMode    组件中条目的选择类型,即多选(Multiple).单选(Single)    Rows             列表框中显示总共多少行    Sel ...

  3. SpringMVC底层数据传输校验的方案(修改版)

    团队的项目正常运行了很久,但近期偶尔会出现BUG.目前观察到的有两种场景:一是大批量提交业务请求,二是生成批量导出文件.出错后,再执行一次就又正常了. 经过跟踪日志,发现是在Server之间进行jso ...

  4. ES6 对象的扩展(上)

    属性的简介表示法 允许直接写入变量和函数作为对象的属性和方法,这样的书写更简洁. function f( x, y ) { return { x, y }; } // 等同于 function f( ...

  5. linux下后台运行MATLAB

    原帖:http://sypeterli1.blog.163.com/blog/static/2283740492013101745824207/ 后台运行matlab脚本文件的方法:nohup     ...

  6. 浅谈OGNL表达式

    OGNL(Object-Graph Navigation Language):对象视图导航语言 ${user.addr.name}这样的写法就叫对象视图导航 OGNL不仅可以视图导航,支持EL表达式更 ...

  7. 【开源】AspnetCore 2.0 自动API文档生成组件,支持protobuffer

    本文地址 http://www.cnblogs.com/likeli/p/8204054.html 关于 API文档自动生成,用于对APP端的开发帮助文档生成,默认ProtoBuffer传输格式. 本 ...

  8. c# 调用python语言

    config   文件配置 <configuration>节中 第一个的位置插入如下节点,版本根据实际用到的来写 <configSections>    <section ...

  9. Linux 下Beanstalk安装

    1.安装 # wget https://github.com/kr/beanstalkd/archive/v1.10.tar.gz # tar xzvf v1.10 # cd beanstalkd-1 ...

  10. linux ext4无法使用超过16T磁盘的解决办法

    大磁盘使用问题 问题:当所要挂载的设备大于16T的时候,可以用parted正常分区,但是分区完成之后,无法格式化, 报错:Size of device /dev/sdb1 too big to be ...