SPOJ GSS4 Can you answer these queries IV
| Time Limit: 500MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
Description
You are given a sequence A of N(N <= 100,000) positive integers. There sum will be less than 1018. On this sequence you have to apply M (M <= 100,000) operations:
(A) For given x,y, for each elements between the x-th and the y-th ones (inclusively, counting from 1), modify it to its positive square root (rounded down to the nearest integer).
(B) For given x,y, query the sum of all the elements between the x-th and the y-th ones (inclusively, counting from 1) in the sequence.
Input
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 A1..AN.
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.
Output
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.
Example
Input:
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 Output:
Case #1:
9
4
6 Case #2:
40
26
Hint
| Added by: | Fudan University Problem Setters |
| Date: | 2008-05-21 |
| Time limit: | 0.5s |
| Source limit: | 50000B |
| Memory limit: | 1536MB |
| Cluster: | Cube (Intel G860) |
| Languages: | All except: C99 strict ERL JS PERL 6 |
| Resource: | Own problem, used in ACM/ICPC Regional Contest, Shanghai 2011 preliminary |
区间开方,询问区间和。
@Bzoj3038 上帝造题的七分钟2
这题常见的解法是并查集,然而现在放在了线段树的套题里,就要思考怎么用线段树做了……
然而根本不虚哈哈哈哈 我当初就是用线段树做的哈哈哈哈
如果一个数已经变成了1,继续开方也只会得到1 。只要在线段树里标记出已经都变成1的连续区间,修改的时候就可以跳过整段区间,大幅度提高效率。
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
long long read1(){
long long x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
long long data[mxn];
struct node{
long long num;
bool one;
}t[mxn<<];
void Build(int l,int r,int rt){
if(l==r){
t[rt].num=data[l];
if(data[l]==)t[rt].one=;
else t[rt].one=;
return;
}
int mid=(l+r)>>;
Build(ls);Build(rs);
t[rt].one=(t[rt<<].one&t[rt<<|].one);
t[rt].num=t[rt<<].num+t[rt<<|].num;
return;
}
void change(int L,int R,int l,int r,int rt){
if(l==r && L<=l && r<=R){
t[rt].num=sqrt(t[rt].num);
if(t[rt].num==) t[rt].one=;
return;
}
int mid=(l+r)>>;
if(L<=mid && !t[rt<<].one)change(L,R,ls);
if(R>mid && !t[rt<<|].one)change(L,R,rs);
if(t[rt<<].one && t[rt<<|].one) t[rt].one=;
t[rt].num=t[rt<<].num+t[rt<<|].num;
return;
}
long long smm(int L,int R,int l,int r,int rt){
if(L<=l && r<=R){
return t[rt].num;
}
long long res=;
int mid=(l+r)>>;
if(L<=mid)res+=smm(L,R,ls);
if(R>mid)res+=smm(L,R,rs);
return res;
}
int op;
int main(){
int T=;
while(scanf("%d",&n)!=EOF){
printf("Case #%d:\n",++T);
int i,j;
for(i=;i<=n;i++)
data[i]=read1();
Build(,n,);
m=read();
int x,y;
while(m--){
op=read();x=read();y=read();
if(x>y)swap(x,y);
if(op==){
change(x,y,,n,);
}
else{
long long ans=smm(x,y,,n,);
printf("%lld\n",ans);
}
}
printf("\n");
}
return ;
}
SPOJ GSS4 Can you answer these queries IV的更多相关文章
- SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集
[题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...
- 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暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国
SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...
- GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)
GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...
- SP2713 GSS4 - Can you answer these queries IV(线段树)
传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...
- 题解【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 ...
- Spoj 2713 Can you answer these queries IV 水线段树
题目链接:点击打开链接 题意: 给定n长的序列 以下2个操作 0 x y 给[x,y]区间每一个数都 sqrt 1 x y 问[x, y] 区间和 #include <stdio.h> # ...
- 【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 ...
随机推荐
- 一个Java Dao测试用例
import static org.junit.Assert.assertTrue; import java.util.Date; import java.util.HashMap; import j ...
- Openwrt iptables分析
这里将载有Openwrt的WR841N的路由表dump出来分析一下. 这个是dump出iptables的命令 root@OpenWrt:/etc/config# iptables-save 这里分为4 ...
- Nginx文件类型错误解析漏洞--攻击演练
今天看书看到其中提到的一个漏洞,那就是Nginx+PHP的服务器中,如果PHP的配置里 cgi.fix_pathinfo=1 那么就会产生一个漏洞.这个配置默认是1的,设为0会导致很多MVC框架(如T ...
- 玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
前文 的最后给出了玉伯的一道课后题,今天我们来讲讲这题的思路. 题目是这样的: Number.MAX_VALUE + 1 == Number.MAX_VALUE; Number.MAX_VALUE + ...
- 奇怪的Js时间计算方法,跨多个月后出现1天的误差
在项目中要求用计算两个时间相差的天数,通俗的说就是两个时间 相减, 我的方法 先把两个时间转成相应的毫秒,相减后,再除以(1000 * 60 * 60 * 24) 就可以得到对应天数,但天数会比实际少 ...
- OS存储器管理(二)
离散分配 分页(Paging),分段,段页式 一.分页 一个进程的物理地址可以是非连续的: 将物理内存分成固定大小的块,称为块(frame): 将逻辑内存分为同样大小的块,称为页(page): ...
- 吉特仓库管理系统-.SQL Server 2012 升级企业版
随着业务数据的不断增大,单表的数量已经上亿,查询的数据越来越慢,所以考虑到将数据库表分区,同时也将数据库升级到SQL Server 2012. 当时没有考虑更多,在服务器上安装了 SQL Server ...
- js的bind方法
转载:http://www.jb51.net/article/94451.htm http://www.cnblogs.com/TiestoRay/p/3360378.html https://seg ...
- Beta版本冲刺Day4
会议讨论: 628:由于昨天的考试我们组目前只把项目放到了服务器上,配置Java环境遇到了问题.601:将一些原来的界面进行了修改,修改成了更加美观的外形. 528:进行一些还未完成得到功能,比如查询 ...
- “CEPH浅析”系列之六——CEPH与OPENSTACK
在 <"Ceph浅析"系列之二--Ceph概况>中即已提到,关注Ceph的原因之一,就是OpenStack社区对于Ceph的重视.因此,本文将对Ceph在OpenSta ...