zoukankan
html css js c++ java
PKU3264线段树解法
#include
<
stdio.h
>
#include
<
string
.h
>
struct
node
{
node
*
pl,
*
pr;
int
left, right;
int
mxa, min;
}
mem[
100000
];
int
memCount;
int
n, q;
int
mxa, min;
node
*
newNode()
{
node
*
pt
=&
mem[memCount
++
];
pt
->
mxa
=-
1
, pt
->
min
=
1000001
;
return
pt;
}
node
*
buildTree(
int
l,
int
r)
{
node
*
root
=
newNode();
root
->
left
=
l;
root
->
right
=
r;
if
(r
-
l
>=
1
)
{
int
mid
=
(l
+
r)
/
2
;
root
->
pl
=
buildTree(l,mid);
root
->
pr
=
buildTree(mid
+
1
,r);
}
return
root;
}
void
update(node
*
root,
int
i,
int
a)
{
if
(root
->
left
==
i
&&
root
->
right
==
i)
{
root
->
mxa
=
a, root
->
min
=
a;
return
;
}
if
(root
->
min
>
a)
root
->
min
=
a;
if
(root
->
mxa
<
a)
root
->
mxa
=
a;
if
(root
->
pl
->
right
>=
i)
{
update(root
->
pl,i,a);
}
else
{
update(root
->
pr,i,a);
}
}
void
que(node
*
root,
int
i,
int
j)
{
if
(root
->
min
>
min
&&
root
->
mxa
<
mxa)
return
;
if
(root
->
left
==
i
&&
root
->
right
==
j)
{
if
(mxa
<
root
->
mxa)
mxa
=
root
->
mxa;
if
(min
>
root
->
min)
min
=
root
->
min;
return
;
}
if
(root
->
pl
->
right
>=
i)
{
if
(root
->
pl
->
right
>=
j)
que(root
->
pl, i, j);
else
{
int
mid
=
(root
->
left
+
root
->
right)
/
2
;
que(root
->
pl,i,mid);
que(root
->
pr,mid
+
1
,j);
}
}
else
{
que(root
->
pr,i,j);
}
}
int
main()
{
while
(scanf(
"
%d%d
"
,
&
n,
&
q)
==
2
)
{
memCount
=
0
;
node
*
root
=
buildTree(
1
, n);
int
i, a;
for
(i
=
0
;i
<
n;i
++
)
{
scanf(
"
%d
"
,
&
a);
update(root,i
+
1
,a);
}
int
x, y;
for
(i
=
0
;i
<
q;i
++
)
{
scanf(
"
%d%d
"
,
&
x,
&
y);
mxa
=-
1
, min
=
1000001
;
que(root,x, y);
printf(
"
%d\n
"
,mxa
-
min);
}
}
return
0
;
}
查看全文
相关阅读:
Python利用Remove.bg接口自动消除图片背景
解决ajxa跨域问题
CentOS7 修改静态IP
CentOS下 安装composer 与tp5.1
centon 安装php-fpm+Nginx
win10 安装selenium和使用
Scrapy 爬虫框架入门
Python 异常处理
selenium和phantomjs的介绍
MongoDB入门
原文地址:https://www.cnblogs.com/SQL/p/932354.html
最新文章
删完垃圾代码
删除垃圾代码前的一些保存
foreach ($users as $key=>$value)
WCF传输大数据 --断点续传(upload、download)
客户端调用wcf服务,如何提高调用性能
图片采集器_PHP
wcf传输Dataset大数据量 -压缩(一)
根据任意两点地图坐标算出距离是多少
Erasure Coding in WAS简单译文
infiniband学习总结
热门文章
JS代码放在head和body中的区别分析
在一般处理文件中访问Session需要添加IRequiresSessionState
重温HTML的基础
JqGrid帮助文档
判断身份证法则
NPOI简单操作Excel
错误与修复:ASP.NET无法检测IE10,IE11,导致_doPostBack未定义JavaScript错误,恒处于F5卷动条位置
C#对文件的操作
实现WebService的调用与被调用
c# 数据类型占用的字节数
Copyright © 2011-2022 走看看