【hdu】Just a Hook(线段树区间修改)

线段树模板题,练的是懒惰标记。

懒惰标记,就是更新一段区间的时候,如果小区间被包含在了所需要更新的区间里面,那么直接对代表这个区间的数组元素赋值,之后做一个标记(表示这个区间的子区间都需要更新)但是不继续递归(这样可以节省很多的时候)。

11657115 2014-09-15 14:17:26 Accepted 1698 796MS 2380K 1750 B G++ KinderRiven

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<cstdlib>
#include<stack>
#include<set>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define esp 1e-10
const int maxn = 100000 + 10;
int n;
int tree[maxn << 2];
int mark[maxn << 2];
void BuildTree(int L,int R,int pos){
    if(L == R){
        tree[pos] = 1;
        mark[pos] = 0;
        return ;
    }
    int m = (L + R) >> 1;
    BuildTree(L,m,pos << 1);
    BuildTree(m + 1, R ,(pos << 1)|1);
    tree[pos] = tree[pos << 1] + tree[(pos << 1)|1];
    mark[pos] = 0;
    return ;
}
void UpDate(int l,int r,int add,int L,int R,int pos){
    if(l <= L && R <= r){
        mark[pos] = add;
        tree[pos] = (R - L + 1) * add;
        return ;
    }
    int m = (R + L) >> 1;
    int len  =  R - L + 1;
    if(mark[pos]){  //懒惰标记下移
        mark[pos << 1]     = mark[pos];
        mark[(pos << 1)|1] = mark[pos];
        tree[pos << 1] = (len - (len >> 1)) * mark[pos];
        tree[(pos << 1)|1] = (len >> 1) * mark[pos];
        mark[pos] = 0;
    }
    if(l <= m)UpDate(l,r,add,L,m,pos << 1);
    if(r >  m)UpDate(l,r,add,m + 1,R,(pos << 1)|1);
    tree[pos] = tree[pos << 1] + tree[(pos << 1)|1];
    return ;
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        BuildTree(1,n,1);
        int m ;
        scanf("%d",&m);
        for(int i = 0 ; i < m ; i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            UpDate(x,y,z,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",Case ++,tree[1]);
    }
    return 0;
}
时间: 2024-07-29 15:38:30

【hdu】Just a Hook(线段树区间修改)的相关文章

HDU 5861 Road(线段树 区间修改 单点查询)

Road Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1132    Accepted Submission(s): 309 Problem Description There are n villages along a high way, and divided the high way into n-1 segments. E

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 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

杭电 1698 Just a Hook(线段树区间修改)

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): 17322    Accepted Submission(s): 8640 Problem Description In the game of DotA, Pudge's

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

hdu1698 Just a Hook(线段树+区间修改+区间查询+模板)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54923    Accepted Submission(s): 25566 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of

poj 2777 Count Color(线段树区间修改)

题目链接:http://poj.org/problem?id=2777 题目意思:就是问你在询问的区间里有几种不同的颜色 思路:这题和一般的区间修改差不多,但是唯一不同的就是我们要怎么计算有种颜色,所以这时候我们就需要把延时标记赋予不同的意义,当某段区间有多种颜色时就赋值为-1,当为一种颜色时就把它赋值为这个颜色的号数.这儿我们要怎么统计询问区间不同的颜色数叻,为了不重复计算同一种颜色,那么我们就需要用一个数组来标记计算过的颜色,当我们下次遇到时就不需要再次计算了.... 代码核心处就在计数那儿

hdu-5023 A Corrupt Mayor&#39;s Performance Art (线段树区间修改)

今天集训队打比赛的一道题,很明显是个线段树,我们队照着lrj蓝书敲了一通,机智的将修改值和加和改成了位运算:|= 但是好像哪里出了点小问题,就是不对,赛后又水了一遍,竟然过了...发现还是lrj的书好啊,市面上的模板一点也不好用,连区间修改都没有 . 等集训完了要静心好好系统的学习一下线段树 . 多看多刷lrj的书 . 细节参见代码: #include<bits/stdc++.h> using namespace std; const int maxn = 1000000 + 5; int n

Hdu1698Just a Hook线段树区间更新

区间更新基础..不说了,也是照着notonlysuccess的博客撸的. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list>