E - Just a Hook HDU - 1698

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HNUST_LIZEMING/article/details/82378443

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1698

题意

在 DotA 游戏中,帕吉的肉钩是很多英雄最害怕的东西。钩子由连续若干段的等长金属棒制成。

现在帕吉对钩子由一些操作:

我们将金属棒 1~n 依次编号,帕吉可以把编号 x~y 的金属棒变成铜棒、银棒、金棒。

每段铜棒的价值是 1;每段银棒的价值是 2;每段金棒的价值是 3。

肉钩的总价值是 n 段金属棒价值之和。

帕吉想知道若干操作以后钩子的总价值。

题解

线段树区间更新、区间查询问题。 
区间更新原理:每一个节点代表一个区间上的信息,对区间进行修改,即对若干个对应的节点进行修改。而考虑到暴力修改所有节点的时间复杂度难以接受,所以引入一个延迟标记。

也就是说只修改一个节点,而对该节点的子节点暂时不修改,等到下一次需要用到该节点的子节点时(包括更新和修改),再根据延迟标记进行修改。

#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<iostream>  
#include<string>  
#include<vector>  
#include<stack>  
#include<bitset>  
#include<cstdlib>  
#include<cmath>  
#include<set>  
#include<list>  
#include<deque>  
#include<map>  
#include<queue>  
#define Max(a,b) ((a)>(b)?(a):(b))  
#define Min(a,b) ((a)<(b)?(a):(b))  
using namespace std;  
typedef long long ll;  
const double PI = acos(-1.0);  
const double eps = 1e-6;  
const int INF = 1000000000;  
const int maxn = 100000 + 10;  
int T,n,l,r,v,q,sum[maxn*4],cur[maxn*4],kase=0;  
void push_up(int o) {  
    sum[o] = sum[o<<1] + sum[o<<1|1];  
}  
void pushdown(int o, int l, int r) {  
    if(cur[o]) {  
        int m = (l + r) >> 1;  
        cur[o<<1] = cur[o<<1|1] = cur[o];  
        sum[o<<1] = (m - l + 1) * cur[o];  
        sum[o<<1|1] = (r - m) * cur[o];  
        cur[o] = 0;  
    }  
}  
void build(int l, int r, int o) {  
    int m = (l + r) >> 1;  
    cur[o] = 0;  
    if(l == r) {  
        sum[o] = 1; return ;  
    }  
    build(l, m, o<<1);  
    build(m+1, r, o<<1|1);  
    push_up(o);  
}  
void update(int L, int R, int c, int l, int r, int o) {  
    int m = (l + r) >> 1;  
    if(L <= l && r <= R) {  
        cur[o] = c;  
        sum[o] = c * (r - l + 1);  
        return ;  
    }  
    pushdown(o, l, r);  
    if(L <= m) update(L, R, c, l, m, o<<1);  
    if(m < R) update(L, R, c, m+1, r, o<<1|1);  
    push_up(o);  
}  
int main() {  
    scanf("%d",&T);  
    while(T--) {  
        scanf("%d%d",&n,&q);  
        build(1,n,1);  
        while(q--) {  
            scanf("%d%d%d",&l,&r,&v);  
            update(l, r, v, 1, n, 1);  
        }  
        printf("Case %d: The total value of the hook is %d.\n",++kase , sum[1]);  
    }  
    return 0;  
}  

猜你喜欢

转载自blog.csdn.net/HNUST_LIZEMING/article/details/82378443