CFdiv1+2-Let Them Slide-(线段树)

E

题意:
就是给你n行m列的表格,每行有一个滑块,然后对于每一列,求这一列的总和最大。对每一列单独去看,滑块可以任意滑动,没有滑块的位置就是+0,有滑块的位置就加滑块这个位置的数。

思考:
刚开始看到n和m的范围都是1e6,如果每次光累加这样都超时了。所以肯定有什么方式去优化,首先对于每个滑块,看看它到底有多长,如果长度*2<=m,就代表这个m列中间的一段地方可以对应滑块的任意一点,所以这些都加上滑块的最大值就可以了。对于两边的都暴力更新就行了,这里复杂度是2×l。然后对于l×2>m的暴力就行了,因为这样的l的总和<=1e6,所以这样的最多1e6个,所以复杂度也是2×l。再加上两个线段树一个维护最大值一个维护答案,复杂度2×l×log(l)×log(l)。不过还有一些细节,在代码中注释了。
这里就用到了一个思想,就是小范围暴力,大范围算法。

代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define ll long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define node_l node<<1
#define node_r node<<1|1

using namespace std;
const int mod = 1e9+7,inf = 1e9;
const int N = 1e6+10,M = 2010;

int T,n,m,k;
int va[N];

struct seg_max{
	struct node{
		int L,R;
		int maxn;
		int laz;
	}t[4*N];
	void pushup(int node)
	{
		t[node].maxn = max(t[node_l].maxn,t[node_r].maxn);
	}
	void pushdown(int node)
	{
		int laz = t[node].laz;
		if(laz!=-inf)
		{
			t[node_l].laz = max(t[node_l].laz,laz);
			t[node_l].maxn = max(t[node_l].maxn,laz);
			t[node_r].laz = max(t[node_r].laz,laz);
			t[node_r].maxn = max(t[node_r].maxn,laz);
			t[node].laz = -inf;
		}
	}
	void build(int node,int l,int r)
	{
		t[node].L = l,t[node].R = r;
		t[node].maxn = -inf;t[node].laz = -inf;
		if(l==r) return ;
		int mid = (l+r)>>1;
		build(node_l,l,mid);build(node_r,mid+1,r);
		pushup(node);
	}
	void update(int node,int l,int r,int value)
	{
		if(t[node].L>=l&&t[node].R<=r)
		{
			t[node].maxn = max(t[node].maxn,value);
			t[node].laz = max(t[node].laz,value);
			return ;
		}
		pushdown(node);
		int mid = (t[node].L+t[node].R)>>1;
		if(r<=mid) update(node_l,l,r,value);
		else if(l>mid) update(node_r,l,r,value);
		else update(node_l,l,mid,value),update(node_r,mid+1,r,value);
		pushup(node);
	}
	int query(int node,int l,int r)
	{
		if(t[node].L>=l&&t[node].R<=r) return t[node].maxn;
		pushdown(node);
		int mid = (t[node].L+t[node].R)>>1;
		if(r<=mid) return query(node_l,l,r);
		else if(l>mid) return query(node_r,l,r);
		else return max(query(node_l,l,mid),query(node_r,mid+1,r));
		pushup(node);
	}
}t_max;

struct seg_sum{
	struct node{
		int L,R;
		ll sum;
		ll laz;
	}t[4*N];
	void pushup(int node)
	{
		t[node].sum = t[node_l].sum+t[node_r].sum;
	}
	void pushdown(int node)
	{
		ll laz = t[node].laz;
		if(laz)
		{
			t[node_l].laz += laz;
			t[node_l].sum += (t[node_l].R-t[node_l].L+1)*laz;
			t[node_r].laz += laz;
			t[node_r].sum += (t[node_r].R-t[node_r].L+1)*laz;
			t[node].laz = 0;
		}
	}
	void build(int node,int l,int r)
	{
		t[node].L = l,t[node].R = r;
		if(l==r)
		{
			return ;
		}
		int mid = (l+r)>>1;
		build(node_l,l,mid);build(node_r,mid+1,r);
		pushup(node);
	}
	void update(int node,int l,int r,int value)
	{
		if(t[node].L>=l&&t[node].R<=r)
		{
			t[node].sum += (t[node].R-t[node].L+1)*value;
			t[node].laz += value;
			return ;
		}
		pushdown(node);
		int mid = (t[node].L+t[node].R)>>1;
		if(r<=mid) update(node_l,l,r,value);
		else if(l>mid) update(node_r,l,r,value);
		else update(node_l,l,mid,value),update(node_r,mid+1,r,value);
		pushup(node);
	}
	ll query(int node,int x)
	{
		if(t[node].L==x&&t[node].R==x) return t[node].sum;
		pushdown(node);
		int mid = (t[node].L+t[node].R)>>1;
		if(x<=mid) return query(node_l,x);
		else return query(node_r,x);
		pushup(node);
	}
}t_sum;

signed main()
{
	IOS;
	cin>>n>>m;
	t_sum.build(1,1,m);
	while(n--)
	{
		cin>>k;
		t_max.build(1,1,k); //这里,建树只到k就可以了。这样一共才log(1e6),否则是1e6*log(1e6)。
		for(int i=1;i<=k;i++)
		{
			int x;
			cin>>x;
			t_max.update(1,i,i,x);
		}
		int l = k+1,r = m-k;
		if(l<=r)
		{
			t_sum.update(1,l,r,max(0,t_max.query(1,1,k))); //大范围update一段区间
			for(int i=1;i<l;i++) //小范围暴力
			t_sum.update(1,i,i,max(0,t_max.query(1,1,i)));
			for(int i=r+1;i<=m;i++)
			t_sum.update(1,i,i,max(0,t_max.query(1,k-m+i,k)));
		}
		else //这种情况最多会跑m次,也就是mlog(m)的
		{
			for(int i=1;i<=k;i++)
			{
				int canl = i-(m-k),canr = i;
				int maxn = canl<=0?0:-inf;
				maxn = max(maxn,t_max.query(1,max(1,canl),canr));
				t_sum.update(1,i,i,maxn);
			}
			for(int i=k+1;i<=m;i++)
			{
				int canl = k-(m-i),canr = k;
				int maxn = 0;
				maxn = max(maxn,t_max.query(1,max(1,canl),canr));
				t_sum.update(1,i,i,maxn);
			}
		}
	}
	for(int i=1;i<=m;i++) cout<<t_sum.query(1,i)<<" ";
	return 0;
}

总结:
多多思考,算好复杂度,要多想想那种小范围暴力,大范围算法的思想。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值