(线段树)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 ...
随机推荐
- Ubuntu技巧之清理系统中无用的软件包
如果你频繁的在你的系统中安装/卸载,那么不时的清理一下你的系统是十分必要的. 在Ubuntu终端中执行如下命令: sudo apt-get autoremove 屏幕输出是这个样子的: Reading ...
- Containerpilot 配置文件 之 Jobs
ContainerPilot job是用户定义的进程和规则,用于何时执行它,如何进行健康检查,以及如何向Consul做广告. 这些规则旨在允许灵活性覆盖几乎可能要运行的任何类型的进程. 一些可能的jo ...
- Spring boot集成 MyBatis 通用Mapper
配置 POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- Appium学习路-安装篇
比较好的文章:http://www.15yan.com/story/4GbuTwXQKDU/ 官网资料:http://appium.io/slate/cn/v1.2.0/?python#appium ...
- 14.7.1&14.7.2
ArrayList <Object> list = new ArrayList<>(); //实例化ArrayList int i; int j; for(i = 1; i & ...
- ubuntu的文本界面修改字体大小
使用命令: dpkg-reconfigure console-setup
- zoj1109-Language of FatMouse 【字典树】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 Language of FatMouse Time Limit: 10 S ...
- Python staticmethod() 函数
Python staticmethod() 函数 Python 内置函数 python staticmethod 返回函数的静态方法. 该方法不强制要求传递参数,如下声明一个静态方法: class ...
- Bom对象介绍
1.windows对象 1.windows对象 1_1.alert:显示一段消息和确认按钮的弹出的警告框 我们平时用的alert的全称就是 window.alert("hahah" ...
- The valid characters are defined in RFC 7230 and RFC 3986
网上大都说什么发送格式与协议定义的不兼容,改tomcat版本或改编码之类的. 本人测试的时候换了个浏览器,不用IE就好了 如果坚持用ie,也有解决方式 @参考文章 成功的方法 在请求地址var url ...