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. iOS 网络监听、判断

    一 网络监听 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary ...

  2. JavaWeb框架_Struts2_(八)----->Struts2的国际化

    这一篇博文拖了蛮久了,现在先把它完成,结束struts2这个版块,当然这只是最基础的部分,做项目还需要更深的理解.下一个web后端的版块准备做Spring框架的学习-嗯,加油! 1. Struts2的 ...

  3. Qt之移动硬盘热插拔监控

    最近在做一个通用对话框,类似于windows的资源管理器,当然了没有windwos资源管理器那么强大.用户报了一个bug,说通用对话框打开之后不能实时监控U盘插入,随手在百度上搜索了一圈,这个问题还是 ...

  4. ADODB.Connection、ADODB.RecordSet

    1.数据库连接对象(ADODB. Connection)该对象用于与ODBC数据库建立连接,所有对数据库的操作均通过该连接进行.数据库连接对象ADODB. Connection的作用象Delphi中的 ...

  5. 干货分享!关于APP导航菜单设计你应该了解的一切

    导航菜单是人机交互的最主要的桥梁和平台,主要作用是不让用户迷失方向.现在市面上产品的菜单栏种类繁多,到底什么样的才是优秀的导航菜单设计呢?好的菜单设计不仅能提升整个产品的用户体验,而且还能让用户耳目一 ...

  6. iOS XIB等比例适配

    选择两个视图使其等宽高,再去约束里面就可以设置乘数因子. 简单的一个例子: 要求:设置白色视图的宽度为蓝色视图的一半 1.点击白色视图连线到父视图,选择 Equal Widths     2.选择右边 ...

  7. 扩展1 - Python 获取当前时间的用法

    1.先导入库:import datetime 2.获取当前日期和时间:now_time = datetime.datetime.now() 3.格式化成我们想要的日期:strftime() 比如:“2 ...

  8. Java自己动手写连接池一

    自己动手写连接池,废话不多说,直接上代码,读取配置文件 package com.kama.cn; import java.io.IOException;import java.io.InputStre ...

  9. StringMVC @RequestParam属性

    1.jsp: <a href="springmvc/testRequestParam?username=allen&age=sss">test RequsetP ...

  10. SpringMVC PathVariable和post、get、put、delete请求

    1.PathVariable 可以映射URL中的占位符到目标方法的参数中. 2.Rest风格的URL 以CRUD为例: 新增:/order POST 修改:/order/id PUT 获取:/orde ...