刷题记录(NC19246 数据结构)

本文介绍了如何利用NC19246数据结构处理乘法及计算区间平方和的问题,通过分析区间操作对和与平方和的影响,设计了lazy1和lazy2标记的更新策略,以提高计算效率。涉及的关键点包括区间和的乘法处理、平方和的更新规则以及pushdown操作的实现。
摘要由CSDN通过智能技术生成

NC19246 数据结构

题目链接

关键点:

1、如何处理乘法以及计算平方和

2、首先分析对于区间内每个数+k,对区间平方和的影响:

∑(xi+k)² = ∑xi² + 2*k*∑xi + ∑k²

因此我们只要将该区间的平方和 += 该区间的和*2*k + 该区间长度*k

tree2[p] += 2*tree1[p]*num + num*num*(r-l+1);

对于乘k对于区间和、区间平方和影响

区间和*k = ∑每个元素*k,因此区间和直接乘k即可

区间平方和*k² = ∑(每个元素*k)²,因此区间平方和直接乘k²即可

3、两个标记,lazy1表示加,lazy2表示乘,如何Pushdown

首先对于两个标记是先加还是先乘很难搞清,统一设为(xi+p1)*p2的形式,即lazy1要随着lazy2更新,每次对该区间进行乘操作时,lazy1也跟着*k,

lazy1[p] *= num;
lazy2[p] *=num;

这时进行pushdown:

先更新标记:

    lazy1[p*2] += lazy1[p];
    lazy1[p*2+1] += lazy1[p];
    
    lazy2[p*2] *= lazy2[p];
    lazy2[p*2+1] *= lazy2[p];

∑((xi+p1)*p2)² = ∑xi² * p2² + 2 * p2² * p1 * ∑xi + ∑p2² * p1²

又因为p1即lazy1标记随着lazy2(p2)标记实时更新,因此此时lazy1[p] = p1*p2;

说明:∑xi为区间和

tree2[p*2] *= lazy2[p]*lazy2[p];
tree2[p*2] += 2*tree1[p*2]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(mid-l+1);
tree2[p*2+1] *= lazy2[p]*lazy2[p];
tree2[p*2+1] += 2*tree1[p*2+1]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(r-(mid+1)+1);

同样的分析对于区间和

∑((xi+p1)*p2) = ∑xi * p2 + 2*∑p2 * p1

    tree1[p*2] *= lazy2[p];
    tree1[p*2] += (mid-l+1)*lazy1[p];
    tree1[p*2+1] *= lazy2[p];
    tree1[p*2+1] += (r-(mid+1)+1)*lazy1[p];

注意这里两个区间的更新不可以调换,因为区间平方和要利用区间和,因此区间平方和要先计算,如果调换,那么就会重复计算

完整代码:

# include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 10000+10;
int n, m;
ll a[N];
ll tree1[N*4], tree2[N*4], lazy1[N*4], lazy2[N*4];
void build(int p, int l, int r)
{
    if (l == r)
    {
        tree1[p] = a[l];
        tree2[p] = a[l]*a[l];
        return ;
    }
    int mid = (l+r)/2;
    build(2*p, l, mid);
    build(2*p+1, mid+1, r);
    tree1[p] = tree1[p*2] + tree1[p*2+1];
    tree2[p] = tree2[p*2] + tree2[p*2+1];
}
void pushdown(int p, int l, int r)
{
    lazy1[p*2] += lazy1[p];
    lazy1[p*2+1] += lazy1[p];
    
    lazy2[p*2] *= lazy2[p];
    lazy2[p*2+1] *= lazy2[p];
    
    int mid = (l+r)/2;
    
    tree2[p*2] *= lazy2[p]*lazy2[p];
    tree2[p*2] += 2*tree1[p*2]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(mid-l+1);
    tree2[p*2+1] *= lazy2[p]*lazy2[p];
    tree2[p*2+1] += 2*tree1[p*2+1]*lazy1[p]*lazy2[p] + lazy1[p]*lazy1[p]*(r-(mid+1)+1);
    
    tree1[p*2] *= lazy2[p];
    tree1[p*2] += (mid-l+1)*lazy1[p];
    tree1[p*2+1] *= lazy2[p];
    tree1[p*2+1] += (r-(mid+1)+1)*lazy1[p];

    lazy1[p] = 0;
    lazy2[p] = 1;
}
void change1(int p, int l, int r, int x, int y, ll num)//+num
{
    if (x<=l && y>=r)
    {
        tree2[p] += 2*tree1[p]*num + num*num*(r-l+1);
        tree1[p] += num*(r-l+1);
        lazy1[p] += num;
        return ;
    }
    if (lazy1[p]!=0 || lazy2[p]!=1)
        pushdown(p, l, r);
    int mid = (l+r)/2;
    if (x<=mid) change1(2*p, l, mid, x, y, num);
    if (y>mid) change1(2*p+1, mid+1, r, x, y, num);
    tree1[p] = tree1[p*2] + tree1[p*2+1];
    tree2[p] = tree2[p*2] + tree2[p*2+1];
}
void change2(int p, int l, int r, int x, int y, ll num)//*num
{
    if (x<=l && y>=r)
    {
        tree2[p] *= num*num;
        tree1[p] *= num;//单个影响
        lazy1[p] *= num;
        lazy2[p] *=num;
        return ;
    }
    if (lazy1[p]!=0 || lazy2[p]!=1)
        pushdown(p, l, r);
    int mid = (l+r)/2;
    if (x<=mid) change2(2*p, l, mid, x, y, num);
    if (y>mid) change2(2*p+1, mid+1, r, x, y, num);
    tree1[p] = tree1[p*2] + tree1[p*2+1];
    tree2[p] = tree2[p*2] + tree2[p*2+1];
}
ll cal1(int p, int l, int r, int x, int y)//求和
{
    if (x<=l && y>=r) return tree1[p];
    if (lazy1[p]!=0 || lazy2[p]!=1)
        pushdown(p, l, r);
    int mid = (l+r)/2;
//     ll ans = 0;
//     if (x<=mid) ans += cal1(2*p, l, mid, x, y);
//     if (y>mid) ans += cal1(2*p+1, mid+1, r, x, y);
//     return ans;
    if (y<=mid) return cal1(2*p, l, mid, x, y);
    if (x>mid) return cal1(2*p+1, mid+1, r, x, y);
    return cal1(2*p, l, mid, x, mid) + cal1(2*p+1, mid+1, r, mid+1, y);
}
ll cal2(int p, int l, int r, int x, int y)//求平方和
{
    if (x<=l && y>=r) return tree2[p];
    if (lazy1[p]!=0 || lazy2[p]!=1)
        pushdown(p, l, r);
    int mid = (l+r)/2;
//     ll ans = 0;
//     if (x<=mid) ans += cal2(2*p, l, mid, x, y);
//     if (y>mid) ans += cal2(2*p+1, mid+1, r, x, y);
//     return ans;
    if (y<=mid) return cal2(2*p, l, mid, x, y);
    if (x>mid) return cal2(2*p+1, mid+1, r, x, y);
    return cal2(2*p, l, mid, x, mid) + cal2(2*p+1, mid+1, r, mid+1, y);
}
int main()
{
    for (int i=0; i<N*4; i++)
        lazy2[i] = 1;
    memset(lazy1, 0, sizeof(lazy1));
    cin>>n>>m;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    build(1, 1, n);
    for (int i=1; i<=m; i++)
    {
        int q;
        cin>>q;
        if (q == 1)
        {
            int l, r;
            cin>>l>>r;
            cout<<cal1(1, 1, n, l, r)<<endl;
        }
        if (q == 2)
        {
            int l, r;
            cin>>l>>r;
            cout<<cal2(1, 1, n, l, r)<<endl;
        }
        if (q == 3)
        {
            int l, r;
            ll x;
            cin>>l>>r>>x;
            change2(1, 1, n, l, r, x);
        }
        if (q == 4)
        {
            int l, r;
            ll x;
            cin>>l>>r>>x;
            change1(1, 1, n, l, r, x);
        }
    }
    
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值