HDU 1689 Just a Hook (线段树, 区间修改)

2024-01-03 13:38
文章标签 修改 区间 hdu 线段 hook 1689

本文主要是介绍HDU 1689 Just a Hook (线段树, 区间修改),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


这道题 卡了我 长达 一个月之久, 从 树状数组 开始 就一直 WA,  简单的一道 线段树 区间修改模板题, 愣是让我 做了这么久 ,  线段树 区间 修改 延迟标记,!!!!!!!!!!!!

Just a Hook

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


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.

反正题意 就是 让你求修改后 1-n 之间的和,  

线段树,   区间修改模板!!!;

注意的要点在于  

1 :延迟修改时    L 与 R  是 建立区间的 长度 而不是 修改区间的长度;

2 :数值修改时    L 与 R  是 建立区间的 长度 而不是 修改区间的长度;

3 : 要进行 向上更新 和向下更新,  并且 向下更新 一定要在  左右区间回溯之前,  向上更新 在 左右区间之后



我的代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define Pr(n)     printf("%d\n",n)using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};struct SegTree{int val;int addmark;
}Seg[maxn<<2];void PushUP(int root)
{Seg[root].val=Seg[root<<1].val+Seg[root<<1|1].val;
}void build(int root,int l,int r)
{Seg[root].addmark=0;if(l==r){Seg[root].val=1;return;}int mid=(l+r)>>1;build(root<<1,l,mid);build(root<<1|1,mid+1,r);PushUP(root);
}
void PushDown(int root,int l,int r)
{if(Seg[root].addmark){Seg[root<<1].addmark=Seg[root<<1|1].addmark=Seg[root].addmark;Seg[root<<1].val=Seg[root].addmark*l;Seg[root<<1|1].val=Seg[root].addmark*r;Seg[root].addmark=0;}
}
void update(int root,int l,int r,int left,int right,int m)
{int mid=(l+r)>>1;if(left<=l&&right>=r){Seg[root].val=m*(r-l+1);Seg[root].addmark=m;return ;}PushDown(root,mid-l+1,r-mid);if(left<=mid)update(root<<1,l,mid,left,right,m);if(right>mid)update(root<<1|1,mid+1,r,left,right,m);PushUP(root);
}int main()
{int T;int n,m;//FIN;cin>>T;int cont=0;int x,y,z;while(T--){S1(n);build(1,1,n);S1(m);while(m--){scanf("%d%d%d",&x,&y,&z);update(1,1,n,x,y,z);}printf("Case %d: The total value of the hook is %d.\n",++cont,Seg[1].val);}
}



这篇关于HDU 1689 Just a Hook (线段树, 区间修改)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565875

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin