A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.

You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.

Input

The input contains several test cases, terminated by EOF.

For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)

The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63.

The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)

For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.

Output

For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.

Sample Input

10

1 2 3 4 5 6 7 8 9 10

5

0 1 10

1 1 10

1 1 5

0 5 8

1 4 8

Sample Output

Case #1:

19

7

6

题意:

给你一个含有n个数的数组,m次操作

操作0,给你一个区间l, r 对区间内的每一个数开方。

操作1,输出一个询问区间的数值sum和。

思路:

我们看到数据范围是小于等于2^63 ,我们通过本地开方测试可以发现,

数组中的每一个数,最多开方7次,就可以到1 ,, 而1无论咋开方都还是1 ,就是不会改变的数值了。

那么我们用线段树维护区间的sum和,

对于更新,我们暴力更新到线段树的每一个叶子节点,求和就正常的求和。

这里有一点必须的优化就是 如果线段树一个区间的sum和等于区间的长度,这个得出这个区间中的每一个值都是1,那么直接return,不去更新,因为更新是没意义的。

本题2个坑点,::

1,给的区间x,y ,并不是 x<y 的,有可能 x>y 此处wa多次。

2、题面讲到每一个样例多输出一个回车,刚开始没看到。 此处pe一次。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, m;
int root;
ll a[maxn];// 初始点权
ll wt[maxn];// 新建编号点权。
int cnt;// 编号用的变量
int top[maxn];// 所在重链的顶点编号
int id[maxn];//节点的新编号。
std::vector<int> son[maxn];
int SZ[maxn];// 子数大小
int wson[maxn];// 重儿子
int fa[maxn];// 父节点
int dep[maxn];// 节点的深度
struct node
{
int l,r;
ll sum;
ll laze;
}segment_tree[maxn<<2]; void pushup(int rt)
{
segment_tree[rt].sum=(segment_tree[rt<<1].sum+segment_tree[rt<<1|1].sum);
}
void build(int rt,int l,int r)
{
segment_tree[rt].l=l;
segment_tree[rt].r=r;
segment_tree[rt].laze=0;
if(l==r)
{
segment_tree[rt].sum=wt[l];
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
pushup(rt);
} void update(int rt,int l,int r)
{
if((segment_tree[rt].l>=l&&segment_tree[rt].r<=r)&&segment_tree[rt].sum==(segment_tree[rt].r-segment_tree[rt].l+1))
{
return ;
}
if(segment_tree[rt].l==segment_tree[rt].r)
{
segment_tree[rt].sum=sqrt(segment_tree[rt].sum);
return ;
}
int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;
if(mid>=l)
{
update(rt<<1,l,r);
}
if(mid<r)
{
update(rt<<1|1,l,r);
}
pushup(rt);
}
ll query(int rt,int l,int r)
{
if(segment_tree[rt].l>=l&&segment_tree[rt].r<=r)
{
ll res=0ll;
res+=segment_tree[rt].sum;
return res;
}
int mid=(segment_tree[rt].l+segment_tree[rt].r)>>1;
ll res=0ll;
if(mid>=l)
{
res+=query(rt<<1,l,r);
}
if(mid<r)
{
res+=query(rt<<1|1,l,r);
}
return res; } int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
// freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); gbtb;
int cas=1;
while(cin>>n)
{
cout<<"Case #"<<cas<<":"<<endl;
repd(i,1,n)
{
cin>>wt[i];
}
build(1,1,n);
cin>>m;
int op;
int x,y;
while(m--)
{
cin>>op>>x>>y;
if(x>y)
{
swap(x,y);
}
if(!op)
{
update(1,x,y);
}else
{
cout<<query(1,x,y)<<endl;
}
}
cout<<endl;
cas++;
} return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Can you answer these queries? HDU - 4027 (线段树,区间开平方,区间求和)的更多相关文章

  1. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  2. V - Can you answer these queries? HDU - 4027 线段树 暴力

    V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...

  3. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  5. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

  6. SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

    GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...

  7. Can you answer these queries III(线段树)

    Can you answer these queries III(luogu) Description 维护一个长度为n的序列A,进行q次询问或操作 0 x y:把Ax改为y 1 x y:询问区间[l ...

  8. hdu4027Can you answer these queries?【线段树】

    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...

  9. 2018.10.16 spoj Can you answer these queries V(线段树)

    传送门 线段树经典题. 就是让你求左端点在[l1,r1][l1,r1][l1,r1]之间,右端点在[l2,r2][l2,r2][l2,r2]之间且满足l1≤l2,r1≤r2l1\le l2,r1 \l ...

随机推荐

  1. 惠州双月湾游记 & 攻略

    惠州双月湾游记&攻略 2019 年的 11 月底和小朱.Josie 约了快乐周末去惠州双月湾玩! 我和时猪一起从武汉出发到广州,然后和他们俩一起从广州自驾去的惠州.大致行程如下: Day 1: ...

  2. Redis 几个类型常用命令

    Redis 字符串(String) 下表列出了常用的 redis 字符串命令: 序号 命令及描述1 SET key value 设置指定 key 的值2 GET key 获取指定 key 的值.3 G ...

  3. java:面向对象(多态,final,抽象方法,(简单工厂模式即静态方法模式),接口)

    * 生活中的多态:同一种物质,因环境不同而表现不同的形态. * 程序中多态:同一个"接口",因不同的实现而执行不同的操作. * 多态和方法的重写经常结合使用,子类重写父类的方法,将 ...

  4. CTF—攻防练习之HTTP—暴力破解

    攻击机:192.168.32.152 靶机:192.168.32.164 首先nmap,nikto -host ,dirb 扫描开放带端口,探测敏感文件,扫描目录 开放了21,22,80端口,看到一个 ...

  5. pytorch中的激励函数(详细版)

          初学神经网络和pytorch,这里参考大佬资料来总结一下有哪些激活函数和损失函数(pytorch表示)      首先pytorch初始化:   import torch import t ...

  6. caoz的梦呓:信息安全常识科普

    猫宁!!! 参考链接:https://mp.weixin.qq.com/s/cl4TfOodBGSjUuEU8e0rGA 对方公众号:caoz的梦呓 前天在新加坡IC咖啡做了一场关于信息安全的常识普及 ...

  7. jvm的学习笔记:二、类的初始化,代码实战(1)

    对于静态字段来说,直接定义该字段的类才会被初始化 System.out.println(MyChild1.str); 输出: myParent1 static block hello myParent ...

  8. FTP简单搭建(一)

    一.FTP服务介绍 vsftp(very security ftp file transfer protocol 非常文件传输协议) FTP分为主动模式和被动模式. 主动模式:(不安全,传数据的端口是 ...

  9. pycharm2019最新激活注册码(亲测有效)

    激活码一: 812LFWMRSH-eyJsaWNlbnNlSWQiOiI4MTJMRldNUlNIIiwibGljZW5zZWVOYW1lIjoi5q2j54mIIOaOiOadgyIsImFzc2l ...

  10. SVN随笔记录(二)

    二.TortoiseSVN操作 1.下载,安装,过程中需要勾选x ,目的是为了后期绑定idea 2.如果点击后出现一系列的找不到目标文件提示,重启电脑 3.重启后,绑定仓库路径 4.一般情况输入账号密 ...