bzoj1798: [Ahoi2009]Seq 维护序列seq(线段树)

来源:互联网 发布:mac用什么编写html 编辑:程序博客网 时间:2024/06/09 09:32

题目传送门
线段树真恶心。

解法:
线段树。。
整段修改的题目一般都要用lazy标记。
这道题打两个标记,一个乘标记,一个加标记。
在乘的时候一定要把加标记也乘上。
乘法分配率啊。
唯一恶心的就是标记的下放顺序。
是先加呢还是先乘呢?
肯定是先乘啦。
为什么?
如果先加的话有可能把本来不要乘的东西乘了。先加肯定错。
那为什么先乘呢?
先乘也有可能把本来要乘的东西没有乘了呀。没事的。
因为我们把加标记也乘了一遍,所以不怕漏乘。

代码实现:

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<cmath>#include<queue>using namespace std;typedef long long ll;struct node {    int l,r,lc,rc;ll c,lazyj,lazyc; //lazyj表示加标记,lazyc表示乘标记}tr[1100000];int len;void bt(int l,int r) {    int now=++len;    tr[now].l=l;tr[now].r=r;    tr[now].lc=tr[now].rc=-1;    tr[now].c=tr[now].lazyj=0;tr[now].lazyc=1;    if(l<r) {        int mid=(l+r)/2;        tr[now].lc=len+1;bt(l,mid);        tr[now].rc=len+1;bt(mid+1,r);    }} ll mod;void update(int now) {    int lc=tr[now].lc,rc=tr[now].rc;    if(lc==-1)        return ;    if(tr[now].lazyc!=1) {        tr[lc].lazyc=(tr[lc].lazyc*tr[now].lazyc)%mod;        tr[rc].lazyc=(tr[rc].lazyc*tr[now].lazyc)%mod;        tr[lc].lazyj=(tr[lc].lazyj*tr[now].lazyc)%mod;        tr[rc].lazyj=(tr[rc].lazyj*tr[now].lazyc)%mod;        tr[lc].c=(tr[lc].c*tr[now].lazyc)%mod;        tr[rc].c=(tr[rc].c*tr[now].lazyc)%mod;        tr[now].lazyc=1;    }    if(tr[now].lazyj!=0) {        tr[lc].lazyj=(tr[lc].lazyj+tr[now].lazyj)%mod;        tr[rc].lazyj=(tr[rc].lazyj+tr[now].lazyj)%mod;        tr[lc].c=(tr[lc].c+(tr[lc].r-tr[lc].l+1)*tr[now].lazyj)%mod;        tr[rc].c=(tr[rc].c+(tr[rc].r-tr[rc].l+1)*tr[now].lazyj)%mod;        tr[now].lazyj=0;    }}void change_j(int now,int l,int r,ll k) {    update(now);    if(tr[now].l==l&&tr[now].r==r) {        tr[now].c=(tr[now].c+(r-l+1)*k)%mod;tr[now].lazyj=(tr[now].lazyj+k)%mod;        return ;    }    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;    if(r<=mid)        change_j(lc,l,r,k);    else if(l>mid)        change_j(rc,l,r,k);    else {        change_j(lc,l,mid,k);        change_j(rc,mid+1,r,k);    }    tr[now].c=(tr[lc].c+tr[rc].c)%mod;}void change_c(int now,int l,int r,ll k) {    update(now);    if(tr[now].l==l&&tr[now].r==r) {        tr[now].c=(tr[now].c*k)%mod;        tr[now].lazyc=(tr[now].lazyc*k)%mod;        tr[now].lazyj=(tr[now].lazyj*k)%mod;  //加标记也要乘        return ;    }    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;    if(r<=mid)        change_c(lc,l,r,k);    else if(l>mid)        change_c(rc,l,r,k);    else {        change_c(lc,l,mid,k);        change_c(rc,mid+1,r,k);    }    tr[now].c=(tr[lc].c+tr[rc].c)%mod;}ll find_sum(int now,int l,int r) {    update(now);    if(tr[now].l==l&&tr[now].r==r)        return tr[now].c;    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;    if(r<=mid)        return find_sum(lc,l,r);    else if(l>mid)        return find_sum(rc,l,r);    else        return (find_sum(lc,l,mid)+find_sum(rc,mid+1,r))%mod;}       int main() {    int n;scanf("%d%lld",&n,&mod);    len=0;bt(1,n);    for(int i=1;i<=n;i++) {        ll x;scanf("%lld",&x);        change_j(1,i,i,x);    }    int m;scanf("%d",&m);    for(int i=1;i<=m;i++) {        int t,x,y;ll c;scanf("%d%d%d",&t,&x,&y);        if(t==1) {            scanf("%lld",&c);            change_c(1,x,y,c);        }        else if(t==2) {            scanf("%lld",&c);            change_j(1,x,y,c);        }        else            printf("%lld\n",find_sum(1,x,y));    }    return 0;}
阅读全文
0 0
原创粉丝点击