问题描述

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. C lang:Array and Pointer formal parameter

    Test Xx_Formal parameter Formal parameter are local variable. It's private to the function. Ax_Array ...

  2. 如何使用python远程操作linux

    在云服务测试中,往往需要我们进入云服务内容进行相关内容的测试.这测试可以使用平台自身的noVNC.外部辅助xshell等工具连接到云服务内部进行测试.但是在如此反复的测试操作中,就需要用到自动化测试方 ...

  3. CSS3 更改字体被选中样式

    CSS3  更改字体被选中样式

  4. Mysql模式匹配两种方法

    一.使用LIKE或NOT LIKE比较操作符 使用 "_" 匹配任何单个字符,而 "%" 匹配任意数量的字符(包括零字符): 例如: 1.要想找出以“b”开头的 ...

  5. 5 其他命令-学习目标以及find命令的基本使用

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  6. MASMPlus连接出错:error LNK2001: unresolved external symbol _WinMainCRTStartup

    坑:汇编语言第三版使用的是masm5.0,网上找到了一个masm32,一看名字,不就是masm的32位版本吗.然也..这是另外一个软件 MASM32并非是指Microsoft的MASM宏汇编器.MAS ...

  7. 剑指Offer-44.翻转单词顺序列(C++/Java)

    题目: 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“student. ...

  8. [译]Vulkan教程(23)暂存buffer

    [译]Vulkan教程(23)暂存buffer Staging buffer 暂存buffer Introduction 入门 The vertex buffer we have right now ...

  9. Yii2 中常用的增删改查操作总结

    一.新增 1.使用save() $model = new User(); $model->name = 'test'; $model->phone = '13000000000'; $mo ...

  10. Linux使用之centos下安装Java环境并运行Java程序

    前言 在Java中所有的程序都是在JVM上运行的.Java虚拟机(JVM)读取并处理经过编译的与平台无关的*.class文件.因为Java语言源程序编写后,先使用Java伪编译器进行伪编译,将其转换为 ...