E - Just a Hook HDU - 1698

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

通过这个题练习了基本的Pushdown的操作

参考着蓝书的模板敲了一下,需要修改的地方就是:这里的区间修改是直接改变值而不是增加值

把+=改成=即可。

 1 #include <cmath>
 2 #include <queue>
 3 #include <string>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <iostream>
 7 #include <algorithm>
 8 #define forn(i, n) for (int i = 0; i < (n); i++)
 9 #define forab(i, a, b) for (int i = (a); i <= (b); i++)
10 #define forba(i, b, a) for (int i = (b); i >= (a); i--)
11 #define mset(a, n) memset(a, n, sizeof(a))
12 #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
13 #define P pair<int,int>
14 #define fi first
15 #define se second
16 using namespace std;
17 #define N 100005
18 #define inf 0x3f3f3f3f
19 #define ll long long
20 struct SegmentTree
21 {
22     int l, r;
23     ll data;
24     ll add;
25     #define l(x) t[x].l
26     #define r(x) t[x].r
27     #define data(x) t[x].data
28     #define add(x) t[x].add
29 } t[N * 4 + 5];
30 ll n, m, cnt;
31 void build(int p,int l,int r)
32 {
33     l(p) = l;
34     r(p) = r;
35     add(p) = 0;
36     if(l==r)
37     {
38         data(p) = 1;
39         return;
40     }
41     int m = (l + r) / 2;
42     int ls = 2 * p, rs = 2 * p + 1;
43     build(ls, l, m);
44     build(rs, m + 1, r);
45     data(p) = data(ls) + data(rs);
46 }
47 void spread(int p)
48 {
49     if(add(p))
50     {
51         data(2 * p) = add(p) * (r(2 * p) - l(2 * p) + 1);
52         data(2 * p + 1) = add(p) * (r(2 * p + 1) - l(2 * p + 1) + 1);
53         add(2 * p) = add(p);
54         add(2 * p + 1) = add(p);
55         add(p) = 0;
56     }
57 }
58 void change(int p,int l,int r,int d)
59 {
60     if(l<=l(p)&&r>=r(p))
61     {
62         data(p) = (r(p) - l(p) + 1) * d;
63         add(p) = d;
64         return;
65     }
66     spread(p);
67     int m = (l(p) + r(p)) / 2;
68     int ls = 2 * p, rs = 2 * p + 1;
69     if(l<=m)
70         change(ls, l, r, d);
71     if(r>m)
72         change(rs, l, r, d);
73     data(p) = data(ls) + data(rs);
74 }
75 int main()
76 {
77     fast;
78     cin >> cnt;
79     forab(cas,1,cnt)
80     {
81         cin >> n >> m;
82         build(1, 1, n);
83         forab(i,1,m)
84         {
85             int x, y, z;
86             cin >> x >> y >> z;
87             change(1, x, y, z);
88         }
89         cout << "Case " << cas << ": The total value of the hook is " <<t[1].data<< "." << endl;
90     }
91 }
92 /*
93 1
94 10
95 2
96 1 5 2
97 5 9 3
98  */

原文地址:https://www.cnblogs.com/zstofljq/p/11076234.html

时间: 2024-11-08 20:31:30

E - Just a Hook HDU - 1698的相关文章

E - Just a Hook HDU 1698 (线段树+类似延迟标记)

E - Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice 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

E - Just a Hook - hdu 1698(区间覆盖)

某个英雄有这样一个金属长棍,这个金属棍有很多相同长度的短棍组成,大概最多有10w节,现在这个人有一种魔法,他可以把一段区间的金属棍改变成别的物质,例如金银或者铜, 现在他会有一些操作在这个金属棍上,他想知道这些操作结束后金属棍的质量是多少呢?(PS,一节铜重量1, 银 2 ,金3). 分析:如果做了那到海报覆盖的题目会发现这两题是差不多的,都可以从后面往前去覆盖,只不过返回这次能覆盖多少节就行了. *************************************************

Just a Hook HDU - 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 consecutive metallic sticks which are of the same length Now Pudge wants to do some operations on th

Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int N=1e5+10; struct node{ int l,r; int sum; int add; }tr[N*4]; void pushdown(int root) { if (tr[root].add)

hdu 1698 Just a Hook 基本线段树

使用线段树更新每段区间的奖(1,2,3),最后在统计整段区间的数和,基本线段树,果断1A啊 #include<iostream> #include<stdio.h> using namespace std; #define N 100000 struct node{ int l,r,p; }a[N*4]; int n; void build(int left,int right,int i){ a[i].l=left; a[i].r=right; a[i].p=1; if(a[i]

线段树(成段更新) HDU 1698 Just a Hook

题目传送门 1 /* 2 线段树-成段更新:第一题!只要更新区间,输出总长度就行了 3 虽然是超级裸题,但是用自己的风格写出来,还是很开心的:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <iostream> 11 using namesp

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 (线段树 成段更新 lazy-tag思想)

题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这个区间值不统一,而且已经向下更新了, val != -1表示这个区间值统一, 更新某个区间的时候只需要把这个区间分为几个区间更新就行了, 也就是只更新到需要更新的区间,不用向下更新每一个一直到底了,在更新的过程中如果遇到之前没有向下更新的, 就需要向下更新了,因为这个区间的值已经不统一了. 其实这就

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.