1. 程式人生 > >hdu4578線段樹維護平方和,立方和(加,乘,賦值)

hdu4578線段樹維護平方和,立方和(加,乘,賦值)

#include<bits/stdc++.h>
#define ll  unsigned long long
using namespace std;
const ll INFINITE = INT_MAX;
const ll MAXNUM = 100005*4;
ll mod=10007;
struct SegTreeNode
{
    ll p1,p2,p3;
    ll l,r;
    ll addMark;//加數標記
    ll mulMark;//乘數標記
    ll assignMark;//賦值標記
} segTree[MAXNUM];

void pushup(ll root)
{
    segTree[root].p1 = (segTree[root*2].p1 + segTree[root*2+1].p1)%mod;
    segTree[root].p2 = (segTree[root*2].p2 + segTree[root*2+1].p2)%mod;
    segTree[root].p3 = (segTree[root*2].p3 + segTree[root*2+1].p3)%mod;
}

void build(ll root, ll arr[], ll L, ll R)
{
    segTree[root].addMark = 0;
    segTree[root].mulMark = 1;
    segTree[root].assignMark = 0;
    segTree[root].l=L;
    segTree[root].r=R;
    if(L == R)//葉子節點
    {
        segTree[root].p1 = segTree[root].p2=segTree[root].p3=0;
    }
    else
    {
        ll mid = (L + R) / 2;
        build(root*2, arr, L, mid);
        build(root*2+1, arr, mid+1, R);
        pushup(root);
    }
}

void pushDown(ll root)
{
    SegTreeNode &rt=segTree[root];
    SegTreeNode &lson=segTree[root*2];
    SegTreeNode &rson=segTree[root*2+1];
    ll len1=(lson.r-lson.l+1)%mod;
    ll len2=(rson.r-rson.l+1)%mod;
    if(rt.assignMark)
    {
        rt.assignMark%=mod;
        lson.assignMark =rson.assignMark= rt.assignMark%mod;
        lson.addMark=rson.addMark=0;
        lson.mulMark=rson.mulMark=1;
        lson.p1=(len1*rt.assignMark)%mod;
        lson.p2=((len1*rt.assignMark)%mod*rt.assignMark)%mod;
        lson.p3=(((len1*rt.assignMark)%mod*rt.assignMark)%mod*rt.assignMark)%mod;
        rson.p1=(len2*rt.assignMark)%mod;
        rson.p2=((len2*rt.assignMark)%mod*rt.assignMark)%mod;
        rson.p3=(((len2*rt.assignMark)%mod*rt.assignMark)%mod*rt.assignMark)%mod;
        rt.assignMark = 0;
    }
    if(rt.mulMark != 1)
    {
        rt.mulMark%=mod;
        lson.mulMark = (lson.mulMark*rt.mulMark)%mod;
        rson.mulMark = (rson.mulMark*rt.mulMark)%mod;
        if(lson.addMark)lson.addMark=(lson.addMark*rt.mulMark)%mod;
         if(rson.addMark)rson.addMark=(rson.addMark*rt.mulMark)%mod;
        lson.p1=(lson.p1*rt.mulMark)%mod;
        lson.p2=((lson.p2*rt.mulMark)%mod*rt.mulMark)%mod;
        lson.p3=(lson.p3*(((rt.mulMark*rt.mulMark)%mod*rt.mulMark)%mod))%mod;
        rson.p1=(rson.p1*rt.mulMark)%mod;
        rson.p2=((rson.p2*rt.mulMark)%mod*rt.mulMark)%mod;
        rson.p3=(rson.p3*(((rt.mulMark*rt.mulMark)%mod*rt.mulMark)%mod))%mod;
        rt.mulMark = 1;
    }
    if(rt.addMark)
    {
        rt.addMark%=mod;
        lson.addMark = (lson.addMark+rt.addMark)%mod;
        rson.addMark = (rson.addMark+rt.addMark)%mod;
        lson.p3 = ((lson.p3+(((len1*rt.addMark)%mod*rt.addMark)%mod*rt.addMark)%mod)%mod+(((3*rt.addMark)%mod)*(((lson.p1*rt.addMark)%mod+lson.p2)%mod))%mod)%mod;
        lson.p2 = ((lson.p2+ ((len1*rt.addMark)%mod*rt.addMark)%mod)%mod+((2*rt.addMark)%mod*lson.p1)%mod)%mod;
        lson.p1 =( lson.p1+ (len1*rt.addMark)%mod)%mod;
        rson.p3 = ((rson.p3+(((len2*rt.addMark)%mod*rt.addMark)%mod*rt.addMark)%mod)%mod+(((3*rt.addMark)%mod)*(((rson.p1*rt.addMark)%mod+rson.p2)%mod))%mod)%mod;
        rson.p2 = ((rson.p2+ ((len2*rt.addMark)%mod*rt.addMark)%mod)%mod+((2*rt.addMark)%mod*rson.p1)%mod)%mod;
        rson.p1 =(rson.p1+ (len2*rt.addMark)%mod)%mod;
        rt.addMark = 0;
    }
}

ll query(ll rt,ll L, ll R,ll p)
{
    if(L == segTree[rt].l&& segTree[rt].r==R)
    {
        if(p==1)return segTree[rt].p1%mod;
        if(p==2)return segTree[rt].p2%mod;
        if(p==3)return segTree[rt].p3%mod;
    }
    pushDown(rt); //----延遲標誌域向下傳遞
    ll mid = (segTree[rt].l+segTree[rt].r)/2;
    if(R <= mid) return query(rt*2,L, R,p);
    else if(L > mid)  return query(rt*2+1,L, R,p);
    else return (query(rt*2,L,mid,p)+query(rt*2+1,mid+1, R,p))%mod;
}

void update(ll rt, ll L, ll R, ll c,ll p)
{
    if(L == segTree[rt].l&& segTree[rt].r==R)
    {
        SegTreeNode &root=segTree[rt];
        SegTreeNode &lson=segTree[rt*2];
        SegTreeNode &rson=segTree[rt*2+1];
        ll len=(root.r-root.l+1)%mod;
        if(p==1)
        {
            root.addMark = (root.addMark+c)%mod;
            root.p3 =((root.p3+(((len*c)%mod*c)%mod*c)%mod)%mod+(((3*c)%mod)*(((root.p1*c)%mod+root.p2)%mod))%mod)%mod;
            root.p2 =((root.p2+((len*c)%mod*c)%mod)%mod+((2*root.p1)%mod*c)%mod)%mod;
            root.p1 =(root.p1+ (len*c)%mod)%mod;
        }
        else if(p==2)
        {
            root.mulMark = (root.mulMark*c)%mod;
            if(root.addMark)root.addMark=(root.addMark*c)%mod;
            root.p1 = (root.p1*c)%mod;
            root.p2 = ((root.p2*c)%mod*c)%mod;
            root.p3 = (((root.p3*c)%mod*c)%mod*c)%mod;
        }
        else if(p==3)
        {
            root.addMark = 0;
            root.mulMark = 1;
            root.assignMark=c;
            root.p1 = (len*c)%mod;
            root.p2 = ((len*c)%mod*c)%mod;
            root.p3 = (((len*c)%mod*c)%mod*c)%mod;
        }
        return ;
    }
    pushDown(rt); //延遲標記向下傳遞
    ll mid = (segTree[rt].l+segTree[rt].r)/2;
    if(R <= mid) update(rt*2,L, R, c,p);
    else if(L > mid)  update(rt*2+1,L, R, c,p);
    else update(rt<<1, L, mid, c, p),update(rt*2+1,mid+1, R, c,p);
    pushup(rt);
}
int main()
{
//    freopen("C://Users//13553//Desktop//in.txt","r",stdin);
//    freopen("C://Users//13553//Desktop//out.txt","w",stdout);
    ll n,m;
    while(scanf("%llu%llu",&n,&m)&&n+m)
    {
        build(1,NULL,1,n);
        while(m--)
        {
            ll op,x,y,p;
            scanf("%llu%llu%llu%llu",&op,&x,&y,&p);
            if(op==4)
            {
                ll ans=query(1,x,y,p);
                printf("%llu\n",ans);
            }
            else
                update(1,x,y,p,op);
        }
    }
    return 0;
}