「SP2713」GSS4 - Can you answer these queries IV
传送门
Luogu
解题思路
区间开方以及区间求和。
考虑用线段树来做。
开方操作看似没有任何结合律可言,但这题有另外一个性质:
一个数的初始值不超过 \(10^{18}\) ,而这个数被开方6次左右就可以到1或0,并且1和0都是不需要再开方的。
所以我们记一下每个节点代表区间的最大值,若该值小于等于1,那么就不需要再进入下一层递归,否则就向下递归修改,修改次数最坏也不过是 \(O(6n)\) 左右,线段树完全没压力,于是这题就做完了。
细节注意事项
- 咕咕咕
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
typedef long long LL;
const int _ = 100010;
int n; LL a[_], sum[_ << 2], mx[_ << 2];
inline int lc(int rt) { return rt << 1; }
inline int rc(int rt) { return rt << 1 | 1; }
inline void pushup(int rt) {
sum[rt] = sum[lc(rt)] + sum[rc(rt)];
mx[rt] = max(mx[lc(rt)], mx[rc(rt)]);
}
inline void build(int rt = 1, int l = 1, int r = n) {
if (l == r) { mx[rt] = sum[rt] = a[l]; return; }
int mid = (l + r) >> 1;
build(lc(rt), l, mid), build(rc(rt), mid + 1, r), pushup(rt);
}
inline void update(int ql, int qr, int rt = 1, int l = 1, int r = n) {
if (mx[rt] <= 1) return;
if (l == r) { mx[rt] = sum[rt] = sqrt(sum[rt]); return; }
int mid = (l + r) >> 1;
if (ql <= mid) update(ql, qr, lc(rt), l, mid);
if (qr > mid) update(ql, qr, rc(rt), mid + 1, r);
pushup(rt);
}
inline LL query(int ql, int qr, int rt = 1, int l = 1, int r = n) {
if (ql <= l && r <= qr) return sum[rt];
int mid = (l + r) >> 1; LL res = 0;
if (ql <= mid) res += query(ql, qr, lc(rt), l, mid);
if (qr > mid) res += query(ql, qr, rc(rt), mid + 1, r);
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
int Case = 0;
while (scanf("%d", &n) != EOF) {
printf("Case #%d:\n", ++Case);
for (rg int i = 1; i <= n; ++i) read(a[i]);
build();
int q; read(q);
for (rg int f, ql, qr; q--; ) {
read(f), read(ql), read(qr);
if (ql > qr) swap(ql, qr);
if (!f) update(ql, qr);
else printf("%lld\n", query(ql, qr));
}
puts("");
}
return 0;
}
完结撒花 \(qwq\)
「SP2713」GSS4 - Can you answer these queries IV的更多相关文章
- 题解【SP2713】GSS4 - Can you answer these queries IV
题目描述 You are given a sequence \(A\) of \(N(N \leq 100,000)\) positive integers. There sum will be le ...
- 题解 SP2713 【GSS4 - Can you answer these queries IV】
用计算器算一算,就可以发现\(10^{18}\)的数,被开方\(6\)次后就变为了\(1\). 所以我们可以直接暴力的进行区间修改,若这个数已经到达\(1\),则以后就不再修改(因为\(1\)开方后还 ...
- 线段树 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 ...
- SP2713 GSS4 - Can you answer these queries IV(线段树)
传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...
- 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 ...
- 【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}\) ...
随机推荐
- java 8 list的stream操作 list中的对象中的某一个成员取出转为该成员的list,以及对象过滤,筛选某个属性后的成员
取成员属性list List<String> configList = codeEntityList.stream().map(t -> t.getName()).distinct( ...
- netty代理http&https请求
(1)关键代码 package test; import java.security.cert.CertificateException; import javax.net.ssl.SSLExcept ...
- mongodb的remove操作
今天学习mongodb时,打算用db.user.remove()函数把user中的数据都删了,结果没闪成功,提示:remove needs a query.上网查了一下,是因为没有给remove函数传 ...
- python去除字符串中的特殊字符(爬虫存储数据时会遇到不能作为文件名的字符串)
问题描述 今天在写爬虫爬取影评时,本来的思路把影评的标题作为文件名,将每个影评的详情内容写入到"标题.txt"文件中,直到我遇到了这个问题: 这时我突然意识到,文件名中有些字符是不 ...
- 工具 - VS Code
杂项 1. 主题 brackets light pro, One Monokai theme 2. directory tree indent guidelines, directory vertic ...
- ubuntu 18.04 上安装 docker
命令安装 docker 1.直接从 ubuntu 仓库安装,打开终端,输入: 2.启动 docker 服务 . 设置开机自启动 docker 服务 3.免 sudo 配置:
- 【转】Python中*args和**kwargs的区别
一.*args的使用方法 *args 用来将参数打包成tuple给函数体调用 例子一: 输出结果以元组的形式展示 def function(*args): print(args, type(args) ...
- Selenium+webdriver自动化登陆QQ邮箱并发送邮件
1.关于selenium Selenium的主要功能包括:(1)测试与浏览器的兼容性:测试应用程序能否兼容工作在不同浏览器和操作系统之上.(2)测试系统功能:录制用例自动生成测试脚本,用于回归功能测 ...
- 02.Scala高级特性:第6节 高阶函数;第7节 隐式转换和隐式参数
Scala高级特性 1. 课程目标 1.1. 目标一:深入理解高阶函数 1.2. 目标二:深入理解隐式转换 2. 高阶函数 2.1. 概念 Scala混合了面向对象和函数式的特 ...
- 四、linux基础-系统目录_安装_相关命令_调度
4 系统目录-安装-版本4.1系统目录Linux的文件系统是采用级层式的树状目录结构,在此结构中的最上层是根目录“/”,然后在此目录下再创建其他的目录. 在装完Linux系统以后会自动生成一下等目录, ...