Spoj 2713 Can you answer these queries IV 水线段树
题目链接:点击打开链接
题意:
给定n长的序列
以下2个操作
0 x y 给[x,y]区间每一个数都 sqrt
1 x y 问[x, y] 区间和
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <math.h>
#include <vector>
#include <set>
using namespace std;
#define ll long long
#define N 100005
#define L(x) tree[x].l
#define R(x) tree[x].r
#define Lson(x) (x<<1)
#define Rson(x) (x<<1|1)
#define Num(x) tree[x].num
#define Sum(x) tree[x].sum
inline int Mid(int x, int y){return (x+y)>>1;}
struct Subtree{
int l, r;
ll num, sum;
}tree[N<<2];
ll a[N];
void push_up(int id){
if(L(id) == R(id))return;
Sum(id) = Sum(Lson(id)) + Sum(Rson(id));
if(Num(Lson(id)) <= 1 && Num(Rson(id)) <= 1)
Num(id) = 1;
else Num(id) = 2;
}
void build(int l, int r, int id){
L(id) = l; R(id) = r;
if(l == r) { Num(id) = Sum(id) = a[l]; return ;}
int mid = Mid(l, r);
build(l, mid, Lson(id));
build(mid+1, r, Rson(id));
push_up(id);
}
void updata(int l, int r, int id){
if(L(id) == R(id))
{
Sum(id) = Num(id) = (ll)sqrt((double)Sum(id));
return ;
}
if(l == L(id) && R(id) == r && Num(id) <= 1) return ; int mid = Mid(L(id), R(id));
if(mid < l)
updata(l, r, Rson(id));
else if(r <= mid)
updata(l, r, Lson(id));
else {
updata(l, mid, Lson(id));
updata(mid+1, r, Rson(id));
}
push_up(id);
}
ll query(int l, int r, int id){
if(l == L(id) && R(id) == r)
return Sum(id);
int mid = Mid(L(id), R(id));
if(mid < l)
return query(l, r, Rson(id));
else if(r <= mid)
return query(l, r, Lson(id));
else return query(l, mid, Lson(id)) + query(mid+1, r, Rson(id));
}
int n;
void solve(){
int q, op, x, y;
for(int i = 1; i <= n; i++)scanf("%lld", &a[i]);
build(1, n, 1);
scanf("%d",&q);
while(q--){
scanf("%d %d %d", &op, &x, &y);
if(x > y) swap(x, y);
if(op == 0)
updata(x, y, 1);
else
printf("%lld\n", query(x, y, 1));
}
}
int main(){
int Cas = 1;
while(~scanf("%d", &n)){
printf("Case #%d:\n", Cas++);
solve();
puts("");
}
return 0;
}
Spoj 2713 Can you answer these queries IV 水线段树的更多相关文章
- 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 ...
- 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 ...
- GSS4 - Can you answer these queries IV(线段树懒操作)
GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...
- Can you answer these queries? HDU 4027 线段树
Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...
- hdu4027Can you answer these queries?【线段树】
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- 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 ...
- V - Can you answer these queries? HDU - 4027 线段树 暴力
V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...
随机推荐
- php笔试算法题:顺时针打印矩阵坐标-蛇形算法
这几天参加面试,本来笔试比较简单,但是在面试的时候,技术面试官说让我现场写一个算法,顺时针打印矩阵的坐标,如图所示 顺序为,0,1,2,3,4,9,14,19,24,23,22,21,20,15,10 ...
- php数组分页类
<?php class ArrayPage{ public $totalPage;//全部页数 public $lists;//每页显示数目 public $arr = array();//分页 ...
- sp<> 强指针类的用法
在android 中可以广泛看到的template<typename T>, class Sp 句柄类实际上是android 为实现垃圾回收机制的智能指针.智能指针是c++ 中的一个概念 ...
- Python核心编程笔记----注释
python 中注释有两种 第一种,文档注释 第二种,一般的注释 下面是例子: class MyClass: '这个是文档注释' def __repr__(self): return "re ...
- 在CMD命令行下关闭进程的命令
转载: [重要]在CMD命令行下关闭进程的命令━━━━━━━━━━━━━━━━━━━━━━━━━━ 方法一: 在"运行"中输入:ntsd -c q -pn 程序名字(在MS-Dos ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(四)
通过前面的操作,我们已经可以创建一个带有我们自己的PCI的watchdog外设qemu 虚拟机了. 目的: 1. 了解我们的外设情况. 2. 为在guest中开发我们自己的linux PCI驱动程序做 ...
- HTML meta标签的用法及head中的一些常用标签
meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta主要为分HTTP标头信息(HTTP-EQUIV)和页面描述信息(NAME)标头信息包括文档类型.字符集.语言等浏览器正确显示网页的信 ...
- 04737_C++程序设计_第7章_类模板与向量
例7.1 使用类模板的实例. 例7.2 求4个数中最大值的类模板程序. #include <iostream> using namespace std; template <clas ...
- Ubuntu 14.04安装地里编码软件Nominatim过程
一.必须软件: 在Ubuntu系统编译执行Nominatim软件系统必须安装的软件有: 1.GCC 编译器 2.postgresql 数据库 3.proj4 4.geos 5.postgis 6.PH ...
- C# 第三方控件 下面的Item不显示了
当高版本的第三方版本 替换成低版本的第三方后,item,不显示了之后,请试着再次在这基础上添加一个Item,观察这个Item和原来已经在的却不显示的Item的区别在哪里.然后去源程序正常文件哪里 将这 ...