hdu Just a Hook

题目链接: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 }
时间: 2024-12-24 14:53:44

hdu Just a Hook的相关文章

hdu just a hook(线段树,区间修改)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24102    Accepted Submission(s): 12063 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing

Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int N=1e5+10; struct node{ int l,r; int sum; int add; }tr[N*4]; void pushdown(int root) { if (tr[root].add)

HDU 1398:Just a Hook(线段树区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of

hdu 1698 Just a Hook 线段树区间更新

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks

HDU 1698 just a hook 线段树,区间定值,求和

Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1698 Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several cons

hdu 1698 Just a Hook 基本线段树

使用线段树更新每段区间的奖(1,2,3),最后在统计整段区间的数和,基本线段树,果断1A啊 #include<iostream> #include<stdio.h> using namespace std; #define N 100000 struct node{ int l,r,p; }a[N*4]; int n; void build(int left,int right,int i){ a[i].l=left; a[i].r=right; a[i].p=1; if(a[i]

线段树(成段更新) HDU 1698 Just a Hook

题目传送门 1 /* 2 线段树-成段更新:第一题!只要更新区间,输出总长度就行了 3 虽然是超级裸题,但是用自己的风格写出来,还是很开心的:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <iostream> 11 using namesp

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU 1698 Just a Hook(线段树成段更新)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目: Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the