转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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.

题意:题意:有t组測试数据,n为钩子长度(1<=n<=100000),m为操作的次数。初始时,每一个钩子的价值为1,操作由三个数字组成x,y,z表示把区间[x,y]的钩子变成的价值变成z(1代表铜,2银,3金)。

代码例如以下:

//线段树功能:update:成段替换 (因为仅仅query一次总区间,所以能够直接输出1结点的信息)

#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
//lson和rson分辨表示结点的左儿子和右儿子
//rt表示当前子树的根(root),也就是当前所在的结点
const int maxn = 111111;
//maxn是题目给的最大区间,而节点数要开4倍,确切的来说节点数要开大于maxn的最小2x的两倍
int col[maxn<<2];
int sum[maxn<<2];
void PushUp(int rt) //把当前结点的信息更新到父结点
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m)//把当前结点的信息更新给儿子结点
{
if (col[rt])
{
col[rt<<1] = col[rt] ;
col[rt<<1|1] = col[rt];
sum[rt<<1] = col[rt] * (m - (m >> 1));
sum[rt<<1|1] = col[rt] * (m >> 1);
col[rt] = 0;
}
}
void build(int l,int r,int rt)
{
col[rt] = 0;
sum[rt] = 1;//初始化每一个节点为1
if (l == r)
{
//scanf("%lld",&sum[rt]);
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
col[rt] = c;
sum[rt] = c * (r - l + 1);
return ;
}
PushDown(rt , r - l + 1);
int m = (l + r) >> 1;
if (L <= m)
update(L , R , c , lson);
if (m < R)
update(L , R , c , rson);
PushUp(rt);
}
/*int query(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}
PushDown(rt , r - l + 1);
int m = (l + r) >> 1;
int ret = 0;
if (L <= m)
ret += query(L , R , lson);
if (m < R)
ret += query(L , R , rson);
return ret;
}*/
int main()
{
int N , Q ,T , K=0;
int a , b , c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&Q);//N为节点数
build(1 , N , 1);//建树
while (Q--)//Q为询问次数
{
/* char op[2];
int a , b , c;
scanf("%s",op);
if (op[0] == 'Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a , b , 1 , N , 1));
}
else
{
scanf("%d%d%d",&a,&b,&c);//c为区间a到b添加的值
update(a , b , c , 1 , N , 1);
}*/
scanf("%d%d%d",&a,&b,&c);//c为区间a到b的改变值
update(a , b , c , 1 , N , 1); }
printf("Case %d: The total value of the hook is %d.\n",++K,sum[1]);
}
return 0;
}

hdu1698 Just a Hook 线段树:成段替换,总区间求和的更多相关文章

  1. UESTC-1057 秋实大哥与花(线段树+成段加减+区间求和)

    秋实大哥与花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  2. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  3. HDU 1698 Just a Hook(线段树成段更新)

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

  4. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  5. hdu698 Just a Hook 线段树-成段更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 很简单的一个线段树的题目,每次更新采用lazy思想,这里我采用了增加一个变量z,z不等于0时其绝 ...

  6. POJ 3225.Help with Intervals-线段树(成段替换、区间异或、简单hash)

    POJ3225.Help with Intervals 这个题就是对区间的各种操作,感觉这道题写的一点意思都没有,写到后面都不想写了,而且更神奇的是,自己的编译器连结果都输不出来,但是交上就过了,也是 ...

  7. poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

    题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.&quo ...

  8. poj_3468线段树成段更新求区间和

    #include<iostream> #include<string.h> #include<cstdio> long long num[100010]; usin ...

  9. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  10. HDU-1698-Just a Hook-区间更新+线段树成段更新

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

随机推荐

  1. Unslider – 轻量的响应式 jQuery 内容轮播插件

    Unslider 是一款非常轻量的 jQuery 插件(压缩后不到3KB),能够用于任何 HTML 内容的滑动. 可以响应容器的大小变化,自动排布不用大小的滑块.可以通过整合 jQuery.event ...

  2. UVa1368/ZOJ3132 DNA Consensus String

    #include <stdio.h>#include <string.h> int main(){    int a[4][1000]; // A/C/G/T在每列中出现的次数 ...

  3. BZOJ 1211: [HNOI2004]树的计数( 组合数学 )

    知道prufer序列就能写...就是求个可重集的排列...先判掉奇怪的情况, 然后答案是(N-2)!/π(d[i]-1)! -------------------------------------- ...

  4. 微信 php 获取ticket

    <?phpheader('content-type:text/html; charset=utf8');define('TOKEN', 'youtoken'); // TOKENdefine(' ...

  5. R语言学习笔记之外部文件读取

    在win32位的系统下,RODBC包内的函数是可以直接运行的,但在win64位的系统则不支持! 1.读取外部文件read.table()---csv,txt,excel 最基本函数是read.tabl ...

  6. [转]如何理解c和c ++的复杂类型声明

    本文作者girlrong是网易广州社区的C语言版版主,这篇文章被选在精华区.很是不错,不敢独享!据说她乐于助人,虚心诚恳,颇受网友欢迎.只可惜现在已退隐江湖了.在最近学习C语言过程中,了解些前辈大牛的 ...

  7. [转]android Handler使用

    转 http://blog.csdn.net/new_abc/article/details/8184634 不过这个我看不懂 不知道为什么i的值可以接着增长... package com.examp ...

  8. nginx 重写 rewrite 基础及实例(转)

    nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f和!-f用来判断是否 ...

  9. HDU3966(树链剖分)

    题目:Aragorn's Story 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上的所有点权值加上K D C1 C2 K:把C1与C2的路径上的所有 ...

  10. python2.6升级到2.7

    开发部需要使用python2.7,由于公网的环境python版本都是系统自带的,版本是2.6,需要升级,最好是通过RPM升级. Linux系统:CentOS CentOS 6.4升级Python后yu ...