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的更多相关文章

  1. SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集

    [题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...

  2. GSS4 - Can you answer these queries IV(线段树懒操作)

    GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...

  3. 线段树 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 ...

  4. GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)

    GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...

  5. SP2713 GSS4 - Can you answer these queries IV(线段树)

    传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...

  6. 题解【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 ...

  7. Spoj 2713 Can you answer these queries IV 水线段树

    题目链接:点击打开链接 题意: 给定n长的序列 以下2个操作 0 x y 给[x,y]区间每一个数都 sqrt 1 x y 问[x, y] 区间和 #include <stdio.h> # ...

  8. 【SP2713 GSS4 - Can you answer these queries IV】 题解

    题目链接:https://www.luogu.org/problemnew/show/SP2713 真暴力啊. 开方你开就是了,开上6次就都没了. #include <cmath> #in ...

  9. 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 ...

随机推荐

  1. Html5 Egret游戏开发 成语大挑战(三)开始界面

    本篇需要在前面的素材准备完毕,才可以开始,使用egret的eui结合代码编辑,快速完成基本的界面搭建,这里写的可能比较细,目的是减少大家对于其中一些操作疑问,我去掉了很多无用的步骤,以最精简的流程来完 ...

  2. Linux 网络编程详解三(p2p点对点聊天)

    //p2p点对点聊天多进程版--服务器(信号的使用) #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  3. struts2和spring3.2的整合 详细演示

    1.首先我们新建一个Web工程,如下: 2.导入Spring和Struts2的jar包. 其中,struts2-spring-plugin-2.1.8.jar是struts2.spring整合的关键. ...

  4. lecture14-RBM的堆叠、修改以及DBN的决策学习和微调

    这是Hinton的第14课,主要介绍了RBM和DBN的东西,这一课的课外读物有三篇论文<Self-taught learning- transfer learning from unlabele ...

  5. 初学git:用git bash往github push代码

    对于我来说,最开始使用github主要是为了使用它的pages功能展示demo.其实这些都是用Github for Windows push上去的,图形化界面的客户端使用确实简单,但是逼格不够,好吧其 ...

  6. js的Array的map和sort实现方法

    Array.prototype.mapA = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "fu ...

  7. 弱占优策略--Weakly Dominant Strategy

    Weakly Dominant Strategy Equilibrium(均衡). 先说弱占优.一个策略弱占优就是说,无论其他人采取什么样的策略,这个策略的回报都大于等于其他策略的回报.如果所有人都使 ...

  8. doc2vec使用说明(一)gensim工具包TaggedLineDocument

    gensim 是处理文本的很强大的工具包,基于python环境下: 1.gensim可以做什么? 它可以完成的任务,参加gensim 主页API中给出的介绍,链接如下: http://radimreh ...

  9. koala不支持中文的解决办法(问题出现在使用中文字体时报错)

    C:\Program Files\Koala\rubygems\gems\sass-3.4.9\lib\sass 这是我的koala的安装路径,在sass文件夹下打开engine.rb(文本文档打开即 ...

  10. Go语言interface详解

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...