HDU4578 Transformation【线段树】

Problem Description
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. 
 

Input
There 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.
 

Output
For 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
 

Source
2013ACM-ICPC杭州赛区全国邀请赛


不要想复杂了,只需要用个flag标记区间是否全部相同就可以了。  注意查询时不是区间相同且flag=1时才计算,而是在区间之内。WA好几次


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define INF 0x1f1f1f1f
#define M 100005
using namespace std;
int Case = 1;

int t, n, m;

struct Node {
    int l, r, mid, f, v;
}l[M<<2];

void build(int a, int b, int r) {
    l[r].l = a, l[r].r = b, l[r].mid = (a+b)>>1;
    l[r].f = 1, l[r].v = 0;
    if(a == b) {
        return ;
    }
    build(a, l[r].mid, r<<1);
    build(l[r].mid+1, b, r<<1|1);
}

int ff, mod = 10007, z;
void solve(int r) {
    if(ff == 1) {
        l[r].v = (l[r].v + z) % mod;
    }
    else if(ff == 2) {
        l[r].v = (l[r].v * z) % mod;
    }
    else if(ff == 3) {
        l[r].v = z % mod;
    }
}

void update(int a, int b, int r) {
    if(l[r].l == l[r].r) {
        solve(r);
        return ;
    }

    if(l[r].l == a && l[r].r == b) {
        if(l[r].f == 1) {
            solve(r);
        }
        else { 
            if(ff == 3) {
                solve(r);
                l[r].f = 1;
            }
            else {
                update(a, l[r].mid, r<<1);
                update(l[r].mid+1, b, r<<1|1);
            }
        }
        return ;
    }

    if(l[r].f == 1) {
        l[r].f = 0;
        l[r<<1].v = l[r].v;
        l[r<<1|1].v = l[r].v;
        l[r<<1].f = l[r<<1|1].f = 1;
    }

    if(a > l[r].mid) {
        update(a, b, r<<1|1);
    }
    else if(b <= l[r].mid) {
        update(a, b, r<<1);
    }
    else {
        update(a, l[r].mid, r<<1);
        update(l[r].mid+1, b, r<<1|1);
    }
}

int query(int a, int b, int r) {
    int res = 0;
    if(l[r].l == l[r].r) {
        int tmp = 1;
        for(int i = 0; i < z; i++) {
            tmp = (tmp * l[r].v) % mod;
        }
        return tmp;
    }

    if(a >= l[r].l && b <= l[r].r) {
        if(l[r].f == 1) {
            int tmp = 1;
            int tot = b-a + 1;
            for(int i = 0; i < z; i++) {
                tmp = (tmp * l[r].v) % mod;
            }
            return (tot*tmp) % mod;
        }
    }

    if(a > l[r].mid) {
        res = query(a, b, r<<1|1);
    }
    else if(b <= l[r].mid) {
        res = query(a, b, r<<1); 
    }
    else {
        res = (query(a, l[r].mid, r<<1) + query(l[r].mid+1, b, r<<1|1)) % mod;
    }
    return res;
}


int main()
{
    while(~scanf("%d%d", &n, &m)) {
        if(n == 0 && m == 0) {
            break;
        }

        build(1, n, 1);

        while(m--) {
            int x, y;
            scanf("%d%d%d%d", &ff, &x, &y, &z);
            if(ff < 4) {
                update(x, y, 1);
            }
            else {
                int ans = query(x, y, 1);
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(数据结构)