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 ...
随机推荐
- noi1696 逆波兰表达式
1696:逆波兰表达式 http://noi.openjudge.cn/ch0303/1696/ 总时间限制: 1000ms 内存限制: 65536kB 描述 逆波兰表达式是一种把运算符前置的算术 ...
- 几种任务调度的 Java 实现方法与比较
综观目前的 Web 应用,多数应用都具备任务调度的功能.本文由浅入深介绍了几种任务调度的 Java 实现方法,包括 Timer,Scheduler, Quartz 以及 JCron Tab,并对其优缺 ...
- c++虚函数注意事项
>在基类方法声明中使用关键字virtual,可以使该方法在基类及所有的派生类中是虚的 >如果使用指向对象的引用或指针来调用虚方法,程序将使用对象类型定义的方法,而不使用为引用或指针类型定义 ...
- 两个Canvas小游戏
或许连小游戏都算不上,可以叫做mini游戏. 没有任何框架或者稍微有点深度的东西,所以有js基础的或者要追求炫酷效果的可以直接ctrl+w了. 先贴出两个游戏的试玩地址: 是男人就走30步 是男人就忍 ...
- 单元测试中Assert类的用法
Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.VisualStudio.Quality ...
- 生成短链(网址) ShortUrlLink
建表 CREATE TABLE [dbo].[ShortUrl]( [Id] [,) NOT NULL, [LongUrl] [nvarchar]() NOT NULL, [BaseUri] [int ...
- HTTP请求头参数
Accept-Language: zh-cn,zh;q=0.5 意思:浏览器支持的语言分别是中文和简体中文,优先支持简体中文. 详解: Accept-Language表示浏览器所支持的语言类型: ...
- win10 进入安全模式的方法
[收藏] 楼主 水际天成 只看他 2014-12-19 17:53:26 Windows10出问题了,无法加载了,一直停留在鼠标刚刚出现的那个界面,只能看到计算机屏幕变了颜色,然后就没有任何反映了.想 ...
- git查看提交历史
git日志的查看 在使用 Git 提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用 git log 命令查看. 查看日志信息: $ git log 可以用 --oneline ...
- Windows命令 dos
1.dos下运行netstat -na 查看本机开启的端口