hdu4027Can you answer these queries?(线段树)
算是裸线段树了,因为没个数最多开63次 ,开到不能再看就标记。查询时,如果某段区间被标记直接返回结果,否则继续向儿子节点更新。
注意用——int64
注意L会大于R 这点我很纠结。。您出题人故意的吗 WAn次。。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100010
#define LL __int64
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
LL s[N<<],f[N<<];
LL a[N];
void up(int w)
{
s[w] =s[w<<]+s[w<<|];
f[w] = (f[w<<]&&f[w<<|]);
}
void build(int l,int r,int w)
{
if(l==r)
{
s[w] = a[l];
if(a[l]==||a[l]==) f[w] = ;
return ;
}
int m = (l+r)>>;
build(l,m,w<<);
build(m+,r,w<<|);
up(w);
}
void update(int a,int b,int l,int r,int w)
{
if(a<=l&&b>=r)
{
if(l==r)
{
LL k = sqrt(s[w]*1.0);
if(k*k>s[w])
k--;
s[w] = k;
if(s[w]<=)
f[w] = ;
// up(w);
return ;
}
if(f[w])
{
up(w);
return ;
}
else
{
int m = (l+r)>>;
if(a<=m)
update(a,b,l,m,w<<);
if(b>m)
update(a,b,m+,r,w<<|);
up(w);
return ;
}
}
int m = (l+r)>>;
if(a<=m)
update(a,b,l,m,w<<);
if(b>m)
update(a,b,m+,r,w<<|);
up(w);
}
LL query(int a,int b,int l,int r,int w)
{
if(a<=l&&b>=r)
{
return s[w];
}
int m = (l+r)>>;
LL res = ;
if(a<=m) res+=query(a,b,l,m,w<<);
if(b>m) res+=query(a,b,m+,r,w<<|);
return res;
}
int main()
{
int i,n,q;
int k,x,y,kk=;
while(scanf("%d",&n)!=EOF)
{
memset(f,,sizeof(f));
for(i = ; i <=n ;i++)
{
scanf("%I64d",&a[i]);
}
build(,n,);
scanf("%d",&q);
printf("Case #%d:\n",++kk);
while(q--)
{
scanf("%d%d%d",&k,&x,&y);
if(x>y) swap(x,y);
if(k)
{
printf("%I64d\n",query(x,y,,n,));
}
else
{
update(x,y,,n,);
}
}
puts("");
}
return ;
}
hdu4027Can you answer these queries?(线段树)的更多相关文章
- HDU-4027-Can you answer these queries?线段树+区间根号+剪枝
传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...
- HDU 4027 Can you answer these queries? (线段树区间修改查询)
描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...
- hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和
Can you answer these queries? Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- HDU4027 Can you answer these queries? 线段树
思路:http://www.cnblogs.com/gufeiyang/p/4182565.html 写写线段树 #include <stdio.h> #include <strin ...
- 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 ...
- HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
随机推荐
- Windows Vista for Developers——第四部分:用户帐号控制(User Account Control,UAC)
作者:Kenny Kerr 翻译:Dflying Chen 原文:http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-f ...
- Building Objective-C static libraries with categories(ObjC、all_load、force_load)
https://developer.apple.com/library/mac/qa/qa1490/_index.html 之所以使用该标志,和Objective-C的一个重要特性:类别(cat ...
- c语言里如何调用汇编里的变量?
c语言里如何调用汇编里的变量? 汇编语言:是声明全局变量 .globl _end_ofs _end_ofs: .word _end - _start c语言:声明这个变量,然后再调用这个变量 void ...
- 类似懒加载的js功能
<!-- 显示15条数据 --> <ul class="list" pagesize="15"> <li> <div ...
- SQL 系统表
http://www.cnblogs.com/asdcer/archive/2007/05/14/746377.aspx
- Robot FrameWork基础学习(三)
一.关键字(Keyword)根据架构的区分可分为以下三层结构: 底层关键字.公共层关键字.特性关键字. 底层关键字一般与最底层的代码在关系,为上层公共关键字和特性关键字提供接口. 公共层关键字:一般是 ...
- 关于checkbox对应一张表时如何遍历
html代码: <table> <tr> <td><input name='one' type='checkbox' value='1'/></t ...
- python 之元类
定义类的两种方法: 1.class定义 2.type(类名,类的基类们,类的名称空间) # 定义类的三要素:类名.基类.名称空间 class_name = 'Chinese' class_bases ...
- UVa 1627 Team them up! (01背包+二分图)
题意:给n个分成两个组,保证每个组的人都相互认识,并且两组人数相差最少,给出一种方案. 析:首先我们可以知道如果某两个人不认识,那么他们肯定在不同的分组中,所以我们可以根据这个结论构造成一个图,如果两 ...
- 4.1 手写Java PriorityQueue 核心源码 - 原理篇
本章先讲解优先级队列和二叉堆的结构.下一篇代码实现 从一个需求开始 假设有这样一个需求:在一个子线程中,不停的从一个队列中取出一个任务,执行这个任务,直到这个任务处理完毕,再取出下一个任务,再执行. ...