题目链接 HDU 1698

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.

大概题意:

一条长度为N得链子, 有Q次操作,每次操作把区间 X~Y 的金属换成 Z;有金,银,铜三种金属分别对应3,2,1三种价值。

求Q次操作后,求整条链子的总价值。

思路:

成段更新,区间求和,本次用一维数组表示法建线段树

AC code:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, k<<1 ///左儿子
#define rson m+1, r, k<<1|1 ///右儿子
using namespace std; const int MAXN = ; int st[MAXN<<]; ///st数组里面记录的是标记 0,1, 2, 3; 0 代表杂色
int flag; void down(int &k)
{
st[k<<] = st[k<<|] = st[k]; ///左儿子和右儿子均设为该颜色
st[k] = ; ///设为杂色 ??避免后面重复操作??
} void build(int l, int r, int k) ///建立线段树,一开始都初始化为 1
{
st[k] = flag;
if(l == r) return;
int m = (l+r)>>;
build(lson);
build(rson);
} void update(int &L, int &R, int l, int r, int k) ///更新L--R,要从1--N, st[1] 开始
{
if(L <= l && R >= r) ///所在区间在更新区间内
{
st[k] = flag; ///整段更新为新颜色
return;
}
if(st[k]) ///如果该结点为纯色,则左右儿子颜色也为该颜色
{
down(k);
}
int m = (l+r)>>;
if(L <= m) update(L, R, lson);
if(R > m) update(L, R, rson);
} int query(int l, int r, int k) ///查询区间颜色之和
{
if(st[k]) return st[k]*(r-l+); ///如果该段为纯色,则成段想乘得结果 int m = (l+r)>>, t1, t2;
t1 = query(lson);
t2 = query(rson);
return t1+t2;
} int main()
{
int T, N, t = ;
int L, R, q;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &q);
flag = ;
build(, N, ); ///建立线段树
while(q--)
{
scanf("%d%d%d", &L, &R, &flag);
update(L, R, , N, );
}
printf("Case %d: The total value of the hook is %d.\n", t++, query(, N, ));
}
return ;
}

HDU 1698 【线段树,区间修改 + 维护区间和】的更多相关文章

  1. HDU 1754 I Hate It 【线段树单点修改 维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others ...

  2. HDU - 1754 线段树-单点修改+询问区间最大值

    这个也是线段树的经验问题,待修改的,动态询问区间的最大值,只需要每次更新的时候,去把利用子节点的信息进行修改即可以. 注意更新的时候区间的选择,需要对区间进行二分. #include<iostr ...

  3. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

  4. hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  6. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  7. HDU 1698 <线段树,区间set>

    题目连接 题意: 一条长为N的铜链子,每个结点的价值为1.有两种修改,l,r,z; z=2:表示把[l,r]区间内链子改为银质,价值为2. z=3:表示把[l,r]区间内链子改为金质,价值为3. 思路 ...

  8. HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  9. HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. MVVM 事件转命令1

    EventToCommand 在WPF中,并不是所有控件都有Command,例如TextBox,那么当文本改变,我们需要处理一些逻辑,这些逻辑在ViewModel 中,没有Command如何绑定呢?这 ...

  2. easyui焦点离开事件的解决方案

  3. js/jq和a标签(刷新/ajax/对话框/循环/select选中/checkbox选中/id的获取//数据处理成钱的格式)//js/jq分页

    1.刷新 <a href="javascript:history.go(-1)">返回上一页</a><a href="javascript: ...

  4. .net IoC 之 Spring.Net 适合刚开始使用

    Spring.Net包括控制反转(IoC) 和面向切面(AOP),这篇文章主要说下IoC方面的入门. 一.首先建立一个MVC项目名称叫SpringDemo,然后用NuGet下载spring(我用的是S ...

  5. Hadoop 完全分布式部署(三节点)

    用来测试,我在VMware下用Centos7搭起一个三节点的Hadoop完全分布式集群.其中NameNode和DataNode在同一台机器上,如果有条件建议大家把NameNode单独放在一台机器上,因 ...

  6. CSS之after与before的content 和 attr 配合使用

    content 和 attr 配合使用 如果你不想把content内容在CSS里写死,那你可以使用attr表达式来从页面元素中动态的获取内容: /* <div data-line="1 ...

  7. Python入门-内置函数一

    什么是内置函数?就是python给你提供的拿来直接用的函数,比如print,input等等,截止到python版本3.6.2 python一共提供了68个内置函数,他们就是python直接提供给我们的 ...

  8. 新项目放到jenkins步骤

      1配置playbook.xml,src和dest需要和运维确认 2项目内config 文件夹下index文件内,build对象内assetsPublicPath属性是否需要更改.   playbo ...

  9. IntelliJ IDEA开发工具println报错的解决方法

    IntelliJ IDEA 编译 JSP,出现 out.println 报错,下图所示: 报错原因:println报红,这是因为没有关联好服务器! 解决方案:点击File->Project st ...

  10. 查看postgre都有哪些语句占用CPU

    查看占用CPU最多的几个postgresql ps aux | grep postgres | sort -n -r -k 3 | head -10 | awk '{print $2, $3}' 查看 ...