hdu 1698 线段树 区间修改

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 200010
#define INF 0x7fffffff
#define inf 10000000
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
using namespace std;

struct node {
    int ri, le, v, sum;
};

node tree[2*maxn];

void build(int id, int L, int R) {
    tree[id].le = L, tree[id].ri = R;
    if(L == R) {
        tree[id].v = 1, tree[id].sum = 1;
    }
    else {
        int M = L + (R-L)/2;
        build(id*2, L, M);
        build(id*2+1, M+1, R);
        tree[id].v = 0;
        tree[id].sum = tree[id*2].sum + tree[id*2+1].sum;
    }
}

void update(int id, int L, int R, int c) {
    if(tree[id].le == L && tree[id].ri == R) {
        tree[id].v = c;
        tree[id].sum = c*(tree[id].ri-tree[id].le+1);
    }
    else {
        if(tree[id].v > 0) {
            tree[id*2+1].v = tree[id*2].v = tree[id].v;
            tree[id*2].sum = tree[id].v*(tree[id*2].ri - tree[id*2].le + 1);
            tree[id*2+1].sum = tree[id].v*(tree[id*2+1].ri - tree[id*2+1].le + 1);
        }
        tree[id].v = 0;
        int M = tree[id].le + (tree[id].ri - tree[id].le)/2;
        if(R <= M) update(id*2, L, R, c);
        else if(L > M) update(id*2+1, L, R, c);
        else {
            update(id*2, L, M, c);
            update(id*2+1, M+1, R, c);
        }
        tree[id].sum = tree[id*2].sum + tree[id*2+1].sum;
    }
}

int main()
{
    int t, n, m, ca = 0;
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d%d", &n, &m);
        build(1, 1, n);
        for(int i = 0; i < m; ++ i)
        {
            int y11, y22, c;
            scanf("%d%d%d", &y11, &y22, &c);
            update(1, y11, y22, c);
        }
        printf("Case %d: The total value of the hook is %d.\n", ++ ca, tree[1].sum);
    }
    return 0;
}

posted @ 2014-05-22 20:39  xlc2845  阅读(116)  评论(0)    收藏  举报