HDU 1166 —— Just a Hook 【线段树 区间修改】

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

要求:

  1. 区间修改

  2. 最后整个区间求和

分析:

  因为没有多次查询,而只有最后的一次整个区间的求和查询,所以可以省去query查询函数。

#include <cstdio>
#include <iostream>
#define lson rt << 1, l, m
#define rson rt << 1 | 1, m+1, r
using namespace std;

const int MAXN = 131072 + 5;
int sum[MAXN * 2];
int set[MAXN * 2];
int n;

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

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

void update(int ql, int qr, int x, int rt, int l, int r)
{
    if(ql <= l && r <= qr) {
        set[rt] = x;
        sum[rt] = x * (r - l + 1);
    } else {
        pushdown(rt, r-l+1);
        int m = (l + r) >> 1;
        if(ql <= m)    update(ql, qr, x, lson);
        if(qr > m)    update(ql, qr, x, rson);
        pushup(rt);
    }
}

void build(int rt, int l, int r) // O(n)
{
    set[rt] = 0; // Initialization

    if(l == r) {
        sum[rt] = 1; // the original hook is made up of cupreous sticks.
    } else {
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        pushup(rt);
    }
}

int main ()
{
    int T, q, x, y, z;
    scanf("%d", &T);
    for(int kase=1; kase<=T; kase++) {
        scanf("%d%d", &n, &q);
        build(1, 1, n);
        while(q--) {
            scanf("%d%d%d", &x, &y, &z);
            update(x, y, z, 1, 1, n);
        }
        printf("Case %d: The total value of the hook is %d.\n", kase, sum[1]);
    }
    return 0;
}
时间: 2024-08-04 20:10:17

HDU 1166 —— Just a Hook 【线段树 区间修改】的相关文章

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU 1698 Just a Hook(线段树区间替换)

题目地址:HDU 1698 区间替换裸题.同样利用lazy延迟标记数组,这里只是当lazy下放的时候把下面的lazy也全部改成lazy就好了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #in

hdu 4902 Nice boat(线段树区间修改,输出最终序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his peopl

hdu 1698 Just a Hook 线段树区间更新

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 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

HDU 1698 Just a Hook (线段树 区间更新基础)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21856    Accepted Submission(s): 10963 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

HDU 1689 Just a Hook 线段树区间更新求和

点击打开链接 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18894    Accepted Submission(s): 9483 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible

(简单) HDU 1698 Just a Hook , 线段树+区间更新。

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. Now Pudge wants to do some operations on the hook

hdu - 1689 Just a Hook (线段树区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<=z<=3),问更新完后的总价值. 线段树的区间更新,需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新或者询问的时候. 这题只需要输出总区间的信息,即直接输出1结点的信息. 1 #include <iostream> 2 #include <

[HDU] 1698 Just a Hook [线段树区间替换]

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18378    Accepted Submission(s): 9213 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU - 1166 敌兵布阵 (线段树+单点修改,区间查询和)

C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视. 中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇