luogu P4145 上帝造题的七分钟2 / 花神游历各国 维护区间和&&区间开根号
因为开根号能使数字减小得非常快

所以开不了几次(6次?)很大的数就会变成1.....
所以我们可以维护区间最大值,若最大值>1,则继续递归子树,暴力修改叶节点,否则直接return
(好像也可以维护区间被开方的次数,但我不会。。。QAQ)
#include<cstdio>
#include<iostream>
#include<cmath>
#define int long long
#define R register int
#define ls (tr<<1)
#define rs (tr<<1|1)
using namespace std;
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int n,m;
int mx[],sum[];
inline void build(int tr,int l,int r){
if(l==r) {mx[tr]=sum[tr]=g(); return ;}
R md=(l+r)>>;
build(ls,l,md),build(rs,md+,r);
mx[tr]=max(mx[ls],mx[rs]);
sum[tr]=sum[ls]+sum[rs];
}
inline void calc(int tr,int l,int r,int ll,int rr) {
if(mx[tr]<=) return ;
if(l==r) {mx[tr]=sqrt(mx[tr]),sum[tr]=sqrt(sum[tr]); return ;}
R md=(l+r)>>;
if(ll>md) calc(rs,md+,r,ll,rr);
else if(rr<md+) calc(ls,l,md,ll,rr);
else calc(ls,l,md,ll,md),calc(rs,md+,r,md+,rr);
mx[tr]=max(mx[ls],mx[rs]);
sum[tr]=sum[ls]+sum[rs];
}
inline int query(int tr,int l,int r,int ll,int rr) {
if(l==ll&&r==rr) return sum[tr];
R md=(l+r)>>;
if(ll>md) return query(rs,md+,r,ll,rr);
else if(rr<md+) return query(ls,l,md,ll,rr);
else return query(ls,l,md,ll,md)+query(rs,md+,r,md+,rr);
}
signed main() {
n=g(); build(,,n); m=g();
for(R i=,k,l,r;i<=m;++i) {
k=g(),l=g(),r=g();
if(l>r) swap(l,r);
if(k&) printf("%lld\n",query(,,n,l,r));
else calc(,,n,l,r);
}
}
upd 2019.06.15
可以用树状数组做,如果这个数已经为$1$就用并查集合并到上一个位置,对于没有被开成$1$的直接暴力开根。。
跑的飞快
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ull unsigned long long
#define ll long long
#define R register ll
#define pause (for(R i=1;i<=10000000000;++i))
#define OUT freopen("out.out","w",stdout);
using namespace std;
namespace Fread {
static char B[<<],*S=B,*D=B;
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
inline ll g() {
R ret=,fix=; register char ch; whaile(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return ch<=||ch>=;}
inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));}
}using Fread::g; using Fread::gs;
const int N=; int n,m,fa[N];
ll c[N],h[N]; bool flg[N];
inline int lbt(int x) {return x&-x;}
inline void change(int pos) { R tmp=h[pos];
h[pos]=sqrt(h[pos]); R d=tmp-h[pos];
if(h[pos]==) flg[pos]=true;
for(;pos<=n;pos+=lbt(pos)) c[pos]-=d;
}
inline ll query(int pos) { R ret=;
for(;pos;pos-=lbt(pos)) ret+=c[pos]; return ret;
}
inline int getf(int x) {return (!flg[fa[x]])?fa[x]:fa[x]=getf(fa[x]);}
signed main() {
n=g(); for(R i=;i<=n;++i) h[i]=c[i]=g(); for(R i=;i<=n;++i) fa[i]=i-;
for(R i=;i<=n;++i) if(i+lbt(i)<=n) c[i+lbt(i)]+=c[i];
m=g(); for(R i=,x,l,r;i<=m;++i) { x=g(),l=g(),r=g(); l>r?swap(l,r):void();
if(x&) printf("%lld\n",query(r)-query(l-));
else for(flg[r]?r=getf(r):;r>=l;r=getf(r)) change(r);
}
}
2019.04.11&&2019.06.15
luogu P4145 上帝造题的七分钟2 / 花神游历各国 维护区间和&&区间开根号的更多相关文章
- 【题解】 Luogu P4145 上帝造题的七分钟2 / 花神游历各国
原题传送门 这道题实际和GSS4是一样的,只是输入方式有点区别 GSS4传送门 这道题暴力就能过qaq(这里暴力指线段树) 数据比较水 开方修改在线段树中枚举叶节点sqrt 查询区间和线段树基本操作 ...
- [Luogu P4145] 上帝造题的七分钟2 / 花神游历各国
题目链接 题目简要:我们需要一个能支持区间内每一个数开方以及区间求和的数据结构. 解题思路:说道区间修改区间查询,第一个想到的当然就是分块线段树.数据范围要用long long.本来我是看到区间这两个 ...
- P4145 上帝造题的七分钟2 / 花神游历各国(线段树区间开平方)
有点意思,不需要什么懒标记之类的东西,因为一个数无论怎样开平方,最后取整的结果必然会是1,所以我们不妨用最大值来维护,若区间最大值不为1,就暴力修改,否则不用管. #include<bits/s ...
- 洛谷P4145 上帝造题的七分钟2/花神游历各国 [树状数组,并查集]
题目传送门 题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是 ...
- 洛谷P4145——上帝造题的七分钟2 / 花神游历各国
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- 洛谷 P4145 上帝造题的七分钟2 / 花神游历各国
洛谷 这题就是区间开根号,区间求和.我们可以分块做. 我们记布尔数组vis[i]表示第i块中元素是否全部为1. 因为显然当一个块中元素全部为1时,并不需要对它进行根号操作. 我们每个块暴力开根号,因为 ...
- P4145 上帝造题的七分钟2 / 花神游历各国
思路 每个数不会被开方超过log次,对每个数暴力开方即可 代码 #include <algorithm> #include <cstring> #include <cst ...
- day1 晚上 P4145 上帝造题的七分钟2 / 花神游历各国 线段树
#include<iostream> #include<cstdio> #include<cmath> using namespace std; ; struct ...
随机推荐
- 创建blog APP
声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 什么是APP呢,Django里的APP其 ...
- 英语影视台词---无敌破坏王2大脑互联网(3)((Ralph)我们去喝根汁汽水吧)
英语影视台词---无敌破坏王2大脑互联网(3)((Ralph)我们去喝根汁汽水吧) 一.总结 一句话总结: Let's go get a root beer. 1.(Ralph)让我来瞧瞧你的本事 ...
- IDEAL葵花宝典:java代码开发规范插件 checkstyle、visualVM、PMD 插件
前言: visualVM: 运行java程序的时候启动visualvm,方便查看jvm的情况 比如堆内存大小的分配:某个对象占用了多大的内存,jvm调优必备工具. checkstyle: CheckS ...
- tensorflow 实现逻辑回归——原以为TensorFlow不擅长做线性回归或者逻辑回归,原来是这么简单哇!
实现的是预测 低 出生 体重 的 概率.尼克·麦克卢尔(Nick McClure). TensorFlow机器学习实战指南 (智能系统与技术丛书) (Kindle 位置 1060-1061). Kin ...
- ES搜索排序,文档相关度评分介绍——Vector Space Model
Vector Space Model The vector space model provides a way of comparing a multiterm query against a do ...
- MySQL性能优化/调优:默认配置的修改
在这里罗列一下这些配置, 每次新装MySQL的时候, 最好根据实际需要调整一下这些配置: max_connections 最大并发连接数.当MySQL的并发连接达到这个设定值时,新的连接将会被拒绝.当 ...
- listen 67
Pay What You Want May Deter Consumers Music, film and video game makers face a new online, digital w ...
- 集训Day10
果然颓的不像话 bzoj3680 gty又虐了一场比赛,被虐的蒟蒻们决定吊打gty.gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了.蒟蒻们将n个gty吊在n根绳子上,每根绳子穿过天 ...
- bzoj 4592(洛谷 4344) [Shoi2015]脑洞治疗仪——线段树上二分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4592 1操作就是用线段树来二分找到第一个有 k 个0的位置. 在洛谷上A了,与暴力和网上题解 ...
- Mysql常用命令行大全(二)
#登录数据库mysql -hlocalhost -uroot -p;#修改密码mysqladmin -uroot -pold password new; #显示数据库show databases;#显 ...