hdu1698(线段树)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息)

#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 100010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int sum[N<<2],col[N<<2];
void Pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void Pushdown(int rt,int len)
{
    if(col[rt])
    {
        col[rt<<1]=col[rt<<1|1]=col[rt];
        sum[rt<<1]=col[rt]*(len-(len>>1));
        sum[rt<<1|1]=col[rt]*(len>>1);
        col[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    col[rt]=0;
    if(l==r)
    {
        sum[rt]=1;return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    Pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        sum[rt]=(r-l+1)*c;
        col[rt]=c;
        return;
    }
    Pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,lson);
    if(m<R)update(L,R,c,rson);
    Pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return sum[rt];
    }
    Pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    int res=0;
    if(L<=m)res+=query(L,R,lson);
    if(m<R)res+=query(L,R,rson);
    return res;
}
int main()
{
    int n,m,t,cas=1;
    int a,b,c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1,n,1);
        }
        printf("Case %d: ",cas++);
        printf("The total value of the hook is %d.\n",query(1,n,1,n,1));
    }
}

时间: 2024-10-23 02:22:56

hdu1698(线段树)的相关文章

HDU1698线段树区间更新

原题http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16935    Accepted Submission(s): 8427 Problem Description In the game of DotA, Pudge

hdu1698(线段树区间替换模板)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意: 第一行输入 t 表 t 组测试数据, 对于每组测试数据, 第一行输入一个 n , 表示钩子有 n 节, 编号为 1 ~ n, 每节钩子的初始价值为 1 , 接下来输入一个 q, 接着 q 行输入, 每行格式为 l, r, x, 表示讲区间 [l, r] 内的钩子价值变成 x , 求最终的总价值: 思路: 线段树区间替换模板 代码: 1 #include <iostream> 2 #

hdu1698(线段树的区间替换)

HDU1698 #include <bits/stdc++.h> using namespace std; #define Maxn 1001000*4 struct Node{ int lt,rt,val; }A[Maxn]; int j = 1; void Build(int i,int lt,int rt){ A[i].lt = lt; A[i].rt = rt; A[i].val = 1; if( lt == rt ){ return ; } int mid = (lt+rt) >

hdu1698 Just a Hook 线段树:成段替换,总区间求和

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 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 consecut

HDU-1698 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. Now Pudge wants to do some operations on t

HDU1698 Just a Hook 【线段树】+【成段更新】+【lazy标记】

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

Hdu1698 Just a Hook(线段树成段更新)

题意很简单:1-n个钩子初始价值是1,然后题目给出Q个操作,x y z,将x->y的钩子价值改为z,最后输出n个钩子的总价值. 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) //3160 KB 624 ms #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define M 100005 #define

【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表)也可以树状存储(链表) 树形typedef struct node { int data; struct node* Lchild; struct node* Rchild; }Btree,*BTree;BTree = (BTree)malloc(Btree);好像是这样吧...大半个暑假过去忘得

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): 34542    Accepted Submission(s): 16868 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing