您的位置:首页 > 其它

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

2016-01-18 08:32 344 查看
C. Drazil and Park

time limit per test
2 seconds

memory limit per test
512 megabytes

input
standard input

output
standard output

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 test(s)

input
5 3
2 2 2 2 2
3 5 2 1 4
1 3
2 2
4 5


output
12
16
18


input
3 3
5 1 4
5 1 4
3 3
2 2
1 1


output
17
22
11


好题

RMQ

#include <iostream>
#include<stdio.h>
#include<math.h>
#define NN 210000
using namespace std;
int n,c1[NN][18],c2[NN][18];
__int64 w1[NN],w2[NN],w3[NN],w4[NN],b[NN],a[NN];

void rmq(__int64 *r,__int64 *q,__int64 *h,int (*s)[18])
{
for(int i=1;i<=2*n;i++)
{
r[i]=q[i]+h[i]*2;
s[i][0]=i;
}
for(int i=1,k=1;i<=n;i*=2,k++)
{
for(int j=1;j<=2*n;j++)
{
if (i+j>2*n) s[j][k]=s[j][k-1];
else
s[j][k]=(r[s[j][k-1]]>r[s[j+i][k-1]]?s[j][k-1]:s[j+i][k-1]);
}
}

}
__int64 dog(int x,int y,__int64 *q,int (*s)[18],int &u)
{
int tmp=(int)(log(y-x+1)/log(2.0));
int k;
tmp=(tmp-1>=0?tmp-1:0);
for(int i=tmp;i<=tmp+2;i++)
if (y-x+1<=(1<<(i+1)))
{
k=i;
break;
}
if (q[s[x][k]]>=q[s[y-(1<<k)+1][k]])
{
u=s[x][k];
return q[s[x][k]];
}
u=s[y-(1<<k)+1][k];
return q[s[y-(1<<k)+1][k]];
}
__int64 ask(int x,int  y)
{
__int64 pt1,pt2,k1,k2;
int index1,index2,index3,index4;
dog(x+2,y-1,w3,c1,index1);
dog(x+1,index1-1,w4,c2,index2);
pt1=w1[index1]-w1[index2]+2*(b[index1]+b[index2]);

dog(x+1,y-2,w4,c2,index3);
dog(index3+1,y-1,w3,c1,index4);
pt2=w1[index4]-w1[index3]+2*(b[index3]+b[index4]);
return (pt1>pt2?pt1:pt2);
}
int main()
{
int m,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
a[i+n]=a[i];
}
for(int i=1;i<=n;i++)
{
scanf("%I64d",&b[i]);
b[i+n]=b[i];
}
w1[1]=0;
w2[2*n]=0;
for(int i=2;i<=2*n;i++)
w1[i]=w1[i-1]+a[i-1];
for(int i=2*n-1;i>=1;i--)
w2[i]=w2[i+1]+a[i];
rmq(w3,w1,b,c1);
rmq(w4,w2,b,c2);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if (x>y) y+=n;
printf("%I64d\n",ask(y,x+n));
}
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: