hdu Just a Hook
题目链接: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的更多相关文章
- hdu just a hook(线段树,区间修改)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...
- HDU 1698 & UESTC 1228 Just a hook
算是线段树中的一道水题了,必须用到懒操作,否则会超时.或者也可以刚开始不计算和,只更新节点,最后算整个线段的颜色和. 1.懒操作法 /* 908ms 3448KB in HDU OJ*/ #inclu ...
- HDU 1698 Just a Hook (线段树区间更新)
题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少. 思路 : 线段树的区间更新.可以理解为线段树成段更新的模板 ...
- hdu 1689 Just a Hook
http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 1698 Just a Hook(线段树成段更新)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目: Problem Description In the game of DotA, P ...
- (线段树)Just a Hook -- hdu -- 1689
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 思路: 我的想法很简单,像上一题一样从后面向前面来算,前面已经覆盖的,后面自然不能再来计算了,具体 ...
- 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 ...
- HDU 1698 Just a Hook(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意:给出1~n的数,每个数初始为1,每次改变[a,b]的值,最后求1~n的值之和. 思路: 区间更新题目 ...
随机推荐
- 在borland c++ builder 中使用 google test (gtest)
google test version: 1.6 c++ builder version: xe6 1 download google test 1.6 2 unzip the zip file. T ...
- 【Spring】Spring学习笔记-01-入门级实例
听说当前Spring框架很流行,我也准备好好学学Spring开发,并将学习的过程和大家分享,希望能对志同道合的同学有所帮助. 以下是我学习Spring的第一个样例. 1.Spring开发环境的搭建 我 ...
- ListView装上拉电阻下拉刷新
主要用到了这个几个文件.MainActivity是界面的Activity,MyAdapter是ListView的自己定义适配,MyListView是自己定义带头部LIistView,假设仅仅须要上拉载 ...
- linux基础知识1
1. 硬盘分区 分区类型:主分区,扩展分区.逻辑分区: 分区规则: 一个硬盘仅仅能有1到4个主分区: 一个硬盘仅仅能有1个扩展分区: 一个硬盘的主分区和扩展分区最多仅仅能有4个. 扩展分区仅仅能用来包 ...
- 得到JAVA项目根文件夹
获得的相对路径 说明:相对路径(这并不说明什么时候相对谁)可以通过以下来获得(无论是一般java项目或web工程) String path = System.getProperty("use ...
- Android利用网络编程HttpClient批量上传(一个)
请尊重他人的劳动成果.转载请注明出处:Android网络编程之使用HttpClient批量上传文件 我曾在<Android网络编程之使用HTTP訪问网络资源>一文中介绍过HttpCient ...
- Ext,保存输入记录,并会提示输入
Ext的ComboBox组件使用非常多,通过配置输入框可以被允许成为一个.并可以存储输入记录,记录提示的基础,然后进行再次进入,以提高的用户体验输入操作. 代码非常easy,基本操作,如以下: // ...
- 阿里云+wordpress
阿里云+wordpress搭建个人博客网站[小白专用的图文教程] [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源 ...
- 两次fclose引发的血案
代码本来在Windows上开发的,功能基本完毕迁移到Linux上,结果一跑,乱象重重.这里只列出两个. 一崩溃: /mnt/diskc/db/app/bin/mysqld: double free ...
- 如何使用 iOS 7 的 AVSpeechSynthesizer 国家有声读物(4)
控制:我们一定要学会控制 尤达大师(电影<星球大战>)有话:的关键在于控制.这本故事书是一个字一个字读出来,我愿意为它添加两个button,音调和语速,以便我们能够调整语音合成实时的时候. ...