hdu 1698 Just a Hook(线段树之 成段更新)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit:
32768/32768 K (Java/Others)
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents
the golden kind.
1
10
2
1 5 2
5 9 3
Case 1: The total value of the hook is 24.
#include<cstdio> #define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
const int N = 100010;
struct node
{
int l;
int r;
int sum;
int color;
}a[N<<2]; void PushUp(int root)
{
a[root].sum = a[root<<1].sum + a[root<<1|1].sum;
}
void PushDown(int len, int root)
{
if(a[root].color)
{
a[root<<1].color = a[root<<1|1].color = a[root].color;
a[root<<1].sum = (len - (len>>1)) * a[root].color;
a[root<<1|1].sum = (len>>1) * a[root].color;
a[root].color = 0;
}
}
void build_tree(int l, int r, int root)
{
a[root].l = l;
a[root].r = r;
a[root].color = 0; if(l == r)
{
a[root].sum = 1;
return ;
}
int mid = (l + r) >> 1;
build_tree(lson);
build_tree(rson);
PushUp(root);
}
void update(int l, int r, int root, int z)
{
if(l <= a[root].l && r >= a[root].r)
{
a[root].color = z;
a[root].sum = (a[root].r - a[root].l + 1) * z;
return;
}
PushDown(a[root].r - a[root].l + 1, root);
int mid = (a[root].l + a[root].r) >> 1;
if(l <= mid) update(l, r, root<<1, z);
if(r > mid) update(l, r, root<<1|1, z);
PushUp(root);
} int main()
{
int T, n, m, cas = 0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build_tree(1, n, 1);
scanf("%d",&m);
int x, y, z;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
update(x, y, 1, z);
}
int ans = a[1].sum;
printf("Case %d: The total value of the hook is %d.\n", ++cas, ans);
}
return 0;
}
hdu 1698 Just a Hook(线段树之 成段更新)的更多相关文章
- Codeforces295A - Greg and Array(线段树的成段更新)
题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...
- HDU 1698 Just a Hook(线段树成段更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 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(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- [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 , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook 线段树+lazy-target 区间刷新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 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 ...
随机推荐
- 几十篇GDI以及MFC自绘的文章
http://www.cnblogs.com/lidabo/category/434801.html
- haproxy redirect location和redirect prefix
<pre name="code" class="html">redirect location <loc> [code <code ...
- Struts2图片文件上传,判断图片格式和图片大小
1. 配置Struts2能够上传的最大文件大小 使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够 ...
- Swift - 环形进度条(UIActivityIndicatorView)的用法
Swift中,除了条形进度条外,还有环形进度条,效果图如下: 1,环形进度条的基本属性 (1)Style: Large White:比较大的白色环形进度条 White:白色环形进度条 Gray:灰色环 ...
- 计算VMT的长度
function GetVirtualMethodCount(AClass: TClass): Integer; begin Result := (PInteger(Integer(AClass) + ...
- Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot
Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot Lisp: Common Lisp, Racket, Clojure, E ...
- PhoneGap-----Contacts
Everything in the code!!! <!DOCTYPE html> <html> <head> <title>Contact Examp ...
- iPhone开发【一】从HelloWorld開始
转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8013553 作者:张燕广 从经典的HelloWorld開始踏入iPhon ...
- JavaFX游戏开发效率浅谈
声明: 本博客文章原创类别的均为个人原创,版权所有.转载请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http://www.wjfxgame.com. ...
- 解析android framework下利用app_process来调用java写的命令及示例
解析android framework下利用app_process来调用java写的命令及示例 在android SDK的framework/base/cmds目录下了,有不少目录,这些目的最终都是b ...