hdu just a hook(线段树,区间修改)

Just a Hook

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

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.

一开始忘记了r--,l--

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <cstring>
 6 using namespace std;
 7 const int Max=100000+3;
 8 int segTree[Max<<2];
 9 int change[Max<<2];
10 int T,q,r,l,z,n;
11 int PushUp(int node)    //回溯,节点更新
12 {
13     segTree[node]=(segTree[node<<1]+segTree[node<<1|1]);
14     return 0;
15 }
16 int PushDown(int node,int arange)
17 {
18     if(change[node]){
19         change[node<<1]=change[node<<1|1]=change[node];
20         segTree[node<<1]=change[node]*(arange-(arange>>1));
21         segTree[node<<1|1]=change[node]*(arange>>1);
22         change[node]=0;
23     }
24     return 0;
25 }
26 void build(int node,int begin,int end)
27 {
28     segTree[node]=1;
29     change[node]=0;
30     if(begin==end)
31         return;
32     int m=(begin+end)>>1;
33     build(node<<1,begin,m);
34     build(node<<1|1,m+1,end);
35     PushUp(node);
36     return;
37 }
38 int updata(int node,int begin,int end)
39 {
40     if(l<=begin&&end<=r)
41     {
42         segTree[node]=z*(end-begin+1);
43         change[node]=z;
44         return 0;
45     }
46     PushDown(node,end-begin+1);
47     int mid=(begin+end)>>1;
48     if(mid>=l)    updata(node<<1,begin,mid);
49     if(mid<r)    updata(node<<1|1,mid+1,end);
50     PushUp(node);
51 }
52 int main()
53 {
54     int i,j;
55     freopen("in.txt","r",stdin);
56     cin>>T;
57     int k=T;
58     while(T--)
59     {
60         scanf("%d%d",&n,&q);
61         build(1,0,n-1);
62         for(i=0;i<q;i++)
63         {
64             scanf("%d%d%d",&l,&r,&z);
65             l--,r--;
66             updata(1,0,n-1);
67         }
68         printf("Case %d: The total value of the hook is %d.\n",k-T,segTree[1]);
69     }
70 }

更简单的代码:

 1 #include <cstdio>
 2 using namespace std;
 3 #define maxN 100005
 4 #define m ((L+R)>>1)
 5 int T, N, Q, X, Y, Z, i;
 6 int val[4 * maxN];
 7 void push_down(int o){
 8     val[o * 2] = val[o * 2 + 1] = val[o];
 9     val[o] = 0;
10 }
11 void update(int o, int L, int R){
12     if (X <= L&&R <= Y) val[o] = Z;
13     else{
14         if (val[o]) push_down(o);
15         if (X <= m) update(o * 2, L, m);
16         if (Y > m) update(o * 2 + 1, m + 1, R);
17     }
18 }
19 int sum(int o,int L,int R){
20     if (val[o]) return (R-L+1)*val[o];
21     return sum(o * 2, L, m) + sum(o * 2 + 1, m + 1, R);
22 }
23 int main(){
24     scanf("%d", &T);
25     for (int Case = 1; Case <= T; Case++){
26         val[1] = 1;
27         scanf("%d%d", &N, &Q);
28         for (i = 0; i < Q; i++){
29             scanf("%d%d%d", &X, &Y, &Z);
30             update(1, 1, N);
31         }
32         printf("Case %d: The total value of the hook is %d.\n",Case,sum(1,1,N));
33     }
34     return 0;
35 }

时间: 2024-10-01 04:56:34

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>