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 询问区间[x,y]的每个数的和
格式: 有多组数据,数据以EOF结束,对于每组数据,输出数据的序号,每组数据之后输出一个空行。
注意: 不保证给出的区间[x, y]有\(x <= y\),如果\(x>y\)请交换\(x\),\(y\)。
感谢@Edgration 提供的翻译
输入输出格式
输入格式:
Multiple test cases, please proceed them one by one. Input terminates by EOF.
For each test case:
The first line contains an integer N. The following line contains N integers, representing the sequence A _{1}1..A _{N}N .
The third line contains an integer M. The next M lines contain the operations in the form "i x y".i=0 denotes the modify operation, i=1 denotes the query operation.
输出格式:
For each test case:
Output the case number (counting from 1) in the first line of output. Then for each query, print an integer as the problem required.
Print an blank line after each test case.
See the sample output for more details.
输入输出样例
输入样例#1:
5
1 2 3 4 5
5
1 2 4
0 2 4
1 2 4
0 4 5
1 1 5
4
10 10 10 10
3
1 1 4
0 2 3
1 1 4
输出样例#1:
Case #1:
9
4
6
Case #2:
40
26
思路:
第一眼,一点头绪都没有,因为涉及到区间修改和查询,像线段树,但又一副不可做的样子,因为如果区间修改不加任何优化的话,常数就会大的一批,应该会超时,那么,我们就来考虑关于开方的有关性质,首先\(sqrt(1)=1\),震惊!!没错,这就是修改终点,如果一个位置的数为1,再开方是没有意义的,那么我们就可以维护一个区间最大值,如果这个区间最大值是\(1\),也就是这个区间的数都是\(1\),那么就没必要进行修改了,区间求和的话就跟普通线段树没什么区别了。还有,为什么区间修改时终点是\(l==r\)呢?难道不是\(L<=l\)&&\(r<=R\)么?因为这里的区间修改是采用了一种暴力的思想,也就是一个一个改,相当于单点修改。
如果不知道怎么写的话,可以参考以下代码:
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cmath>
#define maxn 100007
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
int n,m,tim;
ll maxx[maxn<<2],sum[maxn<<2];
inline ll qread() {
char c=getchar();ll num=0,f=1;
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) num=num*10+c-'0';
return num*f;
}
inline void pushup(int rt) {
sum[rt]=sum[ls]+sum[rs];
maxx[rt]=max(maxx[ls],maxx[rs]);
}
void build(int rt, int l, int r) {
if(l==r) {
sum[rt]=maxx[rt]=qread();
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(rt);
}
void modify(int rt, int l, int r, int L, int R) {
if(l==r) {
sum[rt]=sqrt(sum[rt]);
maxx[rt]=sqrt(maxx[rt]);
return;
}
int mid=(l+r)>>1;
if(L<=mid&&maxx[ls]>1) modify(ls,l,mid,L,R);
if(R>mid&&maxx[rs]>1) modify(rs,mid+1,r,L,R);
pushup(rt);
}
ll csum(int rt, int l, int r, int L, int R) {
if(L>r||R<l) return 0;
if(L<=l&&r<=R) return sum[rt];
int mid=(l+r)>>1;
return csum(ls,l,mid,L,R)+csum(rs,mid+1,r,L,R);
}
int main() {
while(scanf("%d",&n)==1) {
printf("Case #%d:\n",++tim);
build(1,1,n);
m=qread();
for(int i=1,k,x,y;i<=m;++i) {
k=qread(),x=qread(),y=qread();
if(x>y) swap(x,y);
if(!k) modify(1,1,n,x,y);
else printf("%lld\n",csum(1,1,n,x,y));
}
}
return 0;
}
SP2713 GSS4 - Can you answer these queries IV的更多相关文章
- 线段树 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\)的标记.然后每次修改时先看是否有全 ...
- 【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 分块
问题描述 LG-SP2713 题解 分块,区间开根. 如果一块的最大值是 \(1\) ,那么这个块就不用开根了. 如果最大值不是 \(1\) ,直接暴力开就好了. \(\mathrm{Code}\) ...
- 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- 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
题目描述 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
传送门 Luogu 解题思路 区间开方以及区间求和. 考虑用线段树来做. 开方操作看似没有任何结合律可言,但这题有另外一个性质: 一个数的初始值不超过 \(10^{18}\) ,而这个数被开方6次左右 ...
随机推荐
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 85
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 85;import sys reload(sys) sys.s ...
- 【转载】eclipse如何传递main参数
转自:http://blog.csdn.net/theblackbeard/article/details/52172048 在命令行窗口可以通过java +程序名 +参数1(空格)参数2(空格).. ...
- 【275】◀▶ Python 控制语句说明
参考:Python循环语句 01 for 循环语句. 02 while 循环语句. 03 if...else 选择语句. 04 continue 执行循环语句中的下一条循环. 05 ...
- python之数据库的操作(课前准备)
数据库(Database)是按照数据结构来组织.存储和管理数据的仓库. 上面的就是数据库的定义. 何为数据库,简单的来说,就是我们的大型数据的存放地点. 而我们学习的呢就是数据库的访问层的制作. 何为 ...
- ORACLE体系结构一 (逻辑结构)-表空间、段、区和数据块
一.Oracle的逻辑结构 Oracle的逻辑结构是一种层次结构.主要由:表空间.段.区和数据块等概念组成.逻辑结构是面向用户的,用户使用Oracle开发应用程序使用的就是逻辑结构.数据库存储层次结构 ...
- php 中两种获得数据库中 数据条数的方法
一种是传统的利用mysql_num_rows()来计算 $sql="select * from news"; $res=mysql_query($sql); $number=mys ...
- 开发同事 Linux 实用基本操作
Linux 有复杂的体系,有很多的命令,开发同事日常开发时,不像运维同事需要熟练使用很多命令. 下面记录下我在工作中,常用的基本命令: 一 日志查看 对于开发同事来说,日常工作中,Linux 中最常用 ...
- ReentrantLock简单实现2
ReentrantLock: /** * ReentrantLock测试逻辑类 */ public class MyService { private Lock lock = new Reentran ...
- ROS Learning-006 beginner_Tutorials 编译ROS程序包
ROS Indigo beginner_Tutorials-05 编译 ROS 程序包 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04 ...
- 100725B Banal Tickets
传送门 题目大意 有2*n个位置,这些位置有的已经填上了数,有的还没有(用?表示),现在让你在还没有填上数的填0~9中的任意数,使得前n个数的乘积等于后n个数的乘积,问有多少种方案. 分析 首先这个题 ...