hdu 1698 屠夫(线段树成段替换)

题意:

屠夫有钱了,要把他的钩子换成银的或者金的。


解析:

线段树成段替换。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int sum[maxn << 2];
int lazy[maxn << 2];

void pushup(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void pushdown(int rt, int mi)
{
    if (lazy[rt])
    {
        lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt];
        sum[rt << 1] = (mi - (mi >> 1)) * lazy[rt];
        sum[rt << 1 | 1] = (mi >> 1) * lazy[rt];
        lazy[rt] = 0;
    }
}

void build(int lo, int hi, int rt)
{
    lazy[rt] = 0;
    sum[rt] = 1;
    if (lo == hi)
        return;
    int mi = (lo + hi) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int L, int R, int num, int lo, int hi, int rt)
{
    if (L <= lo && hi <= R)
    {
        lazy[rt] = num;
        sum[rt] = num * (hi - lo + 1);
        return;
    }
    pushdown(rt, hi - lo + 1);
    int mi = (lo + hi) >> 1;
    if (L <= mi)
        update(L, R, num, lson);
    if (mi < R)
        update(L, R, num, rson);
    pushup(rt);
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int ncase;
    scanf("%d", &ncase);
    int ca = 1;
    while (ncase--)
    {
        int n, m;
        scanf("%d", &n);
        build(1, n, 1);
        scanf("%d", &m);
        while (m--)
        {
            int fr, to, num;
            scanf("%d%d%d", &fr, &to, &num);
            update(fr, to, num, 1, n, 1);
        }
        printf("Case %d: The total value of the hook is %d.\n", ca++, sum[1]);
    }
    return 0;
}


你可能感兴趣的:(hdu 1698 屠夫(线段树成段替换))