洛谷 P4247 [清华集训]序列操作 线段树

本文介绍了一种处理区间操作和查询的问题,通过维护特定大小的组合来高效地更新区间内的数值,并支持对区间内选择特定数量元素乘积的求和进行查询。针对不同操作提供了详细的算法实现,包括区间加法、区间取反及合并区间。

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

题目描述

有一个长度为 nn 的序列,有三个操作:

I a b c表示将 [a,b] 这一段区间的元素集体增加 cc
R a b表示将 [a,b] 区间内所有元素变成相反数;
Q a b c表示询问 [a,b][a,b] 这一段区间中选择 cc 个数相乘的所有方案的和 mod 19940417 的值。
输入输出格式

输入格式:
第一行两个数 n, qn,q 表示序列长度和操作个数。

第二行 nn 个非负整数,表示序列。

接下来 qq 行每行输入一个操作I a b c或者R a b或者Q a b c,意义如题目描述。

输出格式:
对于每个询问,输出选出 c 个数相乘的所有方案的和 mod 19940417mod 19940417 的值。

输入输出样例

输入样例#1:
5 5
1 2 3 4 5
I 2 3 1
Q 2 4 2
R 1 5
I 1 3 -1
Q 1 5 1
输出样例#1:
40
19940397
说明

样例说明:

做完第一个操作序列变为1 3 4 4 5。

第一次询问结果为 3×4+3×4+4×4=40 。

做完R操作变成-1 -3 -4 -4 -5。

做完I操作变为-2 -4 -5 -4 -5。

第二次询问结果为 −2−4−5−4−5=−20 。

数据范围:

对于100%的数据, n50000,q50000n≤50000,q≤50000 ,初始序列的元素的绝对值 \leq 109≤109 ,保证 [a,b][a,b]是一个合法区间,I操作中  c109 ∣c∣≤109,Q操作中 1cmin(ba+1,20)1≤c≤min(b−a+1,20)

分析:首先因为查询cc很小,所以我们可以维护[0,20]所有的cc
区间加k答案应该怎样修改呢?
对于一个长度为33的序列[a1,a2,a3]会变为[a1+k,a2+k,a3+k][a1+k,a2+k,a3+k]。所以这个序列新的值为,a1a2a3+k(a1a2+a1a3+a2a3)+k2(a1+a2+a3)+k3a1∗a2∗a3+k∗(a1∗a2+a1∗a3+a2∗a3)+k2∗(a1+a2+a3)+k3
观察可以得到,一个序列的新值等于其任意一个子序列的和*kxkx,其中xx为原序列长-子序列长。那对于区间内所有的长度为i序列,每一个长度为j(j<=i)j(j<=i)的序列都对该序列有贡献。假设区间长度为lenlen,区间cc个数相乘结果为f[c],长度为jj的序列可以通过在lenj中选iji−j个数来贡献长度为ii的序列,因为每个长度为j的贡献次数一样,所以直接可以用f[j](lenjij)kijf[j]∗(len−ji−j)∗ki−j来贡献ii。转移方程为,

f[i]=j=0if[j](lenjij)kij

合并区间也就是左边选ii个数,右边选j个数的和相乘,就是新区间i+ji+j个数的和。
区间取反会改变奇数iif[i],暴力处理就可以了。
复杂度为O(202nlogn)O(202∗nlogn)

写完只会疯狂RE,对拍有没错,最后暴力开到50w就过了,我看标都是20w。。。

代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#define LL long long

const int maxn=50007;
const LL mod=19940417;

using namespace std;

struct rec{
    LL f[21];
};

struct tree{
    rec ans;
    LL lazy;
    int rev;
}t[500007];

int a[maxn];
LL c[maxn][21];
LL power[21];
int n,test,x,y,z;
LL k;

rec merge(rec x,rec y)
{
    rec z;
    for (int i=0;i<=20;i++)
    {
        z.f[i]=0;
        for (int j=0;j<=i;j++)
        {
            z.f[i]=(z.f[i]+x.f[j]*y.f[i-j]%mod)%mod;
        }
    }
    return z;
}

void build(int p,int l,int r)
{
    if (l==r)
    {
        t[p].ans.f[0]=1;
        t[p].ans.f[1]=(a[l]%mod+mod)%mod;
        return;
    }
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    t[p].ans=merge(t[p*2].ans,t[p*2+1].ans);
}

void change(int p,int len,LL k)
{
    power[0]=1;
    for (int i=1;i<=20;i++) power[i]=(power[i-1]*k)%mod;
    for (int i=20;i>=0;i--)
    {
        for (int j=0;j<i;j++)
        {
            t[p].ans.f[i]=(t[p].ans.f[i]+t[p].ans.f[j]*power[i-j]%mod*c[len-j][i-j]%mod)%mod;
        }
    }
}

void cleanrev(int p)
{
    for (int i=0;i<=20;i++) if (i%2) t[p].ans.f[i]=(mod-t[p].ans.f[i])%mod;
}

void mark(int p,int l,int r)
{
    int mid=(l+r)/2;
    if (t[p].rev)
    {
        t[p*2].rev^=1;
        t[p*2].lazy=(mod-t[p*2].lazy)%mod;
        cleanrev(p*2);
        t[p*2+1].rev^=1;
        t[p*2+1].lazy=(mod-t[p*2+1].lazy)%mod;
        cleanrev(p*2+1);
        t[p].rev^=1;
    }
    if (t[p].lazy)
    {
        t[p*2].lazy=(t[p*2].lazy+t[p].lazy)%mod;
        t[p*2+1].lazy=(t[p*2+1].lazy+t[p].lazy)%mod;
        change(p*2,mid-l+1,t[p].lazy);
        change(p*2+1,r-mid,t[p].lazy);
        t[p].lazy=0;
    }
}

void ins(int p,int l,int r,int x,int y,LL k)
{
    if ((l==x) && (r==y))
    {
        t[p].lazy=(t[p].lazy+k)%mod;
        change(p,r-l+1,k);
        return;
    }
    int mid=(l+r)/2;
    mark(p,l,r);
    if (y<=mid) ins(p*2,l,mid,x,y,k);
    else if (x>mid) ins(p*2+1,mid+1,r,x,y,k);
    else
    {
        ins(p*2,l,mid,x,mid,k);
        ins(p*2+1,mid+1,r,mid+1,y,k);
    }
    t[p].ans=merge(t[p*2].ans,t[p*2+1].ans);
}

void rep(int p,int l,int r,int x,int y)
{
    if ((l==x) && (r==y))
    {
        t[p].rev^=1;
        cleanrev(p);
        LL now=(mod-t[p].lazy)%mod;
        t[p].lazy=now;
        return;
    }
    int mid=(l+r)/2;
    mark(p,l,r);
    if (y<=mid) rep(p*2,l,mid,x,y);
    else if (x>mid) rep(p*2+1,mid+1,r,x,y);
    else
    {
        rep(p*2,l,mid,x,mid);
        rep(p*2+1,mid+1,r,mid+1,y);
    }
    t[p].ans=merge(t[p*2].ans,t[p*2+1].ans);
}

rec query(int p,int l,int r,int x,int y)
{
    if ((l==x) && (r==y)) return t[p].ans;
    int mid=(l+r)/2;
    mark(p,l,r);
    if (y<=mid) return query(p*2,l,mid,x,y);
    else if (x>mid) return query(p*2+1,mid+1,r,x,y);
    else return merge(query(p*2,l,mid,x,mid),query(p*2+1,mid+1,r,mid+1,y));
}

int main()
{
//  freopen("data.in","r",stdin);
//  freopen("mypout.out","w",stdout);
    scanf("%d%d",&n,&test);
    for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
    c[0][0]=1;
    for (int i=1;i<=n;i++)
    {
        c[i][0]=1;
        for (int j=1;j<=20;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
    }
    build(1,1,n);
    char s[10]; 
    for (int i=1;i<=test;i++)
    {
        scanf("%s",s);
        if (s[0]=='I')
        {                       
            scanf("%d%d%lld",&x,&y,&k);
            k=(k%mod+mod)%mod;          
            ins(1,1,n,x,y,k);
        }
        if (s[0]=='R')
        {
            scanf("%d%d",&x,&y);
            rep(1,1,n,x,y);
        }
        if (s[0]=='Q')
        {           
            scanf("%d%d%d",&x,&y,&z);
            rec d=query(1,1,n,x,y);
            printf("%lld\n",d.f[z]);
        }
    }    
}
``` #include<bits/stdc++.h> #define ll long long using namespace std; const int N=25,M=50005,mod=19940417; ll poww(ll x,int a){ ll ans=1; for(int i=1;i<=a;++i){ ans*=x; ans%=mod; } return ans; } ll c[M][N]; struct info{ int l,r; ll a[N]; info(int l,int r,ll x):l(l),r(r){ a[0]=1; a[1]=x; for(int i=2;i<=20;++i){ a[i]=0; } } info(){ } int len(){ return r-l+1; } friend info operator+(info x,info y){ info ans(x.l,y.r,0); for(int i=1;i<=20;++i){ for(int j=0;j<=i;++j){ ans.a[i]+=x.a[j]*y.a[i-j]; ans.a[i]%=mod; } } return ans; } }; struct tag{ ll jia; bool fan; tag(ll j=0,ll f=0){ jia=j; fan=f; } friend info operator+(info x,tag y){ info ans(x.l,x.r,0); for(int i=1;i<=20;++i){ for(int j=0;j<=i;++j){ ans.a[i]+=poww(y.jia,i-j)*x.a[j]*c[x.len()-j][i-j]; ans.a[i]%=mod; } } if(y.fan==1){ y.fan=0; for(int i=1;i<=20;i+=2){ ans.a[i]*=-1; } } for(int i=1;i<=20;i+=2){ ans.a[i]=(ans.a[i]+mod)%mod; } return ans; } friend tag operator+(tag x,tag y){ return tag((x.jia*(y.fan?-1:1)+y.jia+mod)%mod,x.fan^y.fan); } }; struct Tree__{ #define cl ((x)*2) #define cr ((x)*2+1) vector<info>a; vector<tag>b; void push_down(int x){ a[cl]=a[cl]+b[x]; b[cl]=b[cl]+b[x]; a[cr]=a[cr]+b[x]; b[cr]=b[cr]+b[x]; b[x]=tag(); } void push_up(int x){ a[x]=a[cl]+a[cr]; } void init(int x,int l,int r,info w[]){ if(l==r){ a[x]=w[l]; return; } int mid=(l+r)/2; init(cl,l,mid,w); init(cr,mid+1,r,w); push_up(x); } Tree__(int l,int r,info w[]):a(r*20),b(r*20){ init(1,l,r,w); } void update(int x,int l,int r,tag w){ if((l<=a[x].l)&&(a[x].r<=r)){ a[x]=a[x]+w; b[x]=b[x]+w; return; } push_down(x); if(a[cl].r>=l)update(cl,l,r,w); if(a[cr].l<=r)update(cr,l,r,w); push_up(x); } info query(int x,int l,int r){ if((l<=a[x].l)&&(a[x].r<=r))return a[x]; push_down(x); if(a[cl].r<l)return query(cr,l,r); if(a[cr].l>r)return query(cl,l,r); return query(cl,l,r)+query(cr,l,r); } }; info a[N]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; for(int i=1;i<=n;++i){ ll x; cin>>x; a[i]=info(i,i,x); } for(int i=0;i<M;++i){ c[i][0]=1; for(int j=1;j<N;++j){ if(i==0){ c[i][j]=0; }else{ c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod; } } } Tree__ tr(1,n,a); while(m--){ char op; cin>>op; if(op=='I'){ int l,r,x; cin>>l>>r>>x; tr.update(1,l,r,tag(x,0)); }else if(op=='R'){ int l,r; cin>>l>>r; tr.update(1,l,r,tag(0,1)); }else{ int l,r,x; cin>>l>>r>>x; cout<<tr.query(1,l,r).a[x]<<endl; } } return 0; }```debug RE # P4247 [清华集训 2012] 序列操作 ## 题目背景 **滥用评测功能将被封号。** ## 题目描述 有一个长度为 $n$ 的序列,有三个操作: 1. `I a b c` 表示将 $[a,b]$ 这一段区间的元素集体增加 $c$; 2. `R a b`表示将 $[a,b]$ 区间内所有元素变成相反数; 3. `Q a b c` 表示询问 $[a,b]$ 这一段区间中选择 $c$ 个数相乘的所有方案的和 $\mod 19940417$ 的值。 ## 输入格式 第一行两个数 $n, q$ 表示序列长度和操作个数。 第二行 $n$ 个整数,表示序列。 接下来 $q$ 行每行输入一个操作 `I a b c` 或者 `R a b` 或者 `Q a b c`,意义如题目描述。 ## 输出格式 对于每个询问,输出选出 $c$ 个数相乘的所有方案的和 $\mod 19940417$ 的值。 ## 输入输出样例 #1 ### 输入 #1 ``` 5 5 1 2 3 4 5 I 2 3 1 Q 2 4 2 R 1 5 I 1 3 -1 Q 1 5 1 ``` ### 输出 #1 ``` 40 19940397 ``` ## 说明/提示 **样例说明:** 做完第一个操作序列变为 `1 3 4 4 5`。 第一次询问结果为 $3 \times 4+3 \times 4+4 \times 4=40$。 做完 `R` 操作变成 `-1 -3 -4 -4 -5`。 做完 `I` 操作变为 `-2 -4 -5 -4 -5`。 第二次询问结果为 $-2-4-5-4-5=-20$。 **数据范围:** 对于 $100\%$ 的数据,$n \leq 50000, q \leq 50000$。初始序列的元素的绝对值 $\leq 10^9$,保证 $[a,b]$ 是一个合法区间,`I` 操作中 $|c| \leq 10^9$,`Q` 操作中 $1 \leq c \leq \min(b-a+1,20)$。
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值