problem link
Unravelling the modular arithmetic operations, the problem is quite obviously a data structure problem, with segment modifications can single point queries. These operations can easily be maintainted by a basic segment tree.
Firstly, in each operation, we perform point query to box B i B_i Bi to demtermine the box in hand (make sure to empty the B i B_i Bith element after query)
Since the number of balls in each box can be 1000 times bigger than n n n, all n n n boxes will be interated for ⌊ a i n ⌋ \lfloor \frac{a_i}{n}\rfloor ⌊nai⌋ times in this case. Therefore, for each of the m m m operation, first perform a global adding process (add ⌊ a i n ⌋ \lfloor \frac{a_i}{n}\rfloor ⌊nai⌋ to all n n n boxes). Then, the remaining number of balls in hand would be less than n n n.
The remaining operations would be a simple caculation of endpoint boxes and two segment tree adding operation.
// long long is needed for A_i related variables
// for clarity purposes, no long longs in this code
#include
#include
#include
#include
#include
#include
using namespace std;
const int Maxn=4e5+10;
int add[Maxn<<1],sum[Maxn<<1];
int a[Maxn];
int n,m;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
inline void push_up(int k)
{
sum[k]=sum[k<<1]+sum[k<<1|1];
}
inline void upd(int k,int l,int r,int v)
{
add[k]+=v,sum[k]+=(r-l+1)*v;
}
inline void push_down(int k,int l,int r)
{
if(!add[k] || l==r)return;
int mid=(l+r)>>1;
upd(k<<1,l,mid,add[k]);
upd(k<<1|1,mid+1,r,add[k]);
add[k]=0;
}
void modify(int k,int l,int r,int x,int y,int v)
{
if(x>y)return;
if(x<=l && r<=y)return upd(k,l,r,v);
push_down(k,l,r);
int mid=(l+r)>>1;
if(x<=mid)modify(k<<1,l,mid,x,y,v);
if(mid<y)modify(k<<1|1,mid+1,r,x,y,v);
push_up(k);
}
int query(int k,int l,int r,int pos)
{
if(l==r)return sum[k];
push_down(k,l,r);
int mid=(l+r)>>1;
if(pos<=mid)return query(k<<1,l,mid,pos);
else return query(k<<1|1,mid+1,r,pos);
}
void build(int k,int l,int r)
{
if(l==r){sum[k]=a[l];return;}
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
push_up(k);
}
int main()
{
// freopen("in.txt","r",stdin);
n=read(),m=read();
for(int i=1;i<=n;++i)
a[i]=read();
build(1,1,n);
while(m--)
{
int x=read()+1;
int k=query(1,1,n,x);
modify(1,1,n,x,x,-k);
if(k>=n)modify(1,1,n,1,n,k/n),k%=n;
if(k<=n-x)modify(1,1,n,x+1,x+k,1);
else
{
k-=(n-x);
modify(1,1,n,x+1,n,1);
modify(1,1,n,1,k,1);
}
}
for(int i=1;i<=n;++i)
printf("%lld ",query(1,1,n,i));
putchar('\n');
return 0;
}