HDOJ--1698
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14516 Accepted Submission(s): 7176
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.
10
2
1 5 2
5 9 3
思路:线段树,设计节点的时候不用记录该节点区间里面的sum,只需要记录每个区间里面的kind就行,(sum=R-L+1)*kind),这样更新的时候就看当前节点区间和插入的区间[L,R]的关系:1,如果当前节点区间的kind和插入区间的kind相同就直接结束UpdateTree()函数,不需要处理;2,否则,如果当前节点区间与插入区间正好匹配,把那么就直接把当前节点区间的kind置为插入区间的kind,结束UpdateTree()函数;3,如果上述两条均不满足,则:(1)如果当前节点区间是纯色的(就是只有一种kind),那么该节点区间就变成不是纯色的了,(不止一种kind),那么就将该节点区间的kind变成0,同时注意更新其子区间的kind,因为我们并没有处理当前节点区间,而是把它转移到其子区间了。然后我们应该找该节点区间的子区间,直到满足1和2的条件(即是找到纯色区间或找到平匹配的区间)。(2)如果当前节点区间不是纯色那么就递归寻找其子区间,直到满足1,2条件。
/*************************************************************************
> File Name: Hook.cpp
> Author: wangzhili
> Mail: wangstdio.h@gmail.com
> Created Time: 2014/2/27 星期四 12:40:45
************************************************************************/
#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 100000
class Tree_Node
{
public:
int left;
int right;
int mid;
int kind;
Tree_Node()
{
kind = ;
}
};
Tree_Node node[*MAX];
void BuildTree(int k, int l, int r)
{
node[k].left = l;
node[k].right = r;
node[k].kind = ;
node[k].mid = (l + r) >> ;
if(l == r)
return ;
int mid = (l + r) >> ;
BuildTree(k << , l, mid);
BuildTree(k << |, mid + , r);
} void UpdateTree(int k, int l, int r, int kind)
{
if(node[k].left == l && node[k].right == r)
{
node[k].kind = kind;
return ;
}
if(node[k].kind == kind)
return ;
if(node[k].kind)
{
node[k << ].kind = node[k].kind;
node[k << |].kind = node[k].kind;
}
node[k].kind = ;
if(node[k].mid < l)
UpdateTree(k << |, l, r, kind);
else if(node[k].mid >= r)
UpdateTree(k << , l, r, kind);
else
{
UpdateTree(k << , l, node[k].mid, kind);
UpdateTree(k << |, node[k].mid + , r,kind);
}
} int GetSum(int k)
{
if(node[k].kind)
return (node[k].right-node[k].left + ) * node[k].kind;
return GetSum(k << ) + GetSum(k << |);
} int main(int argc, char const *argv[])
{
int cnt, T, n, Q, i;
int l, r, kind;
cnt = ;
// freopen("in.c", "r", stdin);
scanf("%d", &T);
while(T--)
{
cnt ++;
scanf("%d%d", &n, &Q);
BuildTree(, , n);
for(i = ; i < Q; i ++)
{
scanf("%d%d%d", &l, &r, &kind);
UpdateTree(, l, r, kind);
}
printf("Case %d: The total value of the hook is %d.\n",cnt, GetSum());
}
return ;
}
HDOJ--1698的更多相关文章
- hdoj 1698 Just a Hook 【线段树 区间更新】
题目大意:有一段链子.初始的时候是铜的(价值为1),n代表有n段(1~n),输入a, b, c三个数分别表示将从a到b的链子的价值改为c, 最后问你经过多次改变之后的总价值. 策略:这道题是简单的线段 ...
- hdoj 1698 Just a Hook【线段树区间修改】
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDOJ 1698 Just a Hook (线段树)
题目: Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdoj 1385Minimum Transport Cost
卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...
- HDOJ(2056)&HDOJ(1086)
Rectangles HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...
- 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ
前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...
随机推荐
- SqlServer Alter Table 语句的用法
更改 字段的数据类型 Alter Table TB_ITM_ITEM alter column is_timing int NULL; 新增字段: alter table WeiboSmartCate ...
- Struts2的运行原理和运行与原理
Struts2 struts2的流程图 运行机制 1.客户端发送请求.通过ActionContextLoader调用FilterDispatcher(struts) 2.FilterDispatche ...
- HDU 2809 God of War(DP + 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 题目大意:给出战神吕布的初始攻击力ATI.防御力DEF.生命值HP.每升一级增加的攻击力In_A ...
- References & the Copy-Constructor
1 There are certain rules when using references: (Page 451) A reference must be initialized when it ...
- bootstrap实现手风琴功能(树形列表)
首先把架包拷进项目,然后在页面中引进css,js <script src="js/jquery/jquery-2.1.1.min.js"></script> ...
- ASP.NET 优化 check list
看到一个蛮有意思的网站,里面有针对asp.net方方面面优化的罗列: 点击打开链接http://webdevchecklist.com/asp.net/performance/ 点击打开链接http: ...
- 身份证js验证
<script type="text/javascript"> //--身份证号码验证-支持新的带x身份证 function isIdCardNo(num) { var ...
- javascript 对象的创建,引用,释放,删除方法
1.用函数构造 A.声明时同时设置属性和方法 function func(){ this.name = "myname"; this.say = function(){aler ...
- jQuery移除指定元素后的所有元素
jQuery 遍历的nextAll() 方法可以搜索 DOM 树中的元素跟随的同胞元素,也就是一个元素后面的所有同级元素,删除可以使用方法remove(),所以连起来为 $(selector).nex ...
- linux压缩与解压缩 tar命令
#压缩tar -czvf ***.tar.gz filetar -cjvf ***.tar.bz2 file#解压缩tar -xzvf ***.tar.gz filetar -xjvf ***.ta ...