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 ...
随机推荐
- Objective-C description的用法
description类似于.net/java ToString()方法的用途. 假设有一个CTPerson类, - (NSString *)description { return @"d ...
- mongodb下载及安装配置教程【仅供参考】
1 下载 下载页面地址:https://www.mongodb.org/downloads 版本选择:电脑系统是64位的,所以我选择了 Windows 64-bit 2008 R2+ ,msi包 2 ...
- bc
调试脚本报错bc: command not found 原因是因为这个linux环境里没有安装计算器工具bc 用 apt-get install bc 或者 yum -y install bc 安装后 ...
- Codevs 4600 [NOI2015]程序自动分析
4600 [NOI2015]程序自动分析 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 在实现程序自动分析的过程中,常常需 ...
- 数位DP入门Ural1057
CF一战让我觉得很疲倦,所以今天感觉很慢. 昨天解D题时候,因为太累,根本连题目都没看,今天看了之后感觉不会做,听闻是数位DP问题. 有某神说过,DP的功力建立在刷过的题上,我真的毫无功力可言. 介绍 ...
- [翻译][MVC 5 + EF 6] 1:创建数据模型
原文:Getting Started with Entity Framework 6 Code First using MVC 5 1.新建MVC项目: 2.修改Views\Shared\_Layou ...
- C#实现记事本查找功能
private void button1_Click(object sender, EventArgs e) { if (!(String.IsNullOrEmpty(this.textBox1.Te ...
- 让你的 Node.js 应用跑得更快的 10 个技巧
Node.js 受益于它的事件驱动和异步的特征,已经很快了.但是,在现代网络中只是快是不行的.如果你打算用 Node.js 开发你的下一个Web 应用的话,那么你就应该无所不用其极,让你的应用更快,异 ...
- python学习笔记enumerate()与range(len)运用及赋值小计
#!/uer/bin/env python # _*_ coding: utf-8 _*_ #格式1 a = 'abc' for i in range(len(a)): print a[i],'(%d ...
- C# net部署图片分布式存储服务器的小案例
如果web服务用户多了,访问多了,用户上传的图片,文件等内容放在一块,想必服务器是承受不住的,这个时候,我们就需要考虑分布式存储的方法了. 如图所示:一个web服务器拖2个图片服务器 如何做到用户上传 ...