HDU 1698 Just a Hook(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698
题意:
给出1~n的数,每个数初始为1,每次改变[a,b]的值,最后求1~n的值之和。
思路:
区间更新题目,关于懒惰标记什么的参考这个吧http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html,讲得不错的。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ; int n, m; int col[maxn << ]; struct node
{
int l, r;
int n;
}t[maxn << ]; //如果该区间被标记了,说明该区间值改变,需要向下传递
void PushDown(int o, int m)
{
if (col[o])
{
col[o << ] = col[o << | ] = col[o];
t[o << ].n = (m - (m >> ))*col[o];
t[o << | ].n = (m >> )*col[o];
col[o] = ;
}
} void PushUp(int o)
{
t[o].n = t[o << ].n + t[o << | ].n;
} void build(int l, int r, int o)
{
col[o] = ;
t[o].l = l;
t[o].r = r;
t[o].n = ;
if (l == r) return;
int mid = (l + r) / ;
build(l, mid, * o);
build(mid + , r, * o + );
} void update(int l, int r, int x, int o)
{
if (l <= t[o].l && r >= t[o].r)
{
t[o].n = x*(t[o].r - t[o].l + );
col[o] = x;
return;
}
PushDown(o, t[o].r - t[o].l +);
int mid = (t[o].l + t[o].r) / ;
if (l > mid) update(l, r, x, * o + );
else if (r <= mid) update(l, r,x, * o);
else
{
update(l, mid, x, * o);
update(mid + , r, x, * o + );
}
//向上传递
PushUp(o);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int T;
int x, y, k;
int kase = ;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
scanf("%d", &m);
build(, n, );
for (int i = ; i <= m; i++)
{
scanf("%d%d%d", &x, &y, &k);
update(x, y, k, );
}
printf("Case %d: The total value of the hook is %d.\n", kase++, t[].n);
}
}
HDU 1698 Just a Hook(线段树:区间更新)的更多相关文章
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
随机推荐
- navicat 激活流程
Navicat Premium 12激活 我自己测试了一下可以激活,很好用 原作链接:https://blog.csdn.net/loveer0/article/details/82016644 Na ...
- C++ 初始化函数的实现
http://www.cppblog.com/xlshcn/archive/2007/11/21/37088.aspx
- 【BZOJ2957】楼房重建 分块
[BZOJ2957]楼房重建 Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子 ...
- java如何随机生成定长的字符串
小数,字符串.时间等示例代码 String base = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 public c ...
- 阅读笔记:Solving the “false positives” problem in fraud prediction
刚读完一篇paper<Solving the “false positives” problem in fraud prediction>,趁热打铁,做个笔记. 文章下载链接:https: ...
- 善用缓存提高你的Spring工程效率
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- 浅谈Lambda表达式详解
lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,“转到”或者 “成为”.运算符将表达式分为两部分,左边指定 ...
- oracle之用户名密码包含特殊字符时候怎么使用sqlplus登录
oracle有时候用户密码包含一些特殊字符直接登录会报错,需要使用以下方式登录sqlplus sqlplus 'username/"password"' PS:整体使用单引号括起来 ...
- 解决“The remote certificate is invalid according to the validation procedure”问题
在用HttpClient发起https请求时,遭遇了“The remote certificate is invalid according to the validation procedure”异 ...
- angular-translate国际化
1.<h1>{{"hello" | translate}}</h1>2.<h1 ng-bind-html="'hello' | transl ...