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);
}
}

最新文章

  1. iOS中空字符串报错
  2. C# winform treeView checkbox全选反选
  3. 关于char的定义语句,正确的有()
  4. 使用FreePic2Pdf导出书签至Word建立层级目录——快速初始化Word笔记本目录
  5. SQL Server安全(7/11):使用跨数据库所有权链接(Cross-Database Ownership Chaining)的跨数据库安全
  6. CSS常用选择器名
  7. RNG vs EDG | SKT vs KTB [20160826]
  8. UDPsocket编程
  9. 【linux】chmod命令详细用法
  10. iOS开发之AsyncSocket使用教程
  11. mahout贝叶斯算法开发思路(拓展篇)1
  12. java excel导出
  13. Nginx配置抵御DDOS或CC攻击
  14. LNMP安装Let’s Encrypt 免费SSL证书方法:自动安装与手动配置Nginx
  15. vue集成百度UEditor富文本编辑器
  16. PHP 抓取网页内容的几个函数
  17. (三)初探maven之使用IDE
  18. BaseDao封装
  19. Kafka入门 --安装和简单实用
  20. NSTimer深入理解

热门文章

  1. NOIP2012 题解
  2. 再见Unity3d的死循环
  3. linux下如何查找需要的文件后并删除
  4. Java里的if else语句例子
  5. UVA 11732 strcmp() Anyone? (压缩版字典树)
  6. UITableView动态存放、重用机制
  7. 从Mono 4.0观C# 6.0部分新特性
  8. Arduino I2C + 三轴加速度计LIS3DH
  9. Javascript日期与C# DateTime 转换
  10. 为什么心跳包(HeartBeat)是必须的?