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 ...
随机推荐
- Ubuntu 17.4下如何安装和配置flash player
Ubuntu Linux系统下没有自带的flash player,要自己手动安装.下面post出简单的安装过程. 首先打开终端,输入命令:sudo apt-get install flashplugi ...
- gVim/Vim 一键编译、连接、运行 C/C++ 单文件
用于Gvim 或 Vim 配置文件的一键编译与运行函数(注:需要机器上安装了GCC才行) 本代码只加入了对C/C++的编译与运行,如果要加入其语言的可以参考此代码加入即可 同时,本代码加入了对Wind ...
- linux应用之nginx的源码安装及配置(centos)
1.准备工作选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL.Nginx是C写的,需要用GCC编译:Nginx的 ...
- 时尚与深度学习系列:Fashion forward: Forecasting visual style in fashion
https://arxiv.org/pdf/1705.06394.pdf 将深度学习与时尚预测联系在一起,是一个很有趣但是估计结果会没什么成效的话题.因为,时尚预测这一领 ...
- python下setuptools安装
python下的setuptools带有一个easy_install的工具,在安装python的每三方模块.工具时很有用,也很方便.安装setuptools前先安装pip,请参见<pytho ...
- Btree算法的C语言实现
btree.h //实现对order序(阶)的B-TREE结构基本操作的封装. //查找:search,插入:insert,删除:remove. //创建:create,销毁:destory,打印:p ...
- puppet初始化安装和配置(puppet自动化系列1)
一.服务器规划 以下均直接yum安装最新版. 服务器操作系统为centos6.2 Puppetmaster1 10.168.32.116 puppstmaster1.jq.com Puppetmast ...
- 点阵字体显示系列之一:ASCII码字库的显示
http://blog.csdn.net/subfate/article/details/6444578 起因: 早在阅读tslib源代码时就注意到里面有font_8x8.c和font_8x16.c两 ...
- POJ-3262
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7923 Accepted: ...
- HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)
Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...