问题描述

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. Java实现抢红包功能

    采用多线程模拟多人同时抢红包.服务端将玩家发出的红包保存在一个队列里,然后用Job定时将红包信息推送给玩家.每一批玩家的抢红包请求,其实操作的都是从队列中弹出的第一个红包元素,但当前的红包数量为空的时 ...

  2. bayaim——达梦数据库安装DM7-单实例-Centos7.txt

    bay——安装_达梦DM7-单实例-Centos7.txt IP:10.20.100.22 ---------------------------------------------1.下载 注意:要 ...

  3. Self Service Password 密码策略

    1.在活动目录中新建一个用户,并赋予域管理员权限:2.拷贝conf目录下的config.inc.php为config.inc.local.php:3.按自己的实际情况及要求修改config.inc.l ...

  4. SSM案例整合踩的一些坑

    一.出现错误:Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for p ...

  5. 关于操作服务器上tomcat的常用linux指令

    ll:     列出目录下的所有文件 cd:切换目录 pwd:显示目前的目录 grep  xxxxxx catalina.out    查询文件中关于某个信息的内容 grep -3  xxxxxxca ...

  6. SQl Server 中的decimal( m , n )的意思

    create table sc( cno ), sno ), grade ,), primary key(cno,sno), foreign key(cno) references cou(cno), ...

  7. 了解angularjs中的生命周期钩子函数$onInit,$onChange,$onDestory,$postLink

     壹 ❀ 引 我在前面花了三篇文章用于介绍angularjs的指令directive,组件component,并专门花了一篇文章介绍directive与component的不同,其中提到在compon ...

  8. sshd服务安装-ssh命令使用方法

    SSHD服务概述 介绍:SSH协议:安全外壳协议.为 Secure Shell的缩写.SSH 为建立在应用层和传输层基础上的安全协议. 作用:SSHD服务使用SSH协议可以用来进行远程控制,或在计算机 ...

  9. Hyperledger Fabric相关文件解析

    1相关文件说明 这一部分涉及相关配置文件的解析, 网络的启动涉及到多个文件,本文按以下顺序进行分析: . ├── base │   ├── docker-compose-base.yaml #1 │  ...

  10. node http 模块 常用知识点记录

    关于 node,总是断断续续的学一点,也只能在本地自己模拟实战,相信总会有实战的一天~~ http 作为服务端,开启服务,处理路由,响应等 http 作为客户端,发送请求 http.https.htt ...