FZU-1608 Huge Mission 线段树(更新懒惰标记)

题目链接:

https://cn.vjudge.net/problem/FZU-1608

题目大意:

长度n,m次操作;每次操作都有三个数:a,b,c;意味着(a,b]区间单位长度的价值为c,若某段长度不曾被操作,则意味着该长度价值为0,若有一段长度有多个价值,则选取最大的。(多组输入)请输出(0,n]内最大价值和。

解题思路:

直接进行懒惰标记更新即可。

然后再直接处理出每个数字最大值即可。也就是不断pushdown懒惰标记直到每个叶子节点,这样叶子节点的懒惰标记一定是最大值。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
 6 #define Max(a, b) (a) > (b) ? (a) : (b)
 7 #define Min(a, b) (a) < (b) ? (a) : (b)
 8 #define Mem(a) memset(a, 0, sizeof(a))
 9 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
10 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
11 using namespace std;
12 inline int read()
13 {
14     int x=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
17     return x*f;
18 }
19 
20 typedef long long ll;
21 const int maxn = 50000 + 10;
22 const int MOD = 1000000007;//const引用更快,宏定义也更快
23 #define MID(l, r) ((l) + ((r) - (l)) / 2)
24 #define lson(o) ((o)<<1)
25 #define rson(o) ((o)<<1|1)
26 struct node
27 {
28     int l, r, lazy, sum;
29 }tree[maxn * 4];
30 void pushup(int o)
31 {
32     tree[o].sum = tree[lson(o)].sum + tree[rson(o)].sum;
33 }
34 void pushdown(int o)
35 {
36     if(tree[o].lazy > 0)
37     {
38         int lc = lson(o), rc = rson(o);
39         tree[lc].lazy = Max(tree[lc].lazy, tree[o].lazy);
40         tree[rc].lazy = Max(tree[rc].lazy, tree[o].lazy);
41     }
42 }
43 void build(int o, int l, int r)
44 {
45     tree[o].l = l;
46     tree[o].r = r;
47     if(l == r)
48     {
49         tree[o].lazy = tree[o].sum = 0;
50         return;
51     }
52     int m = MID(l, r);
53     build(lson(o), l, m);
54     build(rson(o), m + 1, r);
55     pushup(o);
56 }
57 int ql, qr;
58 int v;
59 void update(int o)//懒惰标记
60 {
61     //cout<<o<<" "<<tree[o].l<<" "<<tree[o].r<<" "<<ql<<" "<<qr<<endl;
62     if(ql <= tree[o].l && qr >= tree[o].r)tree[o].lazy = Max(tree[o].lazy, v);
63     else
64     {
65         int m = MID(tree[o].l, tree[o].r);
66         if(ql <= m)update(lson(o));
67         if(qr > m)update(rson(o));
68     }
69 }
70 void query(int o)
71 {
72     if(tree[o].l == tree[o].r)//如果是叶子节点,那就计算sum值
73     {
74         tree[o].sum = Max(tree[o].sum, tree[o].lazy);
75         return;
76     }
77     pushdown(o);//不是叶子节点,那就把懒惰标记传递下去
78     query(lson(o));
79     query(rson(o));
80     pushup(o);//计算总和。
81 }
82 int main()
83 {
84     int n, m;
85     while(scanf("%d%d", &n, &m) != EOF)
86     {
87         Mem(tree);
88         build(1, 1, n);
89         while(m--)
90         {
91             ql = read(), qr = read(), v = read();
92             ql++;
93             update(1);
94         }
95         query(1);
96         printf("%d\n", tree[1].sum);
97     }
98     return 0;
99 }

猜你喜欢

转载自www.cnblogs.com/fzl194/p/9562854.html
今日推荐