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

Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

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.

 
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
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.
 
Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
 
Sample Input
1
10
2
1 5 2
5 9 3
 
Sample Output
Case 1: The total value of the hook is 24.

思路:线段树,设计节点的时候不用记录该节点区间里面的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的更多相关文章

  1. hdoj 1698 Just a Hook 【线段树 区间更新】

    题目大意:有一段链子.初始的时候是铜的(价值为1),n代表有n段(1~n),输入a, b, c三个数分别表示将从a到b的链子的价值改为c, 最后问你经过多次改变之后的总价值. 策略:这道题是简单的线段 ...

  2. hdoj 1698 Just a Hook【线段树区间修改】

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDOJ 1698 Just a Hook (线段树)

    题目: Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f ...

  4. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  6. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  9. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  10. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

随机推荐

  1. OpenJudge/Poj 1657 Distance on Chessboard

    1.链接地址: http://bailian.openjudge.cn/practice/1657 http://poj.org/problem?id=1657 2.题目: 总时间限制: 1000ms ...

  2. jQuery选择器(一)

    1.#id根据给定的ID匹配一个元素. <div id="notMe"><p>id="notMe"</p></div& ...

  3. javascript 写策略模式,商场收银打折优惠策略

    [Decode error - output not utf-8] ----------------------------- 购物清单 方便面 : 100 x 50 = 5000 | 4000 菊花 ...

  4. PHP对URL设置

    一.URL规则  1.默认是区分大小写的  2.如果我们不想区分大小写可以改配置文件   'URL_CASE_INSENSITIVE'=>true,               //url不区分 ...

  5. Java RMI(远程方法调用)开发

    参考 https://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmi-arch2.html http://www.cnblogs.com/wxi ...

  6. 截获控制台程序关闭事件(SetConsoleCtrlHandler)

    最近控制台程序中需要捕获控制台关闭事件,在用户关闭的时候进行某些操作,找了一大圈发现了一个方法,通过调用WIN32 API SetConsoleCtrlHandler方法来实现,具体代码如下: usi ...

  7. 前端工程的构建工具对比 Gulp vs Grunt

    1. Grunt -> Gulp 早些年提到构建工具,难免会让人联想到历史比较悠久的Make,Ant,以及后来为了更方便的构建结构类似的Java项目而出现的Maven.Node催生了一批自动化工 ...

  8. postgres 约束 多个条件 联合 约束

    ADD CONSTRAINT xxx CHECK ( (col1 = 0.0) = (col2 IS NOT NULL)); ## 相当于check (true = ture)

  9. 【C语言】37个关键字

    C语言37个关键字 一.相关基础知识 年. 关键字:是由系统定义的,不能重新做其他定义的字符,且每个关键字已经赋予了不同的意义,让编程者能够使用来告诉编译器完成不同的工作PS:C语言严格区分大小写,i ...

  10. Android中的pix,sp,dp相关概念

    px( pixel) 像素,可以简单的理解为一个点或方块,用以颜色的显示(单位),一般指印刷品或屏幕设置设备的颜色显示定义. dip(device independent pixels)设备独立像素. ...