SP2713 GSS4 - Can you answer these queries IV(线段树)
[传送门](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(线段树)的更多相关文章
- 线段树 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 ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- 【SP2713 GSS4 - Can you answer these queries IV】 题解
题目链接:https://www.luogu.org/problemnew/show/SP2713 真暴力啊. 开方你开就是了,开上6次就都没了. #include <cmath> #in ...
- 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 ...
- SP2713 GSS4 - Can you answer these queries IV 分块
问题描述 LG-SP2713 题解 分块,区间开根. 如果一块的最大值是 \(1\) ,那么这个块就不用开根了. 如果最大值不是 \(1\) ,直接暴力开就好了. \(\mathrm{Code}\) ...
- 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- SPOJ2713GSS4 - Can you answer these queries IV(线段树)
题意 Sol 讲过无数次了..很显然,一个$10^12$的数开方不超过$8$次后就会变为$1$ 因此直接暴力更改即可,维护一下这段区间是否被全改为了$1$ 双倍经验:https://www.luogu ...
- GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)
GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...
- GSS4 - Can you answer these queries IV(线段树懒操作)
GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...
随机推荐
- nodejs模块——fs模块 WriteFile写入文件
WriteFile写入文件 使用fs.writeFile(filename,data,[options],callback)写入内容到文件. 参数说明: filename String 文件名 dat ...
- Delphi 窗体函数 ShowScrollBar 控制滚动条
API函数 函数来源:FUNCTION ulong ShowScrollBar(ulong hwnd,ulong wBar,ulong bShow) LIBRARY "user32.dll& ...
- Vue学习笔记【2】——Vue指令之 - v-cloak、v-text和v-html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java类的成员之四:代码块.
3.2类的成员之四:代码块 ①初始化块(代码块)作用:对Java对象进行初始化 ②程序的执行顺序: ③一个类中初始化块若有修饰符,则只能被static修饰,称为静态代码块(static block ) ...
- BZOJ 3328: PYXFIB 解题报告
BZOJ 3328: PYXFIB 题意 给定\(n,p,k(1\le n\le 10^{18},1\le k\le 20000,1\le p\le 10^9,p \ is \ prime,k|(p- ...
- bzoj1050题解
[解题思路] 先把边按边权排序,然后O(m)暴力枚举最小边,对于每条最小边,将比其大的边按序加入直到起终点连通,此时最大边权/最小边权即为选择该最小边情况下的最小比值.复杂度O(m(m+n)α(n)) ...
- siege之-服务端性能测试
官方网站http://www.joedog.org/ 有3种操作模式: 1) Regression (when invoked by bombardment)Siege从配置文件中读取URLs,按递归 ...
- Cell的复用机制问题总结
创建方式汇总,注册和不注册Cell注册的两种方式 1.tableView registerNib:(nullable UINib *) forCellReuseIdentifier:(nonnull ...
- JAVA中HashMap相关知识的总结(一)
Java中HashMap在jdk1.7和jdk1.8中的区别点: 在jdk1.7中是用数组+链表形式存储,1.8采用数组+链表/红黑树形式 Jdk1.8中由链表转为红黑树是长度大于8,由红黑树转为链表 ...
- form提交跳转问题
$.ajax({ type: "POST", url: url, data: $('.form-horizontal').serialize(), 提交form表单 success ...