HDU1698 Just a Hook 区间改为某数 区间求和 懒标记

Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

HDU1698 Just a Hook 区间改为某数 区间求和 懒标记

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample Input

1
10
2
1 5 2
5 9 3

Sample Output

Case 1: The total value of the hook is 24.

题目大意:
T组数据
每组第一个数N为数的个数,每个数初始为1
Q是操作的个数
每个操作对应X,Y,Z:把X~Y这些数变为Z
求最后每个数的总价值

分析:
区间修改,区间查询,需要加入懒标记

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
using ll = long long;
const int maxn = 100005;
int a[maxn], c[maxn];

struct SegmentTree
{
    int l, r;
    ll sum, add;
}tree[maxn<<2];

void build(int p, int l, int r)
{
    tree[p].l = l;
    tree[p].r = r;
    if (l == r)
    {
        tree[p].sum = 1;
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
}

void spread(int p)
{
    if (tree[p].add)
    {
        tree[p<<1].sum = tree[p].add * (tree[p<<1].r - tree[p<<1].l + 1);
        tree[p<<1|1].sum = tree[p].add * (tree[p<<1|1].r - tree[p<<1|1].l + 1);
        tree[p<<1].add = tree[p].add;
        tree[p<<1|1].add = tree[p].add;
        tree[p].add = 0;
    }
}

void change(int p, int l, int r, int z)
{
    if (l <= tree[p].l && r >= tree[p].r)
    {
        tree[p].sum = (ll)z*(tree[p].r - tree[p].l + 1);
        tree[p].add = z;
        return;
    }
    spread(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (l <= mid) change(p<<1, l, r, z);
    if (r > mid) change (p<<1|1, l, r, z);
    tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
}

ll ask(int p, int l, int r)
{
    if (l <= tree[p].l && r >= tree[p].r)
        return tree[p].sum;
    spread(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    ll ans = 0;
    if (l <= mid) ans += ask(p<<1, l, r);
    if (r > mid) ans += ask(p<<1|1, l, r);
    return ans;
}

int main()
{
    int T;
    scanf("%d", &T);
    for (int casenum = 1; casenum <= T; casenum ++)
    {
        memset(tree, 0, sizeof tree);
        int N, Q;
        scanf("%d%d", &N, &Q);
        build(1, 1, N);
        while (Q --)
        {
            int x, y, z;
            scanf("%d %d %d", &x, &y, &z);
            change(1, x, y, z);
        }
        printf("Case %d: The total value of the hook is %lld.\n", casenum, ask(1, 1, N));
    }
    return 0;
}