转载请注明出处: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. 16进制的颜色怎么转换为我们iOS所用的RGB颜色

    这个我们就是可以直接宏定义到 我们的工程中,这样我们就可以用我们UI小妹,给我们16进制的颜色表示,愉快的编程了... #define UIColorFromRGB(rgbValue) [UIColo ...

  2. CTRL+A, CTRL+C, CTRL+V

    (http://leetcode.com/2011/01/ctrla-ctrlc-ctrlv.html) Imagine you have a special keyboard with the fo ...

  3. BZOJ 3916: [Baltic2014]friends( hash )

    字符串哈希..然后枚举每一位+各种判断就行了 ----------------------------------------------------------------------------- ...

  4. Spring Boot使用自定义的properties

    spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 若继续在application.properties中添加 如: ...

  5. config.json ajenti

    {    "users": {        "root": {            "configs": {               ...

  6. LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

    获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.L ...

  7. GDAL1.9.1 IN VS2008 C#中的编译及使用

    下载gdal1.9.1到官网:http://www.gdal.org/ GDAL库的简洁.高效深受开发人员的喜爱,很多开源的GIS软件甚至是商业GIS软件都使用了这个库.GDAL使用C++,在Visu ...

  8. 【剑指offer】调整数组顺序

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/25829395 剑指offer上的第14题,九度OJ为了确保输出的结果的唯一性,在输出上做了 ...

  9. packstack安装以及centos源配置注意事项

    On CentOS:安装分为四步: 1,$ sudo yum install -y centos-release-openstack-mitaka 2,$ sudo yum update -y 3,$ ...

  10. 杭电oj An easy problem

    </pre><h1 style="color: rgb(26, 92, 200);">An easy problem</h1><stron ...