题目链接 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. 一个骚气的前端JS代码生成网站

    生成Javascript 颜文字代码 稍微试了试 原本的代码: alert("Hello, JavaScript") 转换后代码 ゚ω゚ノ= /`m´)ノ ~┻━┻ //*´∇`* ...

  2. ActiveMQ - 入门指南

    首先需要下载ActiveMQ,下面的链接给我们列出了所有版本: http://activemq.apache.org/download-archives.html 每个版本为不同的OS提供了链接: 公 ...

  3. Java - 关于扩展线程安全类的一些思考

    重用可以节省我们进行开发和测试(测试比我们自己测严谨地多)的时间和其他各种成本. 但是,对一个线程安全类进行扩展的时候就需要思考一些问题. 比如我们熟知的线程安全类Vector,该类中对所有的公有方法 ...

  4. 游标的小知识(转载and整理)

    一.游标(用来存储多条查询数据的一种数据结构(结果集),它有一个指针,用来从上往下移动,从而达到遍历每条记录的作用) 游标也可以理解为逐行返回SQL语句的结果集 如何编写一个游标? 1.声明游标 de ...

  5. 四层协议和Socket编程

    <四层协议图> <Soclet编程模型图>

  6. radio 实现点击两次 第一次点击选中第二次点击取消

    由于项目的需求,要求radio点击两次后为取消状态,不方便修改为checkbox,可以用正面的方法实现. // jquery $('input:radio').click(function(){ // ...

  7. Cocos2d-js 开发记录:基本图形绘制

    做着做着想要用基本绘图函数画个矩形,在cocos2d-js 3.0里可以使用DrawNode var dn = new cc.DrawNode(); var ltp = cc.p(0, 32); va ...

  8. centos install redis

    1. 下载 [logan@localhost java]$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz2. 解压    [log ...

  9. BZOJ1898: [Zjoi2005]Swamp 沼泽鳄鱼(矩阵快速幂)

    题意 题目链接 Sol 不难发现吃人鱼的运动每\(12s\)一个周期 所以暴力建12个矩阵,放在一起快速幂即可 最后余下的部分暴力乘 #include<bits/stdc++.h> usi ...

  10. 《火星救援》NASA惊现lisp

    duang-跳出个界面上面一个lisp程序.