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. Linux wait函数详解

    wait和waitpid出现的原因 SIGCHLD --当子进程退出的时候,内核会向父进程SIGCHLD信号,子进程的退出是个异步事件(子进程可以在父进程运行的任何时刻终止) --子进程退出时,内核将 ...

  2. [MetaHook] Event Hook

    #include <metahook.h> struct event_hook_t { event_hook_t *next; char *name; void (*pfnEvent)(e ...

  3. FineUI v4.0.2 (beta) 发布了!

    FineUI v4.0.2 (beta) 已经于 2013-12-15 发布! ================================== 关于FineUI基于 ExtJS 的开源 ASP. ...

  4. 防止 JavaScript 自动插入分号

    JavaScript语言有一个机制:在解析时,能够在一句话后面自动插入一个分号,用来修改语句末尾遗漏的分号分隔符. 然而,由于这个自动插入的分号与JavaScript语言的另一个机制发生了冲突,即所有 ...

  5. 读懂IL代码就这么简单(二)

    一 前言 IL系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...

  6. React Native 在现有项目中的探路

    移动开发中,native开发性能和效果上无疑是最好的. 但是在众多的情况下,native开发并不是最优的选择.当需求经常改动的时候,当预算有限的时候,当deadline很近的时候,native开发的成 ...

  7. Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较

    1.概述 创建Docker镜像的方式有三种 docker commit命令:由容器生成镜像: Dockerfile文件+docker build命令: 从本地文件系统导入:OpenVZ的模板. 关于这 ...

  8. java发送邮件

    1.需要用到javax.mail怎么下载呢?百度javax.mail就会看见http://www.oracle.com/technetwork/java/index-138643.html实际上这个项 ...

  9. C# Cut Line Bressenham Algorithm

    using System; using System.Drawing; using System.Windows.Forms; namespace CutLine { static class Pro ...

  10. 东大OJ-1588: Routing Table

    题目描述 In the computer network, a Router is a device which finds an optimal way to transmit the datagr ...