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

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

代码:

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; #define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 const int maxn=;
int n;
int s[maxn<<],sum[maxn<<]; void pushup(int rt) //
{
sum[rt] = sum[rt << ] + sum[rt << |];
} void pushdown(int rt,int m) //下标为n,这段区间有m个数
{
if( s[rt] ){
s[ rt<< ]=s[ rt<<| ]=s[ rt ];
sum[ rt<< ]=(m-m/)*s[ rt ];
sum[ rt<<| ]=(m/)*s[ rt ];
s[ rt ]=;
}
} void build( int l,int r,int rt ) //建树
{
sum[ rt ]=;
s[ rt ]=;
if( l==r ){
return ;
}
int mid;
mid=(l+r)>>;
build( lson );
build( rson );
pushup( rt ); } void update(int a,int b,int c,int l,int r,int rt) //更新
{
if(a<=l && b>=r){
s[ rt ]=c;
sum[ rt ]=(r-l+)*c;
return;
}
pushdown( rt,r-l+ );
int mid=(l+r)>>;
if( a<=mid )
update( a,b,c,lson );
if( b>mid )
update( a,b,c,rson );
pushup( rt );
return ;
} int query( int a,int b,int l,int r,int rt) //查询
{
if(a==l && b==r){
return sum[ rt ];
}
int mid=(l+r)>>;
int t1,t2;
if( b<=mid )
return query( a,b,lson );
else if( a>mid )
return query( a,b,rson );
else
return query( a,mid,lson )+query( mid+,b,rson );
} int main()
{
int t;
scanf("%d",&t);
for(int i=;i<=t;i++){
scanf("%d",&n);
build( ,n, );
int m;
scanf("%d",&m);
int a,b,c;
while(m--){
scanf("%d%d%d",&a,&b,&c);
update( a,b,c,,n, );
//for( int j=1;j<=25;j++ )printf("j:%d %d\n",j,sum[j]);
}
printf("Case %d: The total value of the hook is %d.\n",i,sum[ ]/*query( 1,n,1,n,1 )*/ );
}
return ; }

hdu Just a Hook的更多相关文章

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

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  3. HDU 1698 & UESTC 1228 Just a hook

    算是线段树中的一道水题了,必须用到懒操作,否则会超时.或者也可以刚开始不计算和,只更新节点,最后算整个线段的颜色和. 1.懒操作法 /* 908ms 3448KB in HDU OJ*/ #inclu ...

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

    题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少. 思路 : 线段树的区间更新.可以理解为线段树成段更新的模板 ...

  5. hdu 1689 Just a Hook

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    ...

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

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目: Problem Description   In the game of DotA, P ...

  7. (线段树)Just a Hook -- hdu -- 1689

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 思路: 我的想法很简单,像上一题一样从后面向前面来算,前面已经覆盖的,后面自然不能再来计算了,具体 ...

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

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  9. HDU 1698 Just a Hook(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意:给出1~n的数,每个数初始为1,每次改变[a,b]的值,最后求1~n的值之和. 思路: 区间更新题目 ...

随机推荐

  1. Java引用类型变量

    Java引用类型变量 1.编译时类型:由声明该变量时使用的类型决定 2.执行时类型:由实际赋给该变量的对象决定    类型不一致的假设编译时类型和执行,可能会出现多态性 版权声明:本文博主原创文章.博 ...

  2. linux根据该文件夹的读取权限和权限运行差异

    假设你linux下使用ls.细心的你会发现居然夹有权限运行.例如: drwxrwxr-x 11 cl cl 4096  9 25 14:22 ./ drwxr-xr-x 49 cl cl 4096 1 ...

  3. ListView装上拉电阻下拉刷新

    主要用到了这个几个文件.MainActivity是界面的Activity,MyAdapter是ListView的自己定义适配,MyListView是自己定义带头部LIistView,假设仅仅须要上拉载 ...

  4. 解决linux看温度是报错No sensors found问题

    今日因为同事的linuxserver频繁死机,其型号和配置与我的一致,故此与其对照了一下server的内存使用率.CPU使用率.温度等信息. 在对照温度信息的时候发现无法使用sensors进行温度查看 ...

  5. 编程算法 - 最小的k个数 红黑树 代码(C++)

    最小的k个数 红黑树 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入n个整数, 找出当中的最小k个数. 使用红黑树(multiset) ...

  6. ruby简单的基本 3

    类 Ruby一切都是对象,它包含了一个恒定.例如,可以使用.class物业查看对象的类型,你可以看一下1.class.你会发现常1类型是Fixnum,1但它是Fixnum的一个例子. Ruby本类cl ...

  7. Java经典23结构模型的设计模式(三)------附加代理模式、适配器型号、Facade模式的差异

    本文介绍了7样的结构模型中的其余2种:轻量级.代理模式. 一.享元模式FlyWeight 享元模式比較简单且重要,在非常多场合都被用到.仅仅只是封装起来了用户看不到.其概念:运用共享内存技术最大限度的 ...

  8. POJ 3356 AGTC(最长公共子)

    AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform  ...

  9. js的StringBuffer实施和使用类

    <strong>JAVA有一个StringBuffer分类,js但不是在下面,以实现自己的简单js的StringBuffer分类.</strong> //创建一个StringB ...

  10. Linux IO工具 iotop备择方案iopp

    iotop毫无疑问linux IO检测上是一个很好的工具,但苦于要求和内核版本Python版本号.我的很多朋友放弃了.我也是.无意中发现iopp,使用c书面,与此iotop它是一个作用.nice! 一 ...