As we know, the majority of students in the world-class university like candy and game so much. With some candies, the students are playing a guessing game with you.
These students are standing in a line. Every student has some candies in the hand. (Of course the quantity of the candies will not be negative.) Some kindhearted students will tell you the exactly quantity of candies they have in hand, but some of them won't do that. You may think that THE GAME CAN'T PLAY, but the fact is, every student in line will tell you the exact total quantities of previous one (if exists), himself, and next one (if exists).
You should guess the maximum possible quantity the queried student might have.
Input
The input will consist of multiple testcases.
The first line of each test case is an integer n, indicating the amount of students, 3 ≤ n ≤100000.
The second line contains n integers, the ith number ai represent the exact quantity of candies the ith student has, it will be a nonnegative number not more than 10000, if ai equals to -1, it means that the corresponding student haven't tell you the candies' quantity.
The third line also contains n integers, the ith number represents the sum of ai-1, ai and ai+1 (the first and last student's number contain only two guys' summation).
The forth line contains an integer m, indicating the amount of queries, 1 ≤ m ≤100.Following m integers in a line indicate the 0-base number we queried.
Output
For each test case, you should output exactly m lines, each line contains an integer which is the answer of the corresponding query. If the queried quantity had been told in the input, just output that number.
Sample Input
5 -1 -1 -1 -1 -1 2 3 3 3 2 2 0 3
Sample Output
2 2
Hint
The quantities they have might be "2 0 1 2 0", "0 2 1 0 2", "1 1 1 1 1" and so on.
任何一个变量都可以由a1 表示,维护a1的左端点和右端点,[l,r], 刚开始进入循环的时候两端点没有处理好,错了很多次。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define N 100010
#define INF 0x7ffffff
using namespace std;
struct num
{
int x,y;
}a[N];
int b[N],c[N];
int main()
{
//freopen("data.in","r",stdin);
int n,m;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&c[i]);
}
int l=0,r=INF;
a[1].x = 0;
a[1].y = 1;
if(b[1]!=-1)
{
l = r = b[1];
}
a[2].x = c[1];
a[2].y = -1;
if(b[2]!=-1)
{
l = r = (c[1]-b[2]);
}else
{
r = min(r,c[1]);
}
for(int i=3;i<=n;i++)
{
int x1 = a[i-2].x;
int y1 = a[i-2].y;
int x2 = a[i-1].x;
int y2 = a[i-1].y;
int x = x1+x2;
int y = y1+y2;
a[i].x = c[i-1]-x;
a[i].y = -y;
x = c[i-1]-x;
y = -y;
if(b[i]!=-1&&y!=0)
{
l = r = (b[i]-x)/y;
}else if(y!=0)
{
if(y>0)
{
int ll = ceil((double)(-x)/y);
l = max(l,ll);
}else
{
int rr = floor((double)(-x)/y);
r = min(r,rr);
}
}
}
int x1 = a[n-1].x;
int y1 = a[n-1].y;
int x2 = a[n].x;
int y2 = a[n].y;
int x = x1+x2;
int y = y1+y2;
if(y!=0)
{
l = r = (c[n]-x)/y;
}
scanf("%d",&m);
while(m--)
{
int k;
scanf("%d",&k);
k+=1;
if(a[k].y==0)
{
printf("%d\n",a[k].x);
continue;
}
int res1 = a[k].x + l * a[k].y;
int res2 = a[k].x + r * a[k].y;
int res = max(res1,res2);
printf("%d\n",res);
}
}
return 0;
}