(线段树)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 ...
随机推荐
- C++ 0x std::async 的应用
#include <iostream> #include <thread> #include <mutex> #include <vector> #in ...
- oracle 网络环境配置
PLSQL Developer连接Oracle11g 64位数据库配置详解 最近换了台64bit的电脑,所以oracle数据库也跟着换成了64bit的,不过 问题也随之产生,由于plsql devel ...
- cmd enabledelayedexpansion
先来说说变量延迟扩展吧.当然,放狗一搜,就能看到满天飞的关于变量延迟扩展的文章,所以,我这里就简单介绍一下.先来看一段批处理: set str=test if %str%==test ( set st ...
- cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】
http://acm.uestc.edu.cn/#/problem/show/841 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others) Memory ...
- swift和OC - 拆分数组 和 拆分字符串
1. 拆分数组 /// 根据 数组 截取 指定个数返回 多个数组的集合 func splitArray( array: [Date], withSubSize subSize: Int) -> ...
- apache配置防盗链
1.确保apache已开启rewrite. 2.在.htaccess文件中添加如下: RewriteEngine On RewriteCond %{HTTP_REFERER} !^http://X ...
- Spring框架中的工厂(了解)
1. ApplicationContext接口 * 使用ApplicationContext工厂的接口,使用该接口可以获取到具体的Bean对象 * 该接口下有两个具体的实现类 * ClassPathX ...
- phpstorm2018.3的安装和激活和汉化
安装 第一步:解压并打开文件,运行安装程序,点击Next进入下一步, 第二步:选择软件安装目录,自定义选择安装根目录--> 注意!后面还需要找安装目录里的文件,所以记住安装到一个比较容易查看的目 ...
- loadrunner12-运行报错原因及解决办法整理集合
1.错误:已超过该load generator的CPU使用率80%: 答:机器内存过小,更换配置更好的机器来执行测试. 是因为虚机的内存过小,运行Controller需要消耗的CPU过高,超过了80% ...
- 2018年这些UI设计趋势正在流行,跟上必拿高薪!
数字设计领域和时尚圈是一样的,总会有各种各样的趋势让人眼花缭乱.无论是用户界面的视觉元素,还是用户体验的细节,总有许多值得说道的新玩法和新方向.就目前来看,UI设计的大趋势是更加大胆新颖的视觉设计,通 ...