Can you answer these queries?---hdu4027
有n个数:当操作为1时求L到R的和;
当操作为0时更新L到R为原来的平方根;
不过如果仔细演算的话会发现一个2^64数的平方根开8次也就变成了 1,所以也更新不了多少次,所以可以每次更新到底。、
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#define INF 0xfffffff
#define N 100010
using namespace std; #define Lson r<<1
#define Rson r<<1|1 struct SegTree
{
int L, R;
long long sum;
int mid()
{
return (L+R)>>;
}
int len()
{
return R-L+;
}
}a[N*]; void Update(int r, int L, int R)
{
if(a[r].len()==a[r].sum)
return ;
if(a[r].L == a[r].R)//到达叶子节点;
{
a[r].sum = (long long)sqrt(a[r].sum);
return ;
}
if(R<=a[r].mid())
Update(Lson, L, R);
else if(L > a[r].mid())
Update(Rson, L, R);
else
{
Update(Lson, L, a[r].mid());
Update(Rson, a[r].mid()+, R);
}
a[r].sum = a[Rson].sum + a[Lson].sum;
} void BuildSegTree(int r, int L, int R)
{
a[r].L = L, a[r].R = R;
if(L == R)
{
scanf("%I64d", &a[r].sum);
return;
}
BuildSegTree(Lson, L, a[r].mid());
BuildSegTree(Rson, a[r].mid()+, R); a[r].sum = a[Rson].sum + a[Lson].sum;
}
long long Query(int r, int L, int R)
{
if(a[r].L == L && a[r].R == R)
{
return a[r].sum;
}
if(L>a[r].mid())
return Query(Rson, L ,R);
else if(R <= a[r].mid())
return Query(Lson, L, R);
else
{
long long ans1 = Query(Lson, L, a[r].mid());
long long ans2 = Query(Rson, a[r].mid()+, R);
return ans1 + ans2;
}
}
int main()
{
int n, m, L, R, op, t=;
while(scanf("%d", &n) != EOF)
{
BuildSegTree(, , n);
scanf("%d", &m);
printf("Case #%d:\n", t++);
while(m--)
{
scanf("%d%d%d", &op, &L, &R);
if(L>R)swap(L, R);//L和R的大小没说;
if(op==)
Update(, L, R);
else
{
long long ans=Query(, L, R);
printf("%I64d\n", ans);
}
}
printf("\n");
}
return ;
}
Can you answer these queries?---hdu4027的更多相关文章
- (线段树 区间查询更新) Can you answer these queries? -- hdu--4027
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4027 分析:因为这个操作是把一个数变成平方根,所以显得略棘手,不过如果仔细演算的话会发现一个2^64数的 ...
- Can you answer these queries?-HDU4027 区间开方
题意: 给你n个数,两个操作,0为区间开方,1为区间求和 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 思路: 如果当该区间的数都为1,我们没必要 ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- HDU4027 Can you answer these queries?(线段树 单点修改)
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- kuangbin专题七 HDU4027 Can you answer these queries? (线段树)
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
随机推荐
- PostgreSQL的表空间
1. 表空间的概念 PostgreSQL中的表空间允许在文件系统中定义用来存放表示数据库对象的文件的位置.在PostgreSQL中表空间实际上就是给表指定一个存储目录. 2. 表空间的作用 官方解释 ...
- TCPdump指定时间或者指定大小进行循环抓取报文
背景:我们用tcpdump工具循环抓取网卡上的报文,我们会遇到如下情况: 1. 抓取报文后隔指定的时间保存一次: 2. 抓取报文后达到指定的大小保存一次: 本文就这两种情况给出tcpdump的使用方法 ...
- npm yarn
1.从接触nodejs开始,一直就青睐于npm包管理工具,熟悉的命令以及提供的各种便利,也让自己没有想过更换为其他的:但是,有人也说过“海纳百川,方可走远”.因此还是有必要了解一下其他的包管理工具,比 ...
- Java枚举的小用法
package com.lxc.wmb; public enum TestEnum { Success("200","成功!"), Faild("50 ...
- (原)强类型dataset(类型化dataset)中动态修改查询条件(不确定参数查询)
原创博客,转载请注明:http://www.cnblogs.com/albert1017/p/3361932.html 查询时有多个参数,参数个数由客户输入决定,不能确定有多少个参数,按一般的方法每种 ...
- PKI技术原理
转:http://3layer.blog.51cto.com/57448/20430 对称加密 symmetric cryptographic 非对称加密 asymmetric ...
- c++ istream转换为std::string
std::istreambuf_iterator<char> eos; std::string s(std::istreambuf_iterator<char>(stream) ...
- sencha touch Container
Container控件是我们在实际开发中最常用的控件,大部分视图控件都是继承于Container控件,了解此控件能帮我们更好的了解sencha touch. layout是一个很重要的属性,能够帮助你 ...
- Node.j中path模块对路径的操作
一.path模块 https://nodejs.org/docs/latest/api/path.html#path_path_join_paths 1.join方法 ==> 该方法将多个参数值 ...
- [C#/Java] C#中调用Servlet示例
需求 通用消息接口使用servlet作为服务器端服务接口,第三方应用程序通过http post的方式调用servlet,实现与通用消息接口的调用连接. 参数说明如下: msgTitle:消息标题,描述 ...