Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26203    Accepted Submission(s): 13101

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.
 
第二道区间更新的题。
回顾了一下区间更新的要领,还不错总体算是自己完成的。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
#define N 200005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 struct Node1
{
int kind;
int sum;
int l,r;
} tree[N<<]; void pushup(int rt)
{
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
} void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].kind=;
if(l==r)
{
tree[rt].sum=;
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} void pushdown(int rt)
{
if(tree[rt].kind!=)
{
tree[rt<<].kind=tree[rt].kind;
tree[rt<<|].kind=tree[rt].kind;
tree[rt<<].sum=(tree[rt<<].r-tree[rt<<].l+)*tree[rt<<].kind;
tree[rt<<|].sum=(tree[rt<<|].r-tree[rt<<|].l+)*tree[rt<<|].kind;
tree[rt].kind=;
}
} void update(int x,int L,int R,int l,int r,int rt)
{
if(L==l&&r==R)
{
tree[rt].kind=x;
tree[rt].sum=(tree[rt].r-tree[rt].l+)*x;
return;
}
//cout<<"*"<<endl;
pushdown(rt);
int mid=(l+r)>>;
if(R<=mid)
update(x,L,R,lson);
else if(L>mid)
update(x,L,R,rson);
else
{
update(x,L,mid,lson);
update(x,mid+,R,rson);
}
pushup(rt);
} int main()
{
int t,n,q,cnt=;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
build(,n,);
scanf("%d",&q);
while(q--)
{
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
update(k,a,b,,n,);
}
printf("Case %d: The total value of the hook is %d.\n",cnt++,tree[].sum);
}
return ;
}

HDU_1698_Just a Hook_线段树区间更新的更多相关文章

  1. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  2. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  3. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  4. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  5. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  6. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  7. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  9. HDU5039--Hilarity DFS序+线段树区间更新 14年北京网络赛

    题意:n个点的树,每个条边权值为0或者1, q次操作 Q 路径边权抑或和为1的点对数, (u, v)(v, u)算2个. M i修改第i条边的权值 如果是0则变成1, 否则变成0 作法: 我们可以求出 ...

随机推荐

  1. Android自己定义控件系列案例【五】

    案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...

  2. mysql安装后改动port号password默认字符编码

    1.改动password grant all privileges on *.* to 'root'@'localhost' identified by 'new password'; 2.改动por ...

  3. opencv中RGB转HSV

    cvCvtColor(src,dst,CV_BGR2HSV); 当中,src为三通道的,dst也为三通道的. OPENCV 中 H.S.V.顺序分别为3*x+0  3*x+1   3*x+2 open ...

  4. 浅析js的函数的按值传递参数

    js的函数传参的方式是按值传递,正常情况下,改变函数参数的值,并不会对函数外部的变量造成影响.例如: 'use strict';var list = [1, 2, 3]; list.forEach(f ...

  5. 【OI】向量&矩阵乘法

    何为向量? 在初中课本中,我们知道: 向量是有大小和方向的量. 这样解释太笼统了,现在我们只讨论平面上的向量. 那么,我们约定:在平面上的向量,由一个二元组组成:如α(c1,c2). 在此平面上建立一 ...

  6. atoi函数——将字符串转换为整数

    atoi在一个叫<cstdlib>的库里,可以把字符串直接转换为整数,贼强势. 还有一个atof,就是换成浮点数,实质上是一样的. 例子: #include<cstdlib> ...

  7. html5中不再支持的元素

    html5中不再支持的元素:1.acronym(建议abbr) : 定义首字母缩写2.applet(建议object): 定义 applet3.basefont(使用css控制)4.big(使用css ...

  8. 《Typecript 入门教程》 3、接口

    转载:<TypeScript 中文入门教程> 3.接口 介绍 TypeScript的核心原则之一是对值所具有的shape进行类型检查. 它有时被称做“鸭式辨型法”或“结构性子类型化”. 在 ...

  9. 灾备还原之gitlab

    灾备还原之gitlab 备份情景:服务器A架设了gitlab,定期通过duplicity发送加密备份给B服务器,现在由于某种情况生产机器A完全无法访问(主机商跑路?硬盘冒烟?服务器BOOM了?),本地 ...

  10. EF--DBFirst

    EF框架有三种基本的方式:DB First,Model First,Code First.这里简单的说一下DB First,适合没有基础的同学照着做,学习基础的东西. DatabaseFirst就是围 ...