Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park
题目连接:
http://codeforces.com/contest/516/problem/C
Description
Drazil is a monkey. He lives in a circular park. There are n trees around the park. The distance between the i-th tree and (i + 1)-st trees is di, the distance between the n-th tree and the first tree is dn. The height of the i-th tree is hi.
Drazil starts each day with the morning run. The morning run consists of the following steps:
Drazil chooses two different trees
He starts with climbing up the first tree
Then he climbs down the first tree, runs around the park (in one of two possible directions) to the second tree, and climbs on it
Then he finally climbs down the second tree.
But there are always children playing around some consecutive trees. Drazil can't stand children, so he can't choose the trees close to children. He even can't stay close to those trees.
If the two trees Drazil chooses are x-th and y-th, we can estimate the energy the morning run takes to him as 2(hx + hy) + dist(x, y). Since there are children on exactly one of two arcs connecting x and y, the distance dist(x, y) between trees x and y is uniquely defined.
Now, you know that on the i-th day children play between ai-th tree and bi-th tree. More formally, if ai ≤ bi, children play around the trees with indices from range [ai, bi], otherwise they play around the trees with indices from .
Please help Drazil to determine which two trees he should choose in order to consume the most energy (since he wants to become fit and cool-looking monkey) and report the resulting amount of energy for each day.
Input
The first line contains two integer n and m (3 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting number of trees and number of days, respectively.
The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 109), the distances between consecutive trees.
The third line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 109), the heights of trees.
Each of following m lines contains two integers ai and bi (1 ≤ ai, bi ≤ n) describing each new day. There are always at least two different trees Drazil can choose that are not affected by children.
Output
For each day print the answer in a separate line.
Sample Input
5 3
2 2 2 2 2
3 5 2 1 4
1 3
2 2
4 5
Sample Output
12
16
18
Hint
题意
m个询问,问你区间[L,R]中h[a]2+h[b]2+dis(a,b)的最大值是多少。
要求a>b
题解
把dis(a,b)拆开成pred[a]-pred[b]
然后我令A = h[a]+pred[a],B = h[b]-pred[b]
那么询问就是问区间A+B的最大值,但是A得大于B。
所以线段树合并的时候注意一下就好了。
其实更简单的办法,就是记录最大值和次大值,然后XJB搞一搞也是可以的……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
const long long inf = 1LL*1e17;
long long h[maxn],d[maxn];
int n,q,a,b;
struct node{
int l,r;
long long A,B,AB;
}tree[maxn*4];
void build(int x,int l,int r){
tree[x].l=l,tree[x].r=r;
if(l==r){
tree[x].A=h[l]+d[l-1];
tree[x].B=h[l]-d[l-1];
tree[x].AB=-inf;
}else{
int mid=(l+r)/2;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
tree[x].A=max(tree[x<<1].A,tree[x<<1|1].A);
tree[x].B=max(tree[x<<1].B,tree[x<<1|1].B);
tree[x].AB=max(tree[x<<1].AB,max(tree[x<<1|1].AB,tree[x<<1].B+tree[x<<1|1].A));
}
}
node query(int x,int l,int r){
int L=tree[x].l,R=tree[x].r;
if(l<=L&&R<=r){
return tree[x];
}
int mid=(L+R)/2;
node tmp1,tmp2,tmp3;
tmp1.A=tmp1.B=tmp1.AB=tmp2.A=tmp2.B=tmp2.AB=tmp3.A=tmp3.B=tmp3.AB=-inf;
if(l<=mid)tmp1=query(x<<1,l,r);
if(r>mid)tmp2=query(x<<1|1,l,r);
tmp3.A=max(tmp1.A,tmp2.A);
tmp3.B=max(tmp1.B,tmp2.B);
tmp3.AB=max(tmp1.AB,max(tmp2.AB,tmp1.B+tmp2.A));
return tmp3;
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)scanf("%lld",&d[i]),d[n+i]=d[i];
for(int i=1;i<=n;i++)scanf("%lld",&h[i]),h[i]*=2,h[n+i]=h[i];
for(int i=1;i<=2*n;i++)d[i]+=d[i-1];
build(1,1,2*n);
for(int i=1;i<=q;i++){
scanf("%d%d",&a,&b);
if(a<=b)
printf("%lld\n",query(1,b+1,a+n-1).AB);
else printf("%lld\n",query(1,b+1,a-1).AB);
}
}
Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树的更多相关文章
- Codeforces Round #292 (Div. 1) C - Drazil and Park
C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
随机推荐
- java多条件不定条件查询
网站或各类管理系统都会用到搜索,会用到一个或多个不确定条件搜索,单条件搜索比较简单,有时候会有多个条件共同查询,如果系统中已经提供了相关的方法供你使用最好,像我做这老系统改版,需要添加搜索,就要自己写 ...
- oracle dblink调用函数
select 用户名.函数名@DBLINK名称(参数) from dual; e.g. select newbosid@TEST('1234ECMA') from dual; -- 成功执行 sel ...
- [原] XAF ListView显示隐藏Footer菜单
using System; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Win.Editors; using DevExpress ...
- oprofile 安装使用
ubuntu上要编译安装oprofile.现在版本(0.9.9)的oprofile的编译需要binutil-dev libpopt-dev apt-get install binutil-dev li ...
- spring jpa @Query中使用in
@Modifying @Query("delete from SmTenant s where s.id in ?1") void deleteByIds(List<Long ...
- jquery 插件原则
区分开 Dom 对象,jQuery Dom对象, Json数据. Dom对象是诸如: document.getElementById 返回的原生Dom对象. jQuery Dom对象,如:$(&quo ...
- 修改注册表来修改IE的设置---资料汇总
原文链接: http://blog.csdn.net/wangqiulin123456/article/details/17068649 附带批处理执行脚本: @echo off &title ...
- RSA密钥的跨平台通用
RSA使用public key加密,用private key解密(签名相反,使用private key签名,用public key验证签名).比如我跟合作方D之间的数据传输,我使用D提供给我的publ ...
- [自娱自乐] 3、超声波测距模块DIY笔记(三)
前言 上一节我们已经研究了超声波接收模块并自己设计了一个超声波接收模块,在此基础上又尝试用单片机加反相器构成生成40KHz的超声波发射电路,可是发现采用这种设计的发射电路存在严重的发射功率太低问题,对 ...
- [ACM_几何] Transmitters (zoj 1041 ,可旋转半圆内的最多点)
Description In a wireless network with multiple transmitters sending on the same frequencies, it is ...