Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) E. Let Them Slide(数据结构+差分)...

本文探讨了一种在一维数组上进行差分运算并结合区间最大值查询的数据结构算法,通过实例讲解了如何在给定的限制条件下求解每一列的最大值,适用于动态数组操作与查询优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 题意:问你有n个长度总和为n的数组 你可以移动数组 但不能移出长度为w的矩形框 问你每一列的最大值是多少?

思路:只有一次询问 我们可以考虑差分来解决 然后对于每一行数组 我们可以用数据结构维护一下区间最大值

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int N = 1e6+7;
typedef long long ll;
const ll mod = 998244353;
using namespace std;
ll a[N],ans[N],sum[N];
ll st[N][20];
void init(int len){
    for(int i=1;i<=len;i++) st[i][0]=a[i];
    int m=log2(len)+1;
    for(int j=1;j<m;j++)
        for(int i=1;i<=(len-(1<<j)+1);i++)
            st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
ll ask(int l,int r){
    int k=log2(r-l+1);
    return max(st[l][k],st[r-(1<<k)+1][k]);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n,w; cin>>n>>w;
    for(int i=1;i<=n;i++){
        int l; cin>>l;
        for(int j=1;j<=l;j++)
            cin>>a[j];
        init(l);
        for(int j=1;j<=l;j++){
            ll x;
            if(j-(w-l)<1){
                x=max(ask(1,j),0ll);
                ans[j]+=x;
                ans[j+1]-=x;
            }else{
                x=ask(j-(w-l),j);
                ans[j]+=x;
                ans[j+1]-=x;    
            }
        }
        for(int j=w;j>max(l,w-l);j--){
            ll x;
            int po=l-(w-j);
            if(po+(w-l)>l){
                x=max(ask(po,l),0ll);
                ans[j]+=x;
                ans[j+1]-=x;
            }else{
                x=ask(po,po+(w-l));
                ans[j]+=x;
                ans[j+1]-=x;
            }
        }
        if(2*l<w){
            ll x=max(0ll,ask(1,l));
            ans[l+1]+=x;
            ans[w-l+1]-=x;
        }
    }
    for(int i=1;i<=w;i++){
        sum[i]=sum[i-1]+ans[i];
        cout<<sum[i]<<" ";
    }
    cout<<endl;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/wmj6/p/11574826.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值