问题描述

LG-SP2713


题解

分块,区间开根。

如果一块的最大值是 \(1\) ,那么这个块就不用开根了。

如果最大值不是 \(1\) ,直接暴力开就好了。


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std; #define int long long template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
} int non; const int maxn=100007; int n,cas;
int a[maxn];
int L[maxn],R[maxn],bel[maxn];
int cnt,blo,mx[maxn],sum[maxn]; void reset(){
memset(mx,0,sizeof(mx));
memset(sum,0,sizeof(sum));
memset(L,0,sizeof(L));
memset(R,0,sizeof(R));
memset(bel,0,sizeof(bel));
} void Init(){
for(int i=1;i<=n;i++) read(a[i]);
} void change(){
int x,y;read(x);read(y);//read(non);
if(x>y) swap(x,y);
if(bel[x]==bel[y]){
if(mx[bel[x]]==1) return;
for(int i=x;i<=y;i++){
sum[bel[i]]-=a[i];
a[i]=sqrt((double)a[i]);
sum[bel[i]]+=a[i];
}
mx[bel[x]]=-1;
for(int i=L[bel[x]];i<=R[bel[x]];i++)
mx[bel[x]]=max(mx[bel[x]],a[i]);
return;
}
if(mx[bel[x]]!=1){
for(int i=x;i<=R[bel[x]];i++){
sum[bel[i]]-=a[i];
a[i]=sqrt((double)a[i]);
sum[bel[i]]+=a[i];
}
mx[bel[x]]=-1;
for(int i=L[bel[x]];i<=R[bel[x]];i++){
mx[bel[x]]=max(mx[bel[x]],a[i]);
}
}
if(mx[bel[y]]!=1){
for(int i=L[bel[y]];i<=y;i++){
sum[bel[i]]-=a[i];
a[i]=sqrt((double)a[i]);
sum[bel[i]]+=a[i];
}
mx[bel[y]]=-1;
for(int i=L[bel[y]];i<=R[bel[y]];i++){
mx[bel[y]]=max(mx[bel[y]],a[i]);
}
}
for(int i=bel[x]+1;i<bel[y];i++){
if(mx[i]==1) continue;
mx[i]=-1;
for(int j=L[i];j<=R[i];j++){
sum[i]-=a[j];
a[j]=sqrt((double)a[j]);
sum[i]+=a[j];
mx[i]=max(mx[i],a[j]);
}
}
} void query(){
int res=0,x,y;read(x);read(y);//read(non);
if(x>y) swap(x,y);
if(bel[x]==bel[y]){
for(int i=x;i<=y;i++) res+=a[i];
printf("%lld\n",res);
return;
}
for(int i=x;i<=R[bel[x]];i++) res+=a[i];
for(int i=L[bel[y]];i<=y;i++) res+=a[i];
for(int i=bel[x]+1;i<bel[y];i++) res+=sum[i];
printf("%lld\n",res);
} void solve(){
blo=sqrt((double)n);cnt=n/blo;
cnt=cnt+((n%blo)!=0);
for(int i=1;i<=cnt;i++){
L[i]=(i-1)*blo+1,R[i]=i*blo;
}
R[cnt]=n;
for(int i=1;i<=n;i++){
bel[i]=(i-1)/blo+1;
sum[bel[i]]+=a[i];
mx[bel[i]]=max(mx[bel[i]],a[i]);
}
int T,op;
// T=n;
read(T);
while(T--){
read(op);
if(op==0) change();
else query();
}
puts("");
} signed main(){
while((~scanf("%lld",&n))&&n){
printf("Case #%d:\n",++cas);
reset();Init();solve();
}
return 0;
}

SP2713 GSS4 - Can you answer these queries IV 分块的更多相关文章

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

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

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

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

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

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

  5. 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)

    题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...

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

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

  7. GSS4 - Can you answer these queries IV(线段树懒操作)

    GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...

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

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

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

随机推荐

  1. Add the Scheduler Module 添加计划程序模块

    Important 重要 Scheduler requires the Event business class to be in your XAF application model. Follow ...

  2. 有了AOE,妈妈再也不用担心我的模型管理!

    前言 越来越多的业务会用到AI相关的技术,大多数的AI模型是部署在云端使用的,毕竟服务端计算更快,管理也更容易.随着终端设备性能提升,在终端使用 AI 模型有了更大的价值,可以更好满足业务对响应实时性 ...

  3. 4.Ansible Task控制

    1.tag标签(调试) --skip-tags install_nfs 跳过此标签 -t 指定标签名 [root@manager tasks]# cat task_nfs.yml - hosts: w ...

  4. 团队项目之测试与发布(Alpha版本)

    小组:BLACK PANDA 时间:2019.12.05 测试报告 1.测试找出的BUG 图片上传,文件过大会出错 用户可访问不具权限的URL 空字段导致异常 serializable反序列化时版本不 ...

  5. openstack在controller节点使用openstack network agent list不显示计算节点

    问题描述: 做完计算节点neutron的相关配置后,在controller节点查看agent列表,结果如下 发现并没有compute节点 在计算节点上查看status显示failed 在查看日志文件/ ...

  6. 如何在Mac上使用Siri

    在您的iPhone上,要求Siri执行命令很简单.但是,如果在计算机上工作时附近没有iPhone,会发生什么情况?您也可以在Mac上使用Siri.快速简便,使其成为iMac或MacBook的完美伴侣. ...

  7. RabbitMQ基础理解

    RabbitMQ基本理解 MQ是消息中间件,常见的有RabbitMQ,Kafka,RocketMQ,activeMQ 等,用于分布式系统中.作用有三点 解耦 异步 削峰 RabbitMQ 整体上是一个 ...

  8. 数组类的创建——DynamicArray.h

    完成DynamicArray类的具体实现 DynamicArray设计要点——类模板 动态确定内部数组空间的大小 实现函数返回数组长度 拷贝构造和赋值操作 DynamicArray类的声明 templ ...

  9. 深入浅出xpath轴定位

    在web自动化里面经常要用到定位,常用的八种定位方式中我最喜欢xpath定位,功能很强大.结合它里面的文本定位.模糊定位.逻辑定位等,基本能搞定所有的元素定位问题. 今天要讨论的是xpath的另一种比 ...

  10. js中call、apply、bind到底有什么区别?bind返回的方法还能修改this指向吗?

     壹 ❀ 引 同事最近在看angularjs源码,被源码中各种bind,apply弄的晕头转向:于是他问我,你知道apply,call与bind的区别吗?我说apply与call是函数应用,指定thi ...