# FZU-1608 Huge Mission 线段树（更新懒惰标记）

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

 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))
11 using namespace std;
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         {
92             ql++;
93             update(1);
94         }
95         query(1);
96         printf("%d\n", tree[1].sum);
97     }
98     return 0;
99 }

posted @ 2018-08-30 21:35  _努力努力再努力x  阅读(241)  评论(0编辑  收藏  举报