[传送门](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. 使用appium1.4在android8.0真机上测试程序时报错command failed shell "ps 'uiautomator'"的解决方式

    appium1.4,运行自动化脚本时提示 org.openqa.selenium.SessionNotCreatedException: A new session could not be crea ...

  2. cdq分治(陈丹琦分治)

    //转自:http://blog.csdn.net/snowy_smile/article/details/49668689 /* 算法介绍之cdq分治: 其实cdq分治的思想与应用都能被很简单地描述 ...

  3. CSV (逗号分隔值文件格式)

    逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本).纯文本意味着该文件是一个字符序列,不 ...

  4. 树————N叉树的层序遍历

    思想: 使用队的思想,将每一层的节点放入队列中,依次弹出,同时将其children放入队列. c++ /* // Definition for a Node. class Node { public: ...

  5. Sophus安装时Eigen库更新版本的问题

    原文链接:https://blog.csdn.net/m0_38055352/article/details/102900516

  6. HDU - 1712 (分组背包模板)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意:给你n个课程,每个课程有很多种学习方法,用的时间和取得的效果都不一样,现在你只有m天时间用来学 ...

  7. docker安装(4)

    centos6 docker安装 wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo yum install -y ...

  8. [HNOI2011]卡农 题解

    题目描述 众所周知卡农是一种复调音乐的写作技法,小余在听卡农音乐时灵感大发,发明了一种新的音乐谱写规则.他将声音分成 n 个音阶,并将音乐分成若干个片段.音乐的每个片段都是由 1 到 n 个音阶构成的 ...

  9. robotframework + selenium2library 一点测试的经验

    1 对于元素的外层包括frame/iframe标签的.一定要先select  frame name=xxx,然后再操作元素. Select frame name=新建个案 click element ...

  10. 4.2 react patterns(转)

    修改 Props Immutable data representation 确定性 在 getInitialState 中使用 props 私有状态和全局事件 render 包含 side effe ...