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 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 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4
 5 const int MAXN=1e5+5;
 6 int st[MAXN<<2],lazy[MAXN<<2];///lazy懒标记数组
 7
 8 void pushup(int rt){
 9     st[rt]=st[rt<<1]+st[rt<<1|1];
10 }
11
12 void pushdown(int l,int r,int rt){
13     if(lazy[rt]==0)return;
14     lazy[rt<<1]=lazy[rt];
15     lazy[rt<<1|1]=lazy[rt];
16     int mid=(l+r)>>1;
17     st[rt<<1]=lazy[rt]*(mid-l+1);
18     st[rt<<1|1]=lazy[rt]*(r-mid);
19     lazy[rt]=0;
20 }
21
22 void build(int l,int r,int rt){
23     lazy[rt]=0;
24     if(l==r){
25         st[rt]=1;
26         return;
27     }
28     int mid=(l+r)>>1;
29     pushdown(l,r,rt);
30     build(l,mid,rt<<1);
31     build(mid+1,r,rt<<1|1);
32     pushup(rt);
33 }
34
35 void update(int L,int R,int val,int l,int r,int rt){
36     if(L<=l&&r<=R){
37         lazy[rt]=val;
38         st[rt]=(r-l+1)*val;
39         return;
40     }
41     pushdown(l,r,rt);
42     int mid=(l+r)>>1;
43     if(L<=mid)update(L,R,val,l,mid,rt<<1);
44     if(R>mid)update(L,R,val,mid+1,r,rt<<1|1);
45     pushup(rt);
46 }
47
48 int query(int L,int R,int l,int r,int rt){
49     if(L<=l&&r<=R)return st[rt];
50     pushdown(l,r,rt);
51     int mid=(l+r)>>1,res=0;
52     if(L<=mid)res+=query(L,R,l,mid,rt<<1);
53     if(R>mid)res+=query(L,R,mid+1,r,rt<<1|1);
54     pushup(rt);
55     return res;
56 }
57
58 int main(){
59     int t,n,q,x,y,z,ca=0;
60     scanf("%d",&t);
61     while(t--){
62         scanf("%d%d",&n,&q);
63         build(1,n,1);
64         while(q--){
65             scanf("%d%d%d",&x,&y,&z);
66             update(x,y,z,1,n,1);
67         }
68         printf("Case %d: The total value of the hook is %d.\n",++ca,st[1]);
69     }
70 }

原文地址:https://www.cnblogs.com/ChangeG1824/p/11508236.html

时间: 2024-10-29 19:06:53

hdu1698 Just a Hook(线段树+区间修改+区间查询+模板)的相关文章

【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表)也可以树状存储(链表) 树形typedef struct node { int data; struct node* Lchild; struct node* Rchild; }Btree,*BTree;BTree = (BTree)malloc(Btree);好像是这样吧...大半个暑假过去忘得

SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)

GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in contests. When

hihocoder1080线段树+区间修改+区间查询+多个懒标记

题目链接:http://hihocoder.com/problemset/problem/1080 对于这种不止一个懒标记的线段树,只要弄清楚各种操作和各种懒标记间的关系就OK了. 我的代码: 1 #include <cstdio> 2 3 using namespace std; 4 5 #define MAXN 100005 6 7 int p[MAXN]; 8 9 struct segNode 10 { 11 int left, right, sum, dd, vv; 12 bool l

【模板】线段树 区间修改+区间查询

题目大意:维护一个长度为 N 的序列,支持区间修改.区间查询两种操作. 代码如下 #include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; inline int read(){ int x=0,f=1;char ch; do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch)); do{x=x*10+ch-'0';ch=getchar();}while(isdigit(

hdu1698 Just a Hook (线段树区间更新 懒惰标记)

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

A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 100010; int n, m; int w[N]; struct Node { int l, r;

线段树区间修改版本模板

#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int sum1[1000]; int sum2[1000]; int a[1000]; int n,m; int lowbit(int x){ return x&(-x); } void update(int x,int w){//更新效果:把x位置后面所有

hdu1698 Just a Hook 线段树:成段替换,总区间求和

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 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 consecut

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.