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. iOS-开发日志-UITextView介绍

    UITextView 属性 1.     text: 设置textView中文本 _textView.text = @"Now is the time for all good develo ...

  2. String类与Date类的转换

    public class DateTest { public static void main(String[] args) throws ParseException { Date date = n ...

  3. Leetcode Count Prime

    Description: Count the number of prime numbers less than a non-negative number, n Hint: The number n ...

  4. apache配置文件中的项目

    对于每个配置项目,有几个要素: 首先是项目名称 其次是配置的语法 再次是配置的默认值 配置所处的配置文件的位置(分区) 配置所在的模块分区(和核心是否紧密) 配置项目所在的模块 所以对于每个配置项目, ...

  5. 【搭建开发环境】在 Windows XP 中参与开源项目,搭建 git 和 cygwin 开发环境

    引言 只有一台 Windows XP 家用机,却想在诸如 Git@OSC 之类的开源社区参与开发,本文提供一个入门级的开发环境搭建指引. 涉及工具:Eclipse,EGit,Cygwin. 欢迎来到 ...

  6. zlib1.2.8 编译小记

    官网下载:http://www.zlib.net/ 用vs命令行工具运行zlib-1.2.8\contrib\masmx86\bld_ml32.bat 用vs2012打开zlib-1.2.8\cont ...

  7. markdown与textile之间互相转换

    markdown与textile之间互相转换 redmine中默认使用的是textile那么从别的地方复制过来的markdown格式的内容需要进行转换 找到一款工具叫做pandoc http://jo ...

  8. html5有什么布局标签

    header h1 nav ul li a section(id) div h3 article figure img article h4 header time datetime='' body ...

  9. hadoop1中hdfs原理详解

    HDFS是Hadoop Distribute File System的简称,也是Hadoop的一个分布四文件系统 一.HDFS的主要设计理念 1.存储超大文件 这里的 “超大文件” 是指几百MB .G ...

  10. 跨线程操作UI控件

    写程序的时候经常会遇到跨线程访问控件的问题,看到不少人去设置Control.CheckForIllegalCrossThreadCalls = false;这句话是告诉编译器不要对跨线程访问作检查,可 ...