HDU 1698 区间更新

Just a Hook

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

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.

Source

2008 “Sunline Cup” National Invitational Contest

题目意思:

给一个长度为n的数组,q次操作,每次操作为l, r, w   即把l-r区间数组元素变为w,初始化数组所有元素都为1

思路:

线段树区间更新,记录颜色

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <set>
 9 using namespace std;
10
11 #define N 100005
12 #define ll root<<1
13 #define rr root<<1|1
14 #define mid (a[root].l+a[root].r)/2
15
16
17 int max(int x,int y){return x>y?x:y;}
18 int min(int x,int y){return x<y?x:y;}
19 int abs(int x,int y){return x<0?-x:x;}
20
21 int n;
22
23 struct node{
24     int l, r, val;
25     bool f;
26 }a[N*4];
27
28 void build(int l,int r,int root){
29     a[root].l=l;
30     a[root].r=r;
31     a[root].val=1;
32     if(l==r){
33         return;
34     }
35     build(l,mid,ll);
36     build(mid+1,r,rr);
37 }
38
39 void update(int l,int r,int val,int root){
40     if(a[root].val==val) return;
41     if(a[root].l==l&&a[root].r==r){
42         a[root].val=val;
43         return;
44     }
45     if(a[root].val){
46         if(a[root].l!=a[root].r) {
47             a[ll].val=a[rr].val=a[root].val;
48         }
49     }
50     if(l>=a[rr].l) update(l,r,val,rr);
51     else if(r<=a[ll].r) update(l,r,val,ll);
52     else {
53         update(l,mid,val,ll);
54         update(mid+1,r,val,rr);
55     }
56     if(a[ll].val==a[rr].val) a[root].val=a[ll].val;
57     else a[root].val=0;
58 }
59
60 int get_sum(int root){
61     if(a[root].val) return a[root].val*(a[root].r-a[root].l+1);
62     return get_sum(ll)+get_sum(rr);
63 }
64
65 main()
66 {
67     int q, i, j, k, x, y, z;
68     int t;
69     cin>>t;
70     for(k=1;k<=t;k++){
71         scanf("%d %d",&n,&q);
72         build(1,n,1);
73         while(q--){
74             scanf("%d %d %d",&x,&y,&z);
75             update(x,y,z,1);
76         }
77         printf("Case %d: The total value of the hook is %d.\n",k,get_sum(1));
78
79     }
80 }
时间: 2025-02-01 08:49:24

HDU 1698 区间更新的相关文章

hdu 4614(区间更新)

这里写代码片题意:有n个花瓶编号从0到n-1,初始花瓶都是空的,然后有两个操作,1 a b表示从位置a开始往后面找b(不够b个也可以)个空花瓶插花,输出插花的首位置和末位置,2 a b表示输出区间[a,b]有多少个花,并且把这个区间内所有花都拿走. 题解:很容易想到用线段树维护区间内有多少个空花瓶,然后操作2可以直接用普通的区间查询和区间修改,操作1可以看作先查询前a-1个花瓶有num个是空的,然后查询第num+1和第num+b个空花瓶的位置,这个查询也可以用线段树查询或二分查找来写. #inc

HDU(1698),线段树区间更新

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r-l+1);col[rt] = c:后面的子区间就不管了,当你下次更新某一个区间的时候,把col[rt]从顶往下推(也没有推到底),推到合适的位置,刚好这个位置是我要更新的区间的子区间(可能被横跨了)就停下来. 在这里感谢网上的大牛,感谢杰哥,彬哥.我才能AC. Just a Hook Time Li

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 (线段树,区间更新)

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 (线段树 区间更新基础)

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(线段树,区间更新)

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

暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)

题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场战斗需要对组成钩子某个区间的棍子进行调整.问经过q次调整后钩子的总价值是多少? 解题思路: 线段树更新.因为数据范围的问题,每次更新到节点会TLE.用区间更新就会节省很多的时间,对每一个区间来讲,如果这个区间里面的棍子都是相同类型的,就用一个节点变量记录棍子类型,如果区间内棍子是不同类型,则将变量记

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

POJ 2777 &amp;&amp; ZOJ 1610 &amp;&amp;HDU 1698 --线段树--区间更新

直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 -- 区间更新的话 分为 增减 或者 修改 主要就是个 laze 标记 就是延迟更新 对于区间更新的写法 一般是有2种 其一 仔细划分到每个细小的区间    另一 粗略划分 反正 ==我的代码里会给出2种写法 看自己喜好 hdu 1 //线段树 成段更新 ---> 替换 根结点的查询 2 3 #i