HDU - 1698 Just a Hook (线段树区间修改)

题目链接

题意:

区间修改,询问总和。

思路:

直接上线段树,裸题。

 1 /*
 2 *  Author: windystreet
 3 *  Date  : 2018-08-11 21:02:54
 4 *  Motto : Think twice, code once.
 5 */
 6 #include<bits/stdc++.h>
 7 
 8 using namespace std;
 9 
10 #define X first
11 #define Y second
12 #define eps  1e-5
13 #define gcd __gcd
14 #define pb push_back
15 #define PI acos(-1.0)
16 #define lowbit(x) (x)&(-x)
17 #define bug printf("!!!!!\n");
18 #define mem(x,y) memset(x,y,sizeof(x))
19 
20 typedef long long LL;
21 typedef long double LD;
22 typedef pair<int,int> pii;
23 typedef unsigned long long uLL;
24 
25 const int maxn = 1e5+7;
26 const int INF  = 1<<30;
27 const int mod  = 1e9+7;
28 
29 struct Tree
30 {
31     int l,r,v,lazy;
32 }tree[maxn<<2];
33 
34 void build(int rt,int l,int r){
35     tree[rt].lazy = 0;tree[rt].l=l,tree[rt].r=r;
36     if(l==r){
37         tree[rt].v = 1;return;
38     }
39     int mid = (l+r)>>1;
40     build(rt<<1,l,mid);
41     build(rt<<1|1,mid+1,r);
42     tree[rt].v = tree[rt<<1].v + tree[rt<<1|1].v;
43 }
44 void pushup(int rt){
45     tree[rt].v = tree[rt<<1].v+tree[rt<<1|1].v;
46 }
47 void pushdown(int rt){
48     if(tree[rt].lazy){
49         int mid = (tree[rt].l+tree[rt].r)>>1;
50         tree[rt<<1].lazy = tree[rt<<1|1].lazy = tree[rt].lazy;
51         tree[rt<<1].v = (mid - tree[rt].l + 1)*tree[rt].lazy;
52         tree[rt<<1|1].v = (tree[rt].r - mid )*tree[rt].lazy;
53         tree[rt].lazy = 0;
54     }
55 }
56 void update(int rt,int L, int R,int l, int r,int v){
57     if(l<=L&&R<=r){
58         tree[rt].lazy = v;
59         tree[rt].v = v*(R-L+1);
60         return;
61     }
62     pushdown(rt);                              // 每次修改之前先将标记下推
63     int mid = (L + R) >>1;
64     if(l<=mid) update(rt<<1,L,mid,l,r,v);
65     if(r>mid) update(rt<<1|1,mid+1,R,l,r,v);
66     pushup(rt);                                // 向上更新
67 }
68 
69 
70 void solve(){
71     int n,m,x,y,v;
72     scanf("%d%d",&n,&m);
73     build(1,1,n);
74     while(m--){
75         scanf("%d%d%d",&x,&y,&v);
76         update(1,1,n,x,y,v);
77     }
78     printf("%d.\n",tree[1].v);
79     return;
80 }
81 
82 int main()
83 {
84 //    freopen("F:\\in.txt","r",stdin);
85 //    freopen("out.txt","w",stdout);
86 //    ios::sync_with_stdio(false);
87     int t = 1,cas = 1;
88     scanf("%d",&t);
89     while(t--){
90         printf("Case %d: The total value of the hook is ",cas++);
91         solve();
92     }
93     return 0;
94 }

猜你喜欢

转载自www.cnblogs.com/windystreet/p/9476949.html