码迷,mamicode.com
首页 > 其他好文 > 详细

hdu Just a Hook

时间:2015-11-26 21:22:45      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

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

 

线段树+lazy操作     线段树是从上到下开始建树,树状数组是从下到上建树....

 

代码:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <math.h>
  4 #include <algorithm>
  5 #include <iostream>
  6 #include <ctype.h>
  7 #include <iomanip>
  8 #include <queue>
  9 #include <stdlib.h>
 10 using namespace std;
 11 
 12 #define lson l,mid,rt<<1
 13 #define rson mid+1,r,rt<<1|1 
 14 
 15 const int maxn=100100;
 16 int n;
 17 int s[maxn<<2],sum[maxn<<2];
 18 
 19 void pushup(int rt)   //
 20 {
 21     sum[rt] = sum[rt << 1] + sum[rt << 1|1];
 22 }
 23 
 24 void pushdown(int rt,int m)   //下标为n,这段区间有m个数
 25 {
 26     if( s[rt] ){
 27         s[ rt<<1 ]=s[ rt<<1|1 ]=s[ rt ];
 28         sum[ rt<<1 ]=(m-m/2)*s[ rt ];
 29         sum[ rt<<1|1 ]=(m/2)*s[ rt ];
 30         s[ rt ]=0;
 31     }
 32 }
 33 
 34 void build( int l,int r,int rt )   //建树
 35 {
 36      sum[ rt ]=1;
 37      s[ rt ]=0;
 38      if( l==r ){
 39          return ;
 40      }
 41      int mid;
 42      mid=(l+r)>>1;
 43      build( lson );
 44      build( rson );
 45      pushup( rt );
 46      
 47  }
 48 
 49 
 50 void update(int a,int b,int c,int l,int r,int rt)  //更新
 51 {
 52     if(a<=l && b>=r){
 53         s[ rt ]=c;
 54         sum[ rt ]=(r-l+1)*c;
 55         return;
 56     }
 57     pushdown( rt,r-l+1 );
 58     int mid=(l+r)>>1;
 59     if( a<=mid )
 60         update( a,b,c,lson );
 61     if( b>mid )
 62         update( a,b,c,rson );
 63     pushup( rt );
 64     return ;
 65 }
 66 
 67 int query( int a,int b,int l,int r,int rt)  //查询
 68 {
 69     if(a==l && b==r){
 70         return sum[ rt ];
 71     }
 72     int mid=(l+r)>>1;
 73     int t1,t2;
 74     if( b<=mid )
 75         return query( a,b,lson );
 76     else if( a>mid )
 77         return query( a,b,rson );
 78     else
 79         return query( a,mid,lson )+query( mid+1,b,rson );
 80 }
 81 
 82 int main()
 83 {
 84     int t;
 85     scanf("%d",&t);
 86     for(int i=1;i<=t;i++){
 87         scanf("%d",&n);
 88         build( 1,n,1 );
 89         int m;
 90         scanf("%d",&m);
 91         int a,b,c;
 92         while(m--){
 93              scanf("%d%d%d",&a,&b,&c);
 94              update( a,b,c,1,n,1 );
 95              //for( int j=1;j<=25;j++ )printf("j:%d  %d\n",j,sum[j]);
 96          }
 97          printf("Case %d: The total value of the hook is %d.\n",i,sum[ 1 ]/*query( 1,n,1,n,1 )*/ );
 98      }
 99      return 0;
100 
101 }

 

hdu Just a Hook

标签:

原文地址:http://www.cnblogs.com/wangmengmeng/p/4998784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!