YbtOJ NOIP2020 模拟赛 B 组 Day4 D. 筹备计划【线段树】【树状数组】


题目:

题目传送门


题意:

我们有如下操作:
在这里插入图片描述
在每次操作后都需要我们选出一个在集合 S S S中的一个位置,使得 ∑ k = 1 n a k ∗ ∣ i − k ∣ \sum_{k=1}^na_k*|i-k| k=1nakik最小


分析:

如果不存在 S S S的限制,我们所求的就是一个权值中位数
再回到这道题上,我们对于区间操作,喜闻乐见的是用线段树来维护这四种操作
当我们求答案时,也是选择权值中位数,但可能 t a ta ta是不在 S S S当中的,那我们就找左边离 t a ta ta最近的一个位置,以及右边离 t a ta ta最近的一个位置,计算两者的答案,比较
当我们计算答案的时候可以用到树状数组来求和


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#define LL long long
#define lowbit(x) (x&-x)
using namespace std;
inline LL read()
{
    
    
	LL s=0,f=1; char c=getchar();
	while(c<'0'||c>'9') {
    
    if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') {
    
    s=s*10+c-'0';c=getchar();}
	return s*f;
}
LL n,q;
struct Tree{
    
    
	LL s,w,lazy,l,r;
}t[800005];
void build(LL k,LL l,LL r)
{
    
    
	t[k].l=l;t[k].r=r;
	if(l==r) return;
	LL mid=(l+r)>>1;
	build(k*2,l,mid);build(k*2+1,mid+1,r);
	return;
}
void pushdown(LL k)
{
    
    
	if(!t[k].lazy) return;
	LL mid=(t[k].l+t[k].r)>>1;
	t[k*2].lazy=t[k*2+1].lazy=t[k].lazy--;
	t[k*2].s=(mid-t[k].l+1)*t[k].lazy;
	t[k*2+1].s=(t[k].r-mid)*t[k].lazy;
	t[k].lazy=0;
	return;
}
void cover(LL k,LL l,LL r,LL w)
{
    
    
	if(t[k].l>=l&&t[k].r<=r) {
    
    t[k].lazy=w+1;t[k].s=(t[k].r-t[k].l+1)*w;return;}
	pushdown(k);
	LL mid=(t[k].l+t[k].r)>>1;
	if(r<=mid) cover(k*2,l,r,w);
	else if(l>mid) cover(k*2+1,l,r,w);
	else cover(k*2,l,r,w),cover(k*2+1,l,r,w);
	t[k].s=t[k*2+1].s+t[k*2].s;
	return;
}
void change(LL k,LL p,LL w)
{
    
    
	if(t[k].l==t[k].r) {
    
    t[k].w+=w;return;}
	pushdown(k);
	LL mid=(t[k].l+t[k].r)>>1;
	if(p<=mid) change(k*2,p,w);
	else change(k*2+1,p,w);
	t[k].w=t[k*2].w+t[k*2+1].w;
	return;
}
LL find(LL k,LL w)
{
    
    
	if(t[k].l==t[k].r) return t[k].s;
	pushdown(k);
	LL mid=(t[k].l+t[k].r)>>1;
	if(t[k*2].w>=w) return find(k*2,w);
	else return find(k*2+1,w-t[k*2].w)+t[k*2].s;
}
LL ask(LL k,LL s)
{
    
    
	if(t[k].l==t[k].r) return t[k].l;
	pushdown(k);
	LL mid=(t[k].l+t[k].r)>>1;
	if(t[k*2].s>=s) return ask(k*2,s);
	else return ask(k*2+1,s-t[k*2].s);
}
struct bit{
    
    
	LL t[200005],w[200005];
	void change(LL x,LL v)
	{
    
    
		LL k=n-x+1;
		while(x<=n)
		{
    
    
			t[x]+=v*k;
			w[x]+=v;
			x+=lowbit(x);
		}
		return;
	}
	LL ask(LL x)
	{
    
    
		LL ans=0,k=n-x+1;
		while(x)
		{
    
    
			ans+=t[x]-w[x]*k;
			x-=lowbit(x);
		}
		return ans;
	}
}tl,tr;
void updata(LL i,LL x)
{
    
    
	tl.change(i,x);
	tr.change(n-i+1,x);
	return;
}
LL check(LL x) {
    
    return tl.ask(x)+tr.ask(n-x+1);}
int main()
{
    
    
	freopen("position.in","r",stdin);
	freopen("position.out","w",stdout);
	n=read(),q=read();
	build(1,0,n+1);
	for(LL i=1;i<=n;i++) 
	{
    
    
		LL x=read();
		change(1,i,x);
		updata(i,x);
	}
	cover(1,0,n+1,1);
	while(q--)
	{
    
    
		LL type=read(),x=read(),y=read();
		if(type==1) {
    
    change(1,x,y);updata(x,y);}
		if(type==2) {
    
    change(1,x,-y);updata(x,-y);}
		if(type==3) cover(1,x,y,1);
		if(type==4) cover(1,x,y,0);
		LL s=find(1,(t[1].w+1)>>1);
		LL a=ask(1,s),b=ask(1,s+1);
		if(!a&&b==n+1) {
    
    printf("-1\n");continue;}
		if(!a) {
    
    printf("%d\n",b);continue;}
		if(b==n+1) {
    
    printf("%lld\n",a);continue;}
		if(check(a)<=check(b)) printf("%lld\n",a);
		else printf("%lld\n",b);
	}
 	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35786326/article/details/109035923