HDU ACM 1698 Just a Hook->线段树+区间修改

分析:线段树的应用,区间修改,使用延迟标记进行延迟修改。

#include<iostream>
using namespace std;

#define N 100010

class SegmentTree
{
private:
	struct Node
	{
		int left,right;     //左右子节点
		int sum;            //区间和
		int lazy;           //延迟标记
	};

public:
	void BuildTree(int root,int l,int r);
	void Change(int root,int l,int r,int c);
	int Query(int root);

private:
	Node m_Tree[N<<2];
};

int SegmentTree::Query(int root)
{
	return m_Tree[root].sum;
}

void SegmentTree::Change(int root,int l,int r,int c)
{
	int mid,x,y;

	x=m_Tree[root].left;
	y=m_Tree[root].right;

	if(l==x && r==y)
	{
		m_Tree[root].lazy=1;
		m_Tree[root].sum=(y-x+1)*c;   //区间的每个点都被修改为c,和就等于点数乘以c
		return ;
	}
	mid=(x+y)>>1;
	if(m_Tree[root].lazy==1)    //该if处理延迟更新
	{
		m_Tree[root].lazy=0;
		Change(2*root+1,x,mid,m_Tree[root].sum/(y-x+1));  //m_Tree[root].sum/(r-l+1)即可得到之前保留的单个点的值
		Change(2*root+2,mid+1,y,m_Tree[root].sum/(y-x+1));
	}
	if(l<=mid) Change(2*root+1,l,mid<r?mid:r,c);    //该两句更新包含三种情况,更新区间在mid左边,在右边,左右两边均有
	if(r>mid) Change(2*root+2,mid+1>l?mid+1:l,r,c);

	m_Tree[root].sum=m_Tree[root*2+1].sum+m_Tree[root*2+2].sum;
}

void SegmentTree::BuildTree(int root,int l,int r)
{
	int mid;

	m_Tree[root].left=l;
	m_Tree[root].right=r;
	m_Tree[root].sum=r-l+1;     //刚开始区间和等于区间的点数
	m_Tree[root].lazy=1;        //设置延迟标记

	if(l==r) return ;
	mid=(l+r)>>1;
	BuildTree(2*root+1,l,mid);     //构建左子树
	BuildTree(2*root+2,mid+1,r);   //构建右子树
}

SegmentTree seg_tree;
int main()
{
	int T,n,Q,X,Y,Z,i;

	scanf("%d",&T);
	i=0;
	while(T--)
	{
		scanf("%d",&n);
		seg_tree.BuildTree(0,0,n-1);
		scanf("%d",&Q);
		while(Q--)
		{
			scanf("%d %d %d",&X,&Y,&Z);
			seg_tree.Change(0,X-1,Y-1,Z);
		}
		printf("Case %d: The total value of the hook is %d.\n",++i,seg_tree.Query(0));
	}
	return 0;
}
时间: 2024-10-25 07:25:37

HDU ACM 1698 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 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 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): 18378    Accepted Submission(s): 9213 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU - 3974 Assign the task (线段树区间修改+构建模型)

https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T x y表示给x结点分配编号为y的任务. 分析 题目读起来就很有区间修改的味道,将一个区间变为一个值.问题在于怎么把这棵树对应到区间上. 对于一个结点,其控制的范围是它的子树,对应区间范围可以看作是以dfs序表示的区间.好像有点绕..就是给每个结点再对应一个dfs序,然后在dfs时把这个点控制的子树看

HDU 1698 Just a Hook 线段树区间更新、

来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用lazy标记的话. 就只需要更新这一个区间然后加一个标记,那么如果要访问这个区间的子区间,因为有lazy标记,所以下次访问会将区间的lazy标记传递给子区间,让后去更新子区间,这样我们不必在每次区间更新操作的时候更新该区间的全部子区间,等下次查询到这个区间的时候只需要传递lazy标记就可以了 但从时

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

HDU - 1698 Just a Hook(线段树区间整体修改值,查询区间和)

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. Let us numb