[传送门](SP2713 GSS4 - Can you answer these queries IV)

解题思路

  大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记。然后每次修改时先看是否有全\(1\)或\(0\)的标记,有就不用理了,没有就暴力开方。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define int long long using namespace std;
const int MAXN = 100005; inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
} int n,m,a[MAXN],sum[MAXN<<2],tag[MAXN<<2],lazy[MAXN<<2],cnt; void build(int x,int l,int r){
if(l==r) {
a[l]=rd();sum[x]=a[l];lazy[x]=tag[x]=0;
if(sum[x]==1 || sum[x]==0) lazy[x]=1;
return ;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);build(x<<1|1,mid+1,r);
sum[x]=sum[x<<1]+sum[x<<1|1];
lazy[x]=(lazy[x<<1]&lazy[x<<1|1]);tag[x]=0;
} inline void pushdown(int x,int l,int r){
int mid=(l+r)>>1;
if(!lazy[x<<1]){
tag[x<<1]+=tag[x];lazy[x<<1]=1;sum[x<<1]=0;
for(int i=l;i<=mid;i++){
if(a[i]>1) lazy[x<<1]=0;sum[x<<1]+=a[i];
}
}
if(!lazy[x<<1|1]){
tag[x<<1|1]+=tag[x];lazy[x<<1|1]=0;sum[x<<1|1]=0;
for(int i=mid+1;i<=r;i++){
if(a[i]>1) lazy[x<<1|1]=0;sum[x<<1|1]+=a[i];
}
}
tag[x]=0;
} void update(int x,int l,int r,int L,int R){
if(lazy[x]) return;
if(L<=l && r<=R){
tag[x]++;lazy[x]=1;sum[x]=0;
for(int i=l;i<=r;i++) {
if(a[i]>1) a[i]=sqrt(a[i]);
if(a[i]>1) lazy[x]=0;
sum[x]+=a[i];
}
return ;
}
int mid=(l+r)>>1;if(tag[x]) pushdown(x,l,r);
if(L<=mid) update(x<<1,l,mid,L,R);
if(mid<R) update(x<<1|1,mid+1,r,L,R);
sum[x]=sum[x<<1]+sum[x<<1|1];
lazy[x]|=(lazy[x<<1]&lazy[x<<1|1]);
} int query(int x,int l,int r,int L,int R){
if(L<=l && r<=R) return sum[x];
int ret=0,mid=(l+r)>>1;
if(tag[x]) pushdown(x,l,r);
if(L<=mid) ret+=query(x<<1,l,mid,L,R);
if(mid<R) ret+=query(x<<1|1,mid+1,r,L,R);
return ret;
} signed main(){
while(~scanf("%lld",&n)){cnt++;
printf("Case #%lld:\n",cnt);
build(1,1,n);m=rd();int op,x,y;
while(m--){
op=rd(),x=rd(),y=rd();if(x>y) swap(x,y);
if(!op) update(1,1,n,x,y);
else printf("%lld\n",query(1,1,n,x,y));
}
putchar('\n');
}
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. GSS4 2713. Can you answer these queries IV 线段树

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

  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. SP2713 GSS4 - Can you answer these queries IV 分块

    问题描述 LG-SP2713 题解 分块,区间开根. 如果一块的最大值是 \(1\) ,那么这个块就不用开根了. 如果最大值不是 \(1\) ,直接暴力开就好了. \(\mathrm{Code}\) ...

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

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

  7. SPOJ2713GSS4 - Can you answer these queries IV(线段树)

    题意 Sol 讲过无数次了..很显然,一个$10^12$的数开方不超过$8$次后就会变为$1$ 因此直接暴力更改即可,维护一下这段区间是否被全改为了$1$ 双倍经验:https://www.luogu ...

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

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

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

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

随机推荐

  1. git命令的基本使用

    git init 创建仓库 git status  查看当前版本库的状态 git add filename    使用git add命令告诉git,把该文件添加到仓库 git commit -m 'c ...

  2. iOS 7 认识 TextKit

    本文由 伯乐在线 - 和谐老约翰 翻译自 Max Seelemann.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. iOS7 的发布给开发者的案头带来了很多新工具.其中一个就是 TextKit( ...

  3. idea 中使用dataBase插件

    最近一段时间重新开始学习Java 使用IntelliJ 发现有个链接数据库插件挺好用的,并且反应速度还挺快的记录下. 点击步骤:View → Tool Windows → data 进行数据库链接 发 ...

  4. (转)OpenFire源码学习之六:用户注册

    转:http://blog.csdn.net/huwenfeng_2011/article/details/43413509 用户注册 注册流程: 1.客户端进行握手给服务端发送连接消息: <s ...

  5. 如何在一个for语句中迭代多个对象(2.7)

    如何在一个for语句中迭代多个对象 总结: 并行迭代使用zip(l1, l2, l3) 每次迭代从3个列表里各取一个数据 串行迭代使用itertools.chain(l1, l2, l3) 相当于把3 ...

  6. JBoss、Tomcat、JBoss EAP、JBoss AS、wildfly,JBoss EAP安装部署,JBoss各个版本下载,JBoss允许远程访问

    感谢: https://www.cnblogs.com/invlong/p/5983334.html https://blog.csdn.net/mooncarp/article/details/78 ...

  7. C++——代码风格

    google代码风格 1.使用安全的分配器(allocator),如scoped_ptr,scoped_array 2.测试用的,其他的不能用: 2.1 友元 2.2 C++异常 2.3 RTTI 3 ...

  8. PAT_A1088#Rational Arithmetic

    Source: PAT A1088 Rational Arithmetic (20 分) Description: For two rational numbers, your task is to ...

  9. 什么是NVMe硬盘?

    本文摘自:https://zhidao.baidu.com/question/590890784.html NVMe硬盘指的是非易失性内存主机控制器接口规范(Non-Volatile Memory e ...

  10. java读取pdf文本转换html

    补充:一下代码基于maven,现将依赖的jar包单独导出 地址:pdf jar 完整代码地址 也就两个文件 java读取pdf中的纯文字,这里使用的是pdfbox工具包 maven引入如下配置 < ...