天天看點

線段樹 I

I - Transformation

Yuanfang is puzzled with the question below: 

There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations. 

Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformation a k<---a k+c, k = x,x+1,…,y. 

Operation 2: Multiply c to each number between a x and a y inclusive. In other words, do transformation a k<---a k×c, k = x,x+1,…,y. 

Operation 3: Change the numbers between a x and a y to c, inclusive. In other words, do transformation a k<---c, k = x,x+1,…,y. 

Operation 4: Get the sum of p power among the numbers between a x and a y inclusive. In other words, get the result of a x p+a x+1 p+…+a y p. 

Yuanfang has no idea of how to do it. So he wants to ask you to help him. 

InputThere are no more than 10 test cases. 

For each case, the first line contains two numbers n and m, meaning that there are n integers and m operations. 1 <= n, m <= 100,000. 

Each the following m lines contains an operation. Operation 1 to 3 is in this format: "1 x y c" or "2 x y c" or "3 x y c". Operation 4 is in this format: "4 x y p". (1 <= x <= y <= n, 1 <= c <= 10,000, 1 <= p <= 3) 

The input ends with 0 0. 

OutputFor each operation 4, output a single integer in one line representing the result. The answer may be quite large. You just need to calculate the remainder of the answer when divided by 10007.Sample Input

5 5
3 3 5 7
1 2 4 4
4 1 5 2
2 2 5 8
4 3 5 3
0 0      

Sample Output

307
7489



題目思路:
這個題目是一個裸的線段樹,有四種操作,
第一種就是區間更新,在每一個數值+c
第二種就是每一個位置*c
第三種就是把每一個位置的數更新成c
第四種求每一個數的c次方的和
前面三種就是普通的線段樹,最後一種因為c比較小,最大隻有三是以就在結構體裡面枚舉三種情況就可以了。
第一種和第二張要設定兩個lazy标志,第三種也要設定,但是如果第三種成立則之前的lazy标志都要删去,一共有了三種lazy标志。

      
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
const int MOD = 10007;
const int MAXN = 100010;
struct Node
{
    int l,r;
    int sum1,sum2,sum3;
    int lazy1,lazy2,lazy3;
}segTree[MAXN*3];
void build(int i,int l,int r)
{
    segTree[i].l = l;
    segTree[i].r = r;
    segTree[i].sum1 = segTree[i].sum2 = segTree[i].sum3 = 0;
    segTree[i].lazy1 = segTree[i].lazy3 = 0;
    segTree[i].lazy2 = 1;
    int mid = (l+r)/2;
    if(l == r)return;
    build(i<<1,l,mid);
    build((i<<1)|1,mid+1,r);
}
void push_up(int i)
{
    if(segTree[i].l == segTree[i].r)
        return;
    segTree[i].sum1 = (segTree[i<<1].sum1 + segTree[(i<<1)|1].sum1)%MOD;
    segTree[i].sum2 = (segTree[i<<1].sum2 + segTree[(i<<1)|1].sum2)%MOD;
    segTree[i].sum3 = (segTree[i<<1].sum3 + segTree[(i<<1)|1].sum3)%MOD;

}

void push_down(int i)
{
    if(segTree[i].l == segTree[i].r) return;
    if(segTree[i].lazy3 != 0)
    {
        segTree[i<<1].lazy3 = segTree[(i<<1)|1].lazy3 = segTree[i].lazy3;
        segTree[i<<1].lazy1 = segTree[(i<<1)|1].lazy1 = 0;
        segTree[i<<1].lazy2 = segTree[(i<<1)|1].lazy2 = 1;
        segTree[i<<1].sum1 = (segTree[i<<1].r - segTree[i<<1].l + 1)*segTree[i<<1].lazy3%MOD;
        segTree[i<<1].sum2 = (segTree[i<<1].r - segTree[i<<1].l + 1)*segTree[i<<1].lazy3%MOD*segTree[i<<1].lazy3%MOD;
        segTree[i<<1].sum3 = (segTree[i<<1].r - segTree[i<<1].l + 1)*segTree[i<<1].lazy3%MOD*segTree[i<<1].lazy3%MOD*segTree[i<<1].lazy3%MOD;
        segTree[(i<<1)|1].sum1 = (segTree[(i<<1)|1].r - segTree[(i<<1)|1].l + 1)*segTree[(i<<1)|1].lazy3%MOD;
        segTree[(i<<1)|1].sum2 = (segTree[(i<<1)|1].r - segTree[(i<<1)|1].l + 1)*segTree[(i<<1)|1].lazy3%MOD*segTree[(i<<1)|1].lazy3%MOD;
        segTree[(i<<1)|1].sum3 = (segTree[(i<<1)|1].r - segTree[(i<<1)|1].l + 1)*segTree[(i<<1)|1].lazy3%MOD*segTree[(i<<1)|1].lazy3%MOD*segTree[(i<<1)|1].lazy3%MOD;
        segTree[i].lazy3 = 0;
    }
    if(segTree[i].lazy1 != 0 || segTree[i].lazy2 != 1)
    {
        segTree[i<<1].lazy1 = ( segTree[i].lazy2*segTree[i<<1].lazy1%MOD + segTree[i].lazy1 )%MOD;
        segTree[i<<1].lazy2 = segTree[i<<1].lazy2*segTree[i].lazy2%MOD;
        int sum1,sum2,sum3;
        sum1 = (segTree[i<<1].sum1*segTree[i].lazy2%MOD + (segTree[i<<1].r - segTree[i<<1].l + 1)*segTree[i].lazy1%MOD)%MOD;
        sum2 = (segTree[i].lazy2 * segTree[i].lazy2 % MOD * segTree[i<<1].sum2 % MOD + 2*segTree[i].lazy1*segTree[i].lazy2%MOD * segTree[i<<1].sum1%MOD + (segTree[i<<1].r - segTree[i<<1].l + 1)*segTree[i].lazy1%MOD*segTree[i].lazy1%MOD)%MOD;
        sum3 = segTree[i].lazy2 * segTree[i].lazy2 % MOD * segTree[i].lazy2 % MOD * segTree[i<<1].sum3 % MOD;
        sum3 = (sum3 + 3*segTree[i].lazy2 % MOD * segTree[i].lazy2 % MOD * segTree[i].lazy1 % MOD * segTree[i<<1].sum2) % MOD;
        sum3 = (sum3 + 3*segTree[i].lazy2 % MOD * segTree[i].lazy1 % MOD * segTree[i].lazy1 % MOD * segTree[i<<1].sum1) % MOD;
        sum3 = (sum3 + (segTree[i<<1].r - segTree[i<<1].l + 1)*segTree[i].lazy1%MOD * segTree[i].lazy1 % MOD * segTree[i].lazy1 % MOD) % MOD;
        segTree[i<<1].sum1 = sum1;
        segTree[i<<1].sum2 = sum2;
        segTree[i<<1].sum3 = sum3;
        segTree[(i<<1)|1].lazy1 = ( segTree[i].lazy2*segTree[(i<<1)|1].lazy1%MOD + segTree[i].lazy1 )%MOD;
        segTree[(i<<1)|1].lazy2 = segTree[(i<<1)|1].lazy2 * segTree[i].lazy2 % MOD;
        sum1 = (segTree[(i<<1)|1].sum1*segTree[i].lazy2%MOD + (segTree[(i<<1)|1].r - segTree[(i<<1)|1].l + 1)*segTree[i].lazy1%MOD)%MOD;
        sum2 = (segTree[i].lazy2 * segTree[i].lazy2 % MOD * segTree[(i<<1)|1].sum2 % MOD + 2*segTree[i].lazy1*segTree[i].lazy2%MOD * segTree[(i<<1)|1].sum1%MOD + (segTree[(i<<1)|1].r - segTree[(i<<1)|1].l + 1)*segTree[i].lazy1%MOD*segTree[i].lazy1%MOD)%MOD;
        sum3 = segTree[i].lazy2 * segTree[i].lazy2 % MOD * segTree[i].lazy2 % MOD * segTree[(i<<1)|1].sum3 % MOD;
        sum3 = (sum3 + 3*segTree[i].lazy2 % MOD * segTree[i].lazy2 % MOD * segTree[i].lazy1 % MOD * segTree[(i<<1)|1].sum2) % MOD;
        sum3 = (sum3 + 3*segTree[i].lazy2 % MOD * segTree[i].lazy1 % MOD * segTree[i].lazy1 % MOD * segTree[(i<<1)|1].sum1) % MOD;
        sum3 = (sum3 + (segTree[(i<<1)|1].r - segTree[(i<<1)|1].l + 1)*segTree[i].lazy1%MOD * segTree[i].lazy1 % MOD * segTree[i].lazy1 % MOD) % MOD;
        segTree[(i<<1)|1].sum1 = sum1;
        segTree[(i<<1)|1].sum2 = sum2;
        segTree[(i<<1)|1].sum3 = sum3;
        segTree[i].lazy1 = 0;
        segTree[i].lazy2 = 1;

    }
}
void update(int i,int l,int r,int type,int c)
{
    if(segTree[i].l == l && segTree[i].r == r)
    {
        c %= MOD;
        if(type == 1)
        {
            segTree[i].lazy1 += c;
            segTree[i].lazy1 %= MOD;
            segTree[i].sum3 = (segTree[i].sum3 + 3*segTree[i].sum2%MOD*c%MOD + 3*segTree[i].sum1%MOD*c%MOD*c%MOD + (segTree[i].r - segTree[i].l + 1)*c%MOD*c%MOD*c%MOD)%MOD;
            segTree[i].sum2 = (segTree[i].sum2 + 2*segTree[i].sum1%MOD*c%MOD + (segTree[i].r - segTree[i].l + 1)*c%MOD*c%MOD)%MOD;
            segTree[i].sum1 = (segTree[i].sum1 + (segTree[i].r - segTree[i].l + 1)*c%MOD)%MOD;
        }
        else if(type == 2)
        {
            segTree[i].lazy1 = segTree[i].lazy1*c%MOD;
            segTree[i].lazy2 = segTree[i].lazy2*c%MOD;
            segTree[i].sum1 = segTree[i].sum1*c%MOD;
            segTree[i].sum2 = segTree[i].sum2*c%MOD*c%MOD;
            segTree[i].sum3 = segTree[i].sum3*c%MOD*c%MOD*c%MOD;
        }
        else
        {
            segTree[i].lazy1 = 0;
            segTree[i].lazy2 = 1;
            segTree[i].lazy3 = c%MOD;
            segTree[i].sum1 = c*(segTree[i].r - segTree[i].l + 1)%MOD;
            segTree[i].sum2 = c*(segTree[i].r - segTree[i].l + 1)%MOD*c%MOD;
            segTree[i].sum3 = c*(segTree[i].r - segTree[i].l + 1)%MOD*c%MOD*c%MOD;
        }
        return;
    }
    push_down(i);
    int mid = (segTree[i].l + segTree[i].r)/2;
    if(r <= mid)update(i<<1,l,r,type,c);
    else if(l > mid)update((i<<1)|1,l,r,type,c);
    else
    {
        update(i<<1,l,mid,type,c);
        update((i<<1)|1,mid+1,r,type,c);
    }
    push_up(i);
}
int query(int i,int l,int r,int p)
{
    if(segTree[i].l == l && segTree[i].r == r)
    {
        if(p == 1)return segTree[i].sum1;
        else if(p== 2)return segTree[i].sum2;
        else return segTree[i].sum3;
    }
    push_down(i);
    int mid = (segTree[i].l + segTree[i].r )/2;
    if(r <= mid)return query(i<<1,l,r,p);
    else if(l > mid)return query((i<<1)|1,l,r,p);
    else return (query(i<<1,l,mid,p)+query((i<<1)|1,mid+1,r,p))%MOD;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    while(scanf("%d%d",&n,&m) == 2)
    {
        if(n == 0 && m == 0)break;
        build(1,1,n);
        int type,x,y,c;
        while(m--)
        {
            scanf("%d%d%d%d",&type,&x,&y,&c);
            if(type == 4)printf("%d
",query(1,x,y,c));
            else update(1,x,y,type,c);
        }
    }
    return 0;
}      

View Code

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <math.h>
#define LL long long
using namespace std;
const LL MAX = 1e6 + 50;
LL INF = 1e8;
LL MOD = 10007;

LL PowerMod(LL a, LL b)
{
    LL ans = 1;
    a = a % MOD;
    while(b > 0) {
        if(b % 2 == 1)
        ans = (ans * a) % MOD;
        b = b / 2;
        a = (a * a) % MOD;
    }
    return ans;
}

LL a[MAX];
LL lazy[MAX << 2][3];


void PushDown(LL rt){
    if(lazy[rt][0] != -1){
        lazy[rt << 1][0] = lazy[rt << 1 | 1][0] = lazy[rt][0] % MOD;
        lazy[rt << 1][1] = lazy[rt << 1 | 1][1] = 1;
        lazy[rt << 1][2] = lazy[rt << 1 | 1][2] = 0;
        lazy[rt][0] = -1;
    }

    if(lazy[rt][1] != 1){
        if(lazy[rt << 1][0] != -1){
            lazy[rt << 1][0] *= lazy[rt][1];
            lazy[rt << 1][0] %= MOD;
        } else{
            PushDown(rt << 1);
            lazy[rt << 1][1] *= lazy[rt][1];
            lazy[rt << 1][1] %= MOD;
        }

        if(lazy[rt << 1 | 1][0] != -1){
            lazy[rt << 1 | 1][0] *= lazy[rt][1];
            lazy[rt << 1 | 1][0] %= MOD;
        } else{
            PushDown(rt << 1 | 1);
            lazy[rt << 1 | 1][1] *= lazy[rt][1];
            lazy[rt << 1 | 1][1] %= MOD;; 
        }
        lazy[rt][1] = 1;
    }

    if(lazy[rt][2] != 0){
        if(lazy[rt << 1][0] != -1){
            lazy[rt << 1][0] += lazy[rt][2];
            lazy[rt << 1][0] %= MOD;
        } else{
            PushDown(rt << 1);
            lazy[rt << 1][2] += lazy[rt][2];
            lazy[rt << 1][2] %= MOD;
        }

        if(lazy[rt << 1 | 1][0] != -1){
            lazy[rt << 1 | 1][0] += lazy[rt][2];
            lazy[rt << 1 | 1][0] %= MOD;
        } else{
            PushDown(rt << 1 | 1);
            lazy[rt << 1 | 1][2] += lazy[rt][2];
            lazy[rt << 1 | 1][2] %= MOD;
        }
        lazy[rt][2] = 0;
    }
}

void Build(LL l, LL r, LL rt){
    lazy[rt][0] = -1;
    lazy[rt][1] = 1;
    lazy[rt][2] = 0;
    if(l == r){
        lazy[rt][0] = 0;
        return ;
    }
    LL m = (l + r) >> 1;
    Build(l, m, rt << 1);
    Build(m + 1, r, rt << 1 | 1);
}


LL L, R, C;
void Update0(LL l, LL r, LL rt){
    if(L <= l && r <= R){
        lazy[rt][0] = C;
        lazy[rt][0] %= MOD;
        lazy[rt][1] = 1;
        lazy[rt][2] = 0;
        return ;
    }
    PushDown(rt);
    LL m = (l + r) >> 1;
    if(L <= m){
        Update0(l, m, rt << 1);
    }
    if(R > m){
        Update0(m + 1, r, rt << 1 | 1);
    }
}

void Update1(LL l, LL r, LL rt){
    if(L <= l && r <= R){
        if(lazy[rt][0] != -1){
            lazy[rt][0] *= C;
            lazy[rt][0] %= MOD;
        } else{
            PushDown(rt);
            lazy[rt][1] *= C;
            lazy[rt][1] %= MOD;
        }
        return ;
    }

    LL m = (l + r) >> 1;
    PushDown(rt);
    if(L <= m){
        Update1(l, m, rt << 1);
    }
    if(R > m){
        Update1(m + 1, r, rt << 1 | 1);
    }
}

void Update2(LL l, LL r, LL rt){
    if(L <= l && r <= R){
        if(lazy[rt][0] != -1){
            lazy[rt][0] += C;
            lazy[rt][0] %= MOD;
        } else{
            PushDown(rt);
            lazy[rt][2] += C;
            lazy[rt][2] %= MOD;
        }
        return ;
    }

    LL m = (l + r) >> 1;
    PushDown(rt);
    if(L <= m){
        Update2(l, m, rt << 1);
    }
    if(R > m){
        Update2(m + 1, r, rt << 1 | 1);
    }
}

LL ans = 0;
void Query(LL l, LL r, LL rt){
    if(L <= l && r <= R && lazy[rt][0] != -1){
        ans = ans + (r - l + 1) * PowerMod(lazy[rt][0], C);
        ans %= MOD;
        return ;
    }

    PushDown(rt);
    LL m = (l + r) >> 1;
    if(L <= m){
        Query(l, m, rt << 1);
    }
    if(R > m){
        Query(m + 1, r, rt << 1 | 1);
    }
}
int main(int argc, char const *argv[])
{
    LL n, m;
    
    while(1){
        scanf("%lld%lld", &n, &m);
        if(n == 0 && m == 0){
            break;
        }
        Build(1, n, 1);
        while(m--){
            LL op;
            scanf("%lld%lld%lld%lld", &op, &L, &R, &C);
            if(op == 1){
                Update2(1, n, 1);
            } else if(op == 2){
                Update1(1, n, 1);
            } else if(op == 3){
                Update0(1, n, 1);
            } else{
                ans = 0;
                Query(1, n, 1);
                printf("%lld
", ans);
            }
        }
    }
    return 0;
}      

View Code