题意 其实就是选两个点 d[i]表示他们的距离前缀和 h[i]为原来高度的两倍
那么就是 d[i] - d[j] + 2(h[i] + h[j]) 可以看成两个式子 d[i]+h[i] - (d[j]-h[j])
那是不是取前面的最大和后面的最小就可以符合了?
有可能两个值在同一个下标 需要取别的情况讨论
用st表会特别简洁优美
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){ll ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
const int MAX_N = 200025;
ll d[MAX_N],h[MAX_N],A[MAX_N],B[MAX_N];
int ma[MAX_N][21],mi[MAX_N][21],lg[MAX_N];
int Max(int a,int b){return A[a]>A[b]?a:b;}
int Min(int a,int b){return B[a]<B[b]?a:b;}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
void init(int n)
{
A[0] = _INF,B[0] = INF;
for(ll i = 1,sum = 0;i<=n*2;++i)
{
sum+=d[i];
A[i] = h[i]+sum;
B[i] = sum-h[i];
ma[i][0] = mi[i][0] = i;
}
for(int i = 0,j = 1;j<=2*n;++j)
lg[j] = 1<<(i+1)==j?++i:i;
for(int j = 1;(1<<j)<=2*n;++j)
for(int i = 1;i+(1<<j)-1<=2*n;++i)
{
ma[i][j] = Max(ma[i][j-1],ma[i+(1<<(j-1))][j-1]);
mi[i][j] = Min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
}
}
int getma(int l,int r)
{
if(l>r) return 0;
int j = lg[r-l+1];
return Max(ma[l][j],ma[r-(1<<j)+1][j]);
}
int getmi(int l,int r)
{
if(l>r) return 0;
int j = lg[r-l+1];
return Min(mi[l][j],mi[r-(1<<j)+1][j]);
}
ll query(int l,int r)
{
int x = getma(l,r),y = getmi(l,r);
if(x!=y) return A[x]-B[y];
int an_x = Max(getma(l,x-1),getma(x+1,r));
int an_y = Min(getmi(l,x-1),getmi(x+1,r));
return max(A[an_x]-B[x],A[x]-B[an_y]);
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,m,x,y;
scanf("%d%d",&n,&m);
for(int i = 1;i<=n;++i) scanf("%lld",&d[i%n+1]),d[i%n+1+n] = d[i%n+1];
for(int i = 1;i<=n;++i) scanf("%lld",&h[i]),h[i]<<=1,h[i+n] = h[i];
init(n);
while(m--)
{
scanf("%d%d",&x,&y);
if(x<=y) printf("%lld\n",query(y+1,n+x-1));
else printf("%lld\n",query(y+1,x-1));
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}