hdoj 1698 Just a Hook

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

解题思路:线段树(成段更新)

  1 /**************************************************************************
  2 user_id: SCNU20102200088
  3 problem_id: hdoj 1698
  4 problem_name: Just a Hook
  5 **************************************************************************/
  6 
  7 #include <algorithm>
  8 #include <iostream>
  9 #include <iterator>
 10 #include <iomanip>
 11 #include <sstream>
 12 #include <fstream>
 13 #include <cstring>
 14 #include <cstdlib>
 15 #include <climits>
 16 #include <bitset>
 17 #include <string>
 18 #include <vector>
 19 #include <cstdio>
 20 #include <cctype>
 21 #include <ctime>
 22 #include <cmath>
 23 #include <queue>
 24 #include <stack>
 25 #include <list>
 26 #include <set>
 27 #include <map>
 28 using namespace std;
 29 
 30 //线段树
 31 #define lson l,m,rt<<1
 32 #define rson m+1,r,rt<<1|1
 33 
 34 //手工扩展栈
 35 #pragma comment(linker,"/STACK:102400000,102400000")
 36 
 37 const double EPS=1e-9;
 38 const double PI=acos(-1.0);
 39 const double E=2.7182818284590452353602874713526;  //自然对数底数
 40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
 41 
 42 const int x4[]={-1,0,1,0};
 43 const int y4[]={0,1,0,-1};
 44 const int x8[]={-1,-1,0,1,1,1,0,-1};
 45 const int y8[]={0,1,1,1,0,-1,-1,-1};
 46 
 47 typedef long long LL;
 48 
 49 typedef int T;
 50 T max(T a,T b){ return a>b? a:b; }
 51 T min(T a,T b){ return a<b? a:b; }
 52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
 53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
 54 
 55 ///////////////////////////////////////////////////////////////////////////
 56 //Add Code:
 57 const int maxn=100005;
 58 int sum[maxn<<2],res[maxn<<2];
 59 
 60 void pushup(int rt){
 61     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
 62 }
 63 
 64 void pushdown(int rt,int len){
 65     if(res[rt]){
 66         res[rt<<1]=res[rt<<1|1]=res[rt];
 67         sum[rt<<1]=(len-(len>>1))*res[rt];
 68         sum[rt<<1|1]=(len>>1)*res[rt];
 69         res[rt]=0;
 70     }
 71 }
 72 
 73 void build(int l,int r,int rt){
 74     res[rt]=0;
 75     if(l==r){
 76         sum[rt]=1;
 77         return ;
 78     }
 79     int m=(l+r)>>1;
 80     build(lson);
 81     build(rson);
 82     pushup(rt);
 83 }
 84 
 85 void update(int L,int R,int val,int l,int r,int rt){
 86     int len=r-l+1;
 87     if(l>=L && r<=R){
 88         res[rt]=val;
 89         sum[rt]=val*len;
 90         return ;
 91     }
 92     pushdown(rt,len);
 93     int m=(l+r)>>1;
 94     if(L<=m) update(L,R,val,lson);
 95     if(R>m) update(L,R,val,rson);
 96     pushup(rt);
 97 }
 98 ///////////////////////////////////////////////////////////////////////////
 99 
100 int main(){
101     std::ios::sync_with_stdio(false);
102     //freopen("in.txt","r",stdin);
103     //freopen("out.txt","w",stdout);
104     ///////////////////////////////////////////////////////////////////////
105     //Add Code:
106     int Case,n,q,x,y,z,i;
107     scanf("%d",&Case);
108     for(i=1;i<=Case;i++){
109         scanf("%d%d",&n,&q);
110         build(1,n,1);
111         while(q--){
112             scanf("%d%d%d",&x,&y,&z);
113             update(x,y,z,1,n,1);
114         }
115         printf("Case %d: The total value of the hook is %d.
",i,sum[1]);
116     }
117     ///////////////////////////////////////////////////////////////////////
118     return 0;
119 }
120 
121 /**************************************************************************
122 Testcase:
123 Input:
124 1
125 10
126 2
127 1 5 2
128 5 9 3
129 Output:
130 Case 1: The total value of the hook is 24.
131 **************************************************************************/
原文地址:https://www.cnblogs.com/linqiuwei/p/3356651.html