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 线段树的更多相关文章

  1. Codeforces Round #292 (Div. 1) C - Drazil and Park

    C - Drazil and Park 每个点有两个值Li 和 Bi,求Li + Rj (i < j) 的最大值,这个可以用线段树巧妙的维护.. #include<bits/stdc++. ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  5. 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. 这题 ...

  6. 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 ...

  7. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  8. 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所 ...

  9. 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 ...

随机推荐

  1. html注意

    value的值是指input type="text" 等里面的value值,<p></p>标签里面的不是value值.

  2. 利用SET STATISTICS IO和SET STATISTICS TIME 优化SQL Server查询性能

    首先需要说明的是这篇文章的内容并不是如何调节SQL Server查询性能的(有关这方面的内容能写一本书),而是如何在SQL Server查询性能的调节中利用SET STATISTICS IO和SET ...

  3. 黑客们的故事(连载六):IT世界里的理想主义者

    一头疏于梳理的长发和一把肆意生长的大胡子,往往可能是人们对于理查德马修斯托曼的第一印象.这位马上就要过60岁生日的自由软件基金会主席不用手机.不买房.没有汽车,看起来像是个流浪汉,但是却是美国工程院院 ...

  4. 天气预报数据API

    http://www.weather.com.cn/data/cityinfo/101010100.html//过期: http://api.36wu.com/Weather/GetMoreWeath ...

  5. Titanium系列--利用Titanium开发android App实战总结

    1. Titanium中,通过ImageView Controller 显示图片,图片地址要放在app/assets下,然后自己建一个文件夹,把图片放在这里.如下图,然后alloy代码:<Ima ...

  6. PHP如何连接Access数据库

    PHP代码: <?php $connstr="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" .realpath(" ...

  7. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  8. AndroidStudio Lod.d在LogCat中不输出

    今天Log.d无论怎样都没有输出. 要在手机开发者选项那开启权限,莫名其妙,之前一直都没事的,具体操作参考:http://jingyan.baidu.com/article/84b4f56597e7b ...

  9. 换个角度说工作单元(Unit Of Work):创建、持有与API调用

    看到一些工作单元的介绍,有两种感觉,第一种是很学院,说了等于没说,我估计很多都是没有自己引入到实际的项目中去,第二种是告诉我一种结果,说这就是工作单元,但是没说为什么要这么使用.所以,本篇想要探讨的是 ...

  10. [WinAPI] API 7 [判断光驱内是否有光盘]

    判断光驱中是否有光盘,仍然可以使用GetDriveType和GetVolumeInformation函数实现.首先使用驱动器根路径作为GetDriveType和参数,如果返回值是DRIVE_CDROM ...