BZOJ 3038: 上帝造题的七分钟2 / BZOJ 3211: 花神游历各国 (线段树区间开平方)
题意
给出一些数,有两种操作。(1)将区间内每一个数开方(2)查询每一段区间的和
分析
普通的线段树保留修改+开方优化。可以知道当一个数为0或1时,无论开方几次,答案仍然相同。所以设置flag=1变表示这一段区间全是0/1,那么修改的时候直接暴力遍历线段树结点。因为一个数被开方下取整到1,只会被开方几次,所以说这样修改是O(n)O(n)O(n)的.总时间还是O(nlogn)O(nlogn)O(nlogn)
CODE1
上帝造题的七分钟2
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getc());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 100005;
int n, m;
struct seg {
LL sum; bool flg;
}t[MAXN<<2];
inline void upd(int i) {
t[i].sum = t[i<<1].sum + t[i<<1|1].sum;
t[i].flg = t[i<<1].flg & t[i<<1|1].flg;
}
void build(int i, int l, int r) {
t[i].flg = 0;
if(l == r) {
read(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
build(i<<1, l, mid);
build(i<<1|1, mid+1, r);
upd(i);
}
void modify(int i, int l, int r, int x, int y) {
if(t[i].flg) return;
if(l == r) {
t[i].sum = sqrt(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) modify(i<<1, l, mid, x, y);
if(y > mid) modify(i<<1|1, mid+1, r, x, y);
upd(i);
}
LL query(int i, int l, int r, int x, int y) {
if(x <= l && r <= y) return t[i].sum;
int mid = (l + r) >> 1;
if(y <= mid) return query(i<<1, l, mid, x, y);
else if(x > mid) return query(i<<1|1, mid+1, r, x, y);
else return query(i<<1, l, mid, x, y) + query(i<<1|1, mid+1, r, x, y);
}
int main () {
read(n); build(1, 1, n); read(m);
int x, y, op;
while(m--) {
read(op), read(x), read(y);
if(x > y) swap(x, y); //坑!
if(!op) modify(1, 1, n, x, y);
else printf("%lld\n", query(1, 1, n, x, y));
}
}
CODE 2
花神游历各国
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getc());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 100005;
int n, m;
struct seg {
LL sum; bool flg;
}t[MAXN<<2];
inline void upd(int i) {
t[i].sum = t[i<<1].sum + t[i<<1|1].sum;
t[i].flg = t[i<<1].flg & t[i<<1|1].flg;
}
void build(int i, int l, int r) {
t[i].flg = 0;
if(l == r) {
read(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
build(i<<1, l, mid);
build(i<<1|1, mid+1, r);
upd(i);
}
void modify(int i, int l, int r, int x, int y) {
if(t[i].flg) return;
if(l == r) {
t[i].sum = sqrt(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) modify(i<<1, l, mid, x, y);
if(y > mid) modify(i<<1|1, mid+1, r, x, y);
upd(i);
}
LL query(int i, int l, int r, int x, int y) {
if(x <= l && r <= y) return t[i].sum;
int mid = (l + r) >> 1;
if(y <= mid) return query(i<<1, l, mid, x, y);
else if(x > mid) return query(i<<1|1, mid+1, r, x, y);
else return query(i<<1, l, mid, x, y) + query(i<<1|1, mid+1, r, x, y);
}
int main () {
read(n); build(1, 1, n); read(m);
int x, y, op;
while(m--) {
read(op), read(x), read(y);
if(op == 2) modify(1, 1, n, x, y);
else printf("%lld\n", query(1, 1, n, x, y));
}
}
BZOJ 3038: 上帝造题的七分钟2 / BZOJ 3211: 花神游历各国 (线段树区间开平方)的更多相关文章
- BZOJ 3038: 上帝造题的七分钟2
3038: 上帝造题的七分钟2 Description XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分 ...
- BZOJ 3038: 上帝造题的七分钟2【线段树区间开方问题】
3038: 上帝造题的七分钟2 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1469 Solved: 631[Submit][Status][Dis ...
- bzoj 3038: 上帝造题的七分钟2 线段树||hdu 4027
3038: 上帝造题的七分钟2 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1066 Solved: 476[Submit][Status][Dis ...
- BZOJ 3038 上帝造题的七分钟2 (并查集+树状数组)
题解:同 BZOJ 3211 花神游历各国,需要注意的是需要开long long,还有左右节点需要注意一下. #include <cstdio> #include <cmath> ...
- BZOJ 3038 上帝造题的七分钟2 树状数组+并查集
题目大意:一个序列,有两种操作.1.将一段数中的每个数开根号.2.查询一段数的和. 思路:和3211是一个题,有兴趣的能够看看我的那篇博客. CODE: #include <cmath> ...
- BZOJ 3038 上帝造题的七分钟二
无题目 但是百度会发现题目和3211基本一致 所以看上一篇博文的上一篇博文呢
- BZOJ 3211: 花神游历各国( 线段树 )
线段树...区间开方...明显是要处理到叶节点的 之前在CF做过道区间取模...差不多, 只有开方, 那么每个数开方次数也是有限的(0,1时就会停止), 最大的数10^9开方10+次也就不会动了.那么 ...
- BZOJ 3211 花神游历各国 线段树平方开根
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3211 题目大意: 思路: 由于数据范围只有1e9,一个数字x开根号次数超过logx之后 ...
- 【BZOJ】3038: 上帝造题的七分钟2(线段树+暴力)
http://www.lydsy.com:808/JudgeOnline/problem.php?id=3038 这题我就有得吐槽了,先是线段树更新写错,然后不知哪没pushup导致te,精度问题sq ...
随机推荐
- [NOI2019]序列
LOJ3158 , Luogu5470 从 \(a_1\dots a_n\) , \(b_1\dots b_n\) 中各选出 \(K\) 个数 , 且至少 \(L\) 组下标在两个数组中都被选择 , ...
- 配置了Ubuntu环境变量,系统启动不了
修改了etc/init.d/rcS文件后重启后Ubuntu起不来了, 开启按shift+e或者直接选择,进入恢复模式 进入root shell 执行这个命令 可以有写入权限,重新挂载 mount - ...
- [转]mac升级Nodejs和Npm到最新版
第一步,先查看本机node.js版本: node -v 第二步,清除node.js的cache: sudo npm cache clean -f 第三步,安装 n 工具,这个工具是专门用来管理node ...
- Django2.2连接mysql数据库出现django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None问题
在使用Django2.2开发的时候,想要使用mysql数据库,在settings.py文件中更改命令: DATABASES = { 'default': { 'ENGINE': 'django.db. ...
- IDEA插件之alibaba编程规范
1.做什么 这是阿里巴巴的编码规范插件,规范内容可以查阅 https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(华山版).pdf 2.File ...
- 后端排序,debug模式中map的顺序出错
js中map遍历的顺序是按照插入的顺序来执行的.如果map的来源是字符串转换的,那么就会按照字符串中key值的顺序进行遍历.千万不要被debug中显示的顺序误导,这里应该是为了方便查看对key进行了字 ...
- RBAC授权
RBAC RBAC使用rbac.authorization.k8s.io API Group 来实现授权决策,允许管理员通过 Kubernetes API 动态配置策略,要启用RBAC,需要在 api ...
- 使用lodop.js打印控件打印table并分页等
import {getLodop} from '@/utils/LodopFuncs.js' //打印表格 export default{ // num 打印还是打印预览 conData 对象形式 传 ...
- python使用openpyxl操作execl
openpyxl openpyxl可以用来对excel进行操作,但只能操作xlsx文件而不能操作xls文件. 主要用到三个概念:Workbooks,Sheets,Cells.Workbook就是一个e ...
- 解释mysql 语句
一.在我们创建mysql数据库的时候我们经常会用到这句SQL: CREATE DATABASE TEST DEFAULT CHARACTER SET utf8 COLLATE utf8_general ...