【BZOJ 3211&3038】 花神游历各国 & 上帝造题的七分钟2
【题目链接】
【BZOJ 3211】 点击打开链接
【BZOJ 3038】 点击打开链接
【算法】
线段树
开根操作直接开到叶子节点,注意当区间中所有数都是0或1时,不需要开根
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 int i,n,m,opt,l,r;
long long a[MAXN]; struct SegmentTree
{
struct Node
{
int l,r;
long long sum;
bool flag;
} Tree[MAXN<<];
inline void update(int index)
{
Tree[index].sum = Tree[index<<].sum + Tree[index<<|].sum;
Tree[index].flag = Tree[index<<].flag & Tree[index<<|].flag;
}
inline void build(int index,int l,int r)
{
int mid;
Tree[index].l = l; Tree[index].r = r;
if (l == r)
{
Tree[index].sum = a[l];
if (a[l] == || a[l] == ) Tree[index].flag = true;
else Tree[index].flag = false;
return;
}
mid = (l + r) >> ;
build(index<<,l,mid);
build(index<<|,mid+,r);
update(index);
}
inline void modify(int index,int l,int r)
{
int mid;
if (Tree[index].flag) return;
if (Tree[index].l == Tree[index].r)
{
Tree[index].sum = (long long)sqrt(Tree[index].sum);
if (Tree[index].sum == || Tree[index].sum == ) Tree[index].flag = true;
return;
}
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index<<,l,r);
else if (mid + <= l) modify(index<<|,l,r);
else
{
modify(index<<,l,mid);
modify(index<<|,mid+,r);
}
update(index);
}
inline long long query(int index,int l,int r)
{
int mid;
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].sum;
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query(index<<,l,r);
else if (mid + <= l) return query(index<<|,l,r);
else return query(index<<,l,mid) + query(index<<|,mid+,r);
}
} T;
template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < )
{
putchar('-');
x = -x;
}
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
} int main() { read(n);
for (i = ; i <= n; i++) read(a[i]);
T.build(,,n);
read(m);
while (m--)
{
read(opt);
if (opt == )
{
read(l); read(r);
writeln(T.query(,l,r));
} else
{
read(l); read(r);
T.modify(,l,r);
}
}
return ; }
【BZOJ 3211&3038】 花神游历各国 & 上帝造题的七分钟2的更多相关文章
- 【BZOJ3211&3038】花神游历各国&上帝造题的七分钟2(CodeVS)
Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 ...
- BZOJ 3038: 上帝造题的七分钟2 / BZOJ 3211: 花神游历各国 (线段树区间开平方)
题意 给出一些数,有两种操作.(1)将区间内每一个数开方(2)查询每一段区间的和 分析 普通的线段树保留修改+开方优化.可以知道当一个数为0或1时,无论开方几次,答案仍然相同.所以设置flag=1变表 ...
- [bzoj3038/3211]上帝造题的七分钟2/花神游历各国_线段树
上帝造题的七分钟2 bzoj-3038 题目大意:给定一个序列,支持:区间开方:查询区间和. 注释:$1\le n\le 10^5$,$1\le val[i] \le 10^{12}$. 想法:这题还 ...
- bzoj3211花神游历各国&&bzoj3038上帝造题的七分钟2*
bzoj3211花神游历各国 题意: n个数的序列,m个操作,操作两种:区间开根(向下取整)和区间求和.n≤100000,m≤200000,序列中的数非负且≤109. 题解: 一个≤109的数开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 ...
- 题解 洛谷 P4145 【上帝造题的七分钟2 / 花神游历各国】
题目 上帝造题的七分钟2 / 花神游历各国 题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. ...
- 洛谷P4145 上帝造题的七分钟2/花神游历各国 [树状数组,并查集]
题目传送门 题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是 ...
- 【bzoj3211】花神游历各国&&【bzoj3038】上帝造题的七分钟2
bzoj3038]上帝造题的七分钟2 Description XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. “第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟, ...
- 洛谷P4145——上帝造题的七分钟2 / 花神游历各国
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
随机推荐
- [Python3网络爬虫开发实战] 7.2-Splash的使用
Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库.利用它,我们同样可以实现动态渲染页面的抓取. 1. 功能介 ...
- 源码学习-Object类
1.Object类是Java所有类的超类 2.查看Object的属性和方法,发现Object类没有属性,只有13个方法,其中7个本地方法. 3.接下来看具体的方法 3.1 Object() 默认的构造 ...
- 2. Java中的垃圾收集 - GC参考手册
标记-清除(Mark and Sweep)是最经典的垃圾收集算法.将理论用于生产实践时, 会有很多需要优化调整的地点, 以适应具体环境.下面通过一个简单的例子, 让我们一步步记录下来, 看看如何才能保 ...
- history.go history.back()
转http://www.mikebai.com/Article/2009-11/757.html <input type=button value=刷新 onclick="window ...
- FIRST集合、FOLLOW集合、SELECT集合以及预测分析表地构造
FIRST集合.FOLLOW集合.SELECT集合以及预测分析表地构造 FIRST集合的简单理解就是推导出的字符串的开头终结符的集合. FOLLOW集合简单的理解就对于非终结符后面接的第一个终结符. ...
- 2018/2/17 SpringCloud的一个简单小介绍
在学习SpringCloud之前,我以为SpringCloud是与Double一样,只是个单纯的RPC框架.但在今天的学习中,我发现并非如此,事实上,SpringCloud是多个框架的集合,感觉Spr ...
- JavaWeb图片显示与存储
在数据库中存储文件的名称,在存储信息资料里面存下照片,利用文件名称. 代码如下: 其中iamgeFile为 图片存储的路径userImages/ Resultuser.setImageName(Pro ...
- Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
Spring Boot 官网在 2019/03/15 这天发布了 Spring Boot 2.1.5 正式版,栈长表示真跟不上了.. 官宣如下 : https://spring.io/blog/201 ...
- MySQL集群方案收集
MySQL集群是一个需要时间才能磨得出的话题,不可能一下子就全部能掌握.由于整个方案结合LVS+Keepalived这种,更加的复杂. 下面是一些主流方案的收集: MySQL双主 + Keepaliv ...
- JDBC的数据类型
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/data-types.html: JDBC驱动程序在将Java数据类型发送到数据库之前,会将其转换为相应 ...