AtCoder Beginner Contest 340E - Mancala 2

23 篇文章 0 订阅

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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
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;
}
  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值