Salesman
Title Description
Amin was a salesman, he was ordered to screw the street to sell their company's products. Screw street is a dead end, the outlet and the inlet are the same, the wall side of the street, the other side being the household. Screw N total households in a street, the i-th tenants from the entrance to the meter is Si. Since the same house can have a number of tenants, so there may be an equal number of households from the entrance. Amin would enter from the entrance, in order to promote their products to the X family households street screws, and then go out the same way.
Amin Every one meter will accumulate 1:00 fatigue value, to promote their products will accumulate fatigue Ai-point value to the i-th household. Amin is a workaholic, he wanted to know, for different X, without unnecessary way to go, and how many points he accumulated fatigue value most.
Input Format
The first line has a positive integer N, the number of households street screws.
The next line of N positive integers, wherein Si represents the i-th integer from households to the i-th entry. Data assurance S_1≤S_2≤ ... ≤S_n <10 ^ 8 next line with N a positive integer, wherein the integer A_i represents the i-th value of the product to promote fatigue will accumulate in the i-th households. Data assurance A_i <1000
Output Format
N output lines, each a positive integer, the i-th row fatigue integer value when X = i, Amin accumulated most.
Segment tree maintenance interval maximum value;,
I-1 before recording user mxl household maximum distance, when the first user distance is less than the maximum value i, then no operation;
if larger than the maximum value, the value of the i mxl- user becomes fatigue value of the household, i + 1 of the n-th user to the user minus the i-th user to the user's distance mxl;
Code:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100010;
const int M=2000100;
const LL mod=2e9;
int n,s[N],a[N];
struct Node{
int l,r,num;
LL mx,lt;
}tr[N*4];
void pp(int k){
if(tr[ls].mx>=tr[rs].mx) tr[k].mx=tr[ls].mx,tr[k].num=tr[ls].num;
else tr[k].mx=tr[rs].mx,tr[k].num=tr[rs].num;
}
void pd(int k){
if(tr[k].lt){
tr[ls].mx-=tr[k].lt;
tr[rs].mx-=tr[k].lt;
tr[ls].lt+=tr[k].lt;
tr[rs].lt+=tr[k].lt;
}
tr[k].lt=0;
}
void build(int l,int r,int k){
tr[k].l=l,tr[k].r=r;
if(l==r){
tr[k].mx=(LL)(2*s[l]+a[l]);
tr[k].num=l;
return;
}
int d=(l+r)>>1;
build(l,d,ls);
build(d+1,r,rs);
pp(k);
}
void update1(int l,int r,int k){
if(tr[k].mx<=0||l>r) return;
if(tr[k].l==tr[k].r){
tr[k].mx=a[tr[k].num];
return;
}
pd(k);
int d=(tr[k].l+tr[k].r)>>1;
if(l<=d) update1(l,r,ls);
if(r>d) update1(l,r,rs);
pp(k);
}
void update2(int l,int r,LL w,int k){
if(tr[k].mx<=0||l>r) return;
if(tr[k].l>=l&&tr[k].r<=r){
tr[k].mx-=w;
tr[k].lt+=w;
return;
}
pd(k);
int d=(tr[k].l+tr[k].r)>>1;
if(l<=d) update2(l,r,w,ls);
if(r>d) update2(l,r,w,rs);
pp(k);
}
void dele(int pos,int k){
if(tr[k].l==tr[k].r){
tr[k].mx=0;
return;
}
pd(k);
int d=(tr[k].l+tr[k].r)>>1;
if(pos<=d) dele(pos,ls);
else dele(pos,rs);
pp(k);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&s[i]);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
build(1,n,1);
LL ans=0;
int mxl=0;
for(int i=1;i<=n;i++){
ans+=tr[1].mx;
int k=tr[1].num;
if(k>mxl){
update1(mxl+1,k-1,1);
update2(k+1,n,2ll*(s[k]-s[mxl]),1);
mxl=k;
}
printf("%d\n",ans);
dele(k,1);
}
return 0;
}