hdoj 1689 Just a Hook【线段树区间修改】

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23137    Accepted Submission(s):
11600

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 the hook.

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 or golden sticks.
The total value of the hook is
calculated as the sum of values of N metallic sticks. More precisely, the value
for each kind of stick is calculated as follows:

For each cupreous stick,
the value is 1.
For each silver stick, the value is 2.
For each golden
stick, the value is 3.

Pudge wants to know the total value of the hook
after performing the operations.
You may consider the original hook is made
up of cupreous sticks.

Input

The input consists of several test cases. The first
line of the input is the number of the cases. There are no more than 10
cases.
For each case, the first line contains an integer N,
1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and
the second line contains an integer Q, 0<=Q<=100,000, which is the number
of the operations.
Next Q lines, each line contains three integers X, Y,
1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the
sticks numbered from X to Y into the metal kind Z, where Z=1 represents the
cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden
kind.

Output

For each case, print a number in a line representing
the total value of the hook after the operations. Use the format in the
example.

Sample Input

1

10

2

1 5 2

5 9 3

Sample Output

Case 1: The total value of the hook is 24.

题意:输入t代表有t组测试数据,然后一个数字n代表n个英雄,接下来数字q代表接下来有q行,每行三个整数a,b,c,代表将区间a到b的值全部换为c,最后问总分数,注意  初始分数为1

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 100100
#define INF 0x3f3f3f
using namespace std;
int sum[MAX<<2];
int change[MAX<<2];
void pushup(int o)
{
	sum[o]=sum[o<<1]+sum[o<<1|1];
}
void pushdown(int o,int m)
{
	if(change[o])
	{
		change[o<<1]=change[o<<1|1]=change[o];
		sum[o<<1]=change[o]*(m-(m>>1));
		sum[o<<1|1]=change[o]*(m>>1);
		change[o]=0;
	}
}
void gettree(int o,int l,int r)
{
	sum[o]=1;change[o]=0;
	if(l==r)
	    return ;
	int mid=(l+r)>>1;
	gettree(o<<1,l,mid);
	gettree(o<<1|1,mid+1,r);
	pushup(o);
}
void update(int o,int l,int r,int L,int R,int v)
{
	if(L<=l&&R>=r)
	{
		change[o]=v;
		sum[o]=v*(r-l+1);
		return ;
	}
	pushdown(o,r-l+1);
	int mid=(r+l)>>1;
	if(L<=mid)
	    update(o<<1,l,mid,L,R,v);
	if(R>mid)
	    update(o<<1|1,mid+1,r,L,R,v);
    pushup(o);
}
int find(int o,int l,int r,int L,int R)
{
	if(L<=l&&R>=r)
	{
		return sum[o];
	}
	pushdown(o,r-l+1);
	int ans=0;
	int mid=(r+l)>>1;
	if(L<=mid)
	    ans+=find(o<<1,l,mid,L,R);
	if(R>mid)
	    ans+=find(o<<1|1,mid+1,r,L,R);
	return ans;
}
int main()
{
	int t,k,i,j;
	int n,m;
	k=1;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		int a,b,c;
		gettree(1,1,n);
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			update(1,1,n,a,b,c);
		}
		printf("Case %d: The total value of the hook is ",k++);
		printf("%d.\n",find(1,1,n,1,n));
	}
	return 0;
}

  

时间: 2024-10-14 16:58:39

hdoj 1689 Just a Hook【线段树区间修改】的相关文章

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

Hdu1698Just a Hook线段树区间更新

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

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 (线段树区间修改)

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(线段树区间替换)

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

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

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