(线段树)Just a Hook -- hdu -- 1689
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1698
思路:
我的想法很简单,像上一题一样从后面向前面来算,前面已经覆盖的,后面自然不能再来计算了,具体是每次都计算覆盖的总长度,然后用这次的总长度减上次的总长度,自然就得到了这次覆盖的长度,可能我的方法不是很好吧!不过这是自己想出来的,再去借鉴一下别人的代码,多种想法是好的,毕竟是自己做的,因为数据不是很大所以不用离散化就可以
代码:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = ; struct Node
{
int L, R, e;
} s[N<<]; struct node
{
int L, R;
int len; //len里面要记录被覆盖的长度
int Mid()
{
return (L+R)>>;
}
} a[N<<]; void UpDate(int r)
{
if(a[r].L != a[r].R)
a[r].len = a[Lson].len + a[Rson].len;
} void BuildTree(int r, int L, int R)
{
a[r].L = L, a[r].R = R;
a[r].len = ; if(L==R) return ; BuildTree(Lson, L, a[r].Mid());
BuildTree(Rson, a[r].Mid()+, R);
} int Insert(int r, int L, int R)
{
if(a[r].L<=L && a[r].R>=R && a[r].len == a[r].R-a[r].L+)
return a[r].len;
if(a[r].L==L && a[r].R==R)
{
a[r].len = R-L+;
return a[r].len;
} if(R<=a[r].Mid())
Insert(Lson, L, R);
else if(L>a[r].Mid())
Insert(Rson, L, R);
else
{
Insert(Lson, L, a[r].Mid());
Insert(Rson, a[r].Mid()+, R);
} UpDate(r); return a[r].len;
} int main()
{
int t, k=;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d%d", &n, &m); BuildTree(, , n); for(int i=; i<=m+; i++)
scanf("%d%d%d", &s[i].L, &s[i].R, &s[i].e); s[].L = , s[].R = n, s[].e = ; int sum = , z=, w=;
for(int i=m+; i>; i--)
{
w = z;
z = Insert(, s[i].L, s[i].R); sum += (z-w)*s[i].e; if(z==n)
break;
} printf("Case %d: The total value of the hook is %d.\n", k++, sum); }
return ;
}
代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Lson root<<1,L,tree[root].Mid()
#define Rson root<<1|1,tree[root].Mid()+1,R const int maxn = ; struct Hook{int l, r, z;}hook[maxn];
struct Tree{
int L, R;
int isCover;//等于1的时候被覆盖,等于2的时候区间下面有被覆盖的
int Mid(){return (L+R)/;}
int Len(){return (R-L+);}
}tree[maxn*]; void Build(int root, int L, int R)
{
tree[root].L = L, tree[root].R = R;
tree[root].isCover = false; if(L == R)return ; Build(Lson);
Build(Rson);
}
void Up(int root)
{
if(tree[root].L != tree[root].R)
if(tree[root<<].isCover == && tree[root<<|].isCover == )
tree[root].isCover = ;
}
int Insert(int root, int L, int R)
{
if(tree[root].isCover == )return ; if(tree[root].L == L && tree[root].R == R && !tree[root].isCover)
{
tree[root].isCover = ;
return tree[root].Len();
}
tree[root].isCover = ; int ans = ; if(R <= tree[root].Mid())
ans = Insert(root<<, L, R);
else if(L > tree[root].Mid())
ans = Insert(root<<|, L, R);
else
ans = Insert(Lson)+Insert(Rson); Up(root); return ans;
} int main()
{
int i, T, t=; scanf("%d", &T); while(T--)
{
int N, M; scanf("%d%d", &N, &M);
Build(, , N); for(i=; i<=M; i++)
scanf("%d%d%d", &hook[i].l, &hook[i].r, &hook[i].z); int ans = N; for(i=M; i>; i--)
{
int len = Insert(, hook[i].l, hook[i].r);
ans = ans - len + len * hook[i].z;
} printf("Case %d: The total value of the hook is %d.\n", t++, ans);
} return ; }
(线段树)Just a Hook -- hdu -- 1689的更多相关文章
- 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543
学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...
- 线段树练习[单点更新] HDU 2795 Billboard
题目大意:有一个h*w的公告榜,可以依次在上面添加信息.每个信息的长度为x,高为1. 优先在最上面加入,如果空间足够的话,然后优先放在最左面.统计每条公告最终的位置,即它所在的行数. 这里是线段树来存 ...
- HDU 1754 线段树 单点跟新 HDU 1166 敌兵布阵 线段树 区间求和
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 线段树->面积并 Atlantis HDU - 1542
题目链接:https://cn.vjudge.net/problem/HDU-1542 题目大意:求面积并 具体思路:我们首先把矩形分割成一横条一横条的,然后对于每一个我们给定的矩形,我们将储存两个点 ...
- 线段树 + 区间更新: HDU 4893 Wow! Such Sequence!
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 线段树 面积并问题 hdu 1255 1542
重点整理面积并的思想 以及PushUp的及时更新 还有就是cover的实现 以及建树每个节点存的信息(每个节点存的是一个线段的信息) http://www.tuicool.com/articles/6 ...
- 线段树 [成段更新] HDU 1698 Just a Hook
成段更新,需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候. 此处建议在纸上模拟一遍. Problem Descript ...
- 线段树(区间维护):HDU 3308 LCIS
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 线段树(维护最大值):HDU Billboard
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- unity 设置屏幕旋转
只允许竖屏: Portrait √ Portrait Upside Down √ Landscape Right × Landscape Left ...
- ES5之defineProperty
一 概述 Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象. 对象里目前存在的属性描述符有两种主要形式:数据描述符和存 ...
- SSH框架分模块开发
------------------siwuxie095 SSH 框架分模块开发 1.在 Spring 核心配置文件中配置多个内容,容易造成 配置混乱,不利于维护 「分模块开发主要针对 Spring ...
- TZOJ 4839 麦森数(模拟快速幂)
描述 形如2^P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^P-1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=3021377,它有9 ...
- linux中使用locate搜索文件方法记录
在linux中,有时用apt或者yum等软件包管理工具直接安装软件的时候,不知道软件到底安装到哪里去了,配置文件放哪里?这个时候就可以使用搜索命令locate来找到这些文件.海词上locate翻译为找 ...
- My97DatePicker 日历控件
My97DatePicker 是一款非常强大的日历控件,使用也非常简单,也能修改源码,牛逼我就不吹了,自己用用看 使用 1.引入 <script language="javascrip ...
- [udemy]WebDevelopment_History of The Web
WWW vs Internet For the begining, Internet was there. it was for the academics among universities Th ...
- linux整合apache、php、mysql
1.打开apache配置文件,添加AddType.找到DirectoryIndex并添加index.php AddType application/x-httpd-php .php AddType a ...
- JsonConvert.SerializeObject 空值处理
var settings = new JsonSerializerSettings() { ContractResolver= new NullToEmptyStringResolver() }; v ...
- springmvc与struts2的不同
1.springmv的入口是一个servlet,即前端控制器.而struts2入口是一个fliter过滤器. 2.springmvc是基于开发方法(一个url对应一个方法,通过注解的方式进行访问),请 ...