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. 察看linux 发行版

    好像没有太通用的方法. 看一下/etc/redhat-release.  redhat 系列(包括centos) 会有如下内容 [root@localhost ~]# cat /etc/redhat- ...

  2. 1.4-动态路由协议OSPF⑤

    OSPF的特殊区域(Stub/total Stub区域,无法引入外部路由): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 第一种 ...

  3. [JavaEE] Implement a test for REST endpoint

    1. We have the BookEndpoint.java: package com.pluralsight.bookstore.rest; import com.pluralsight.boo ...

  4. VBS调用Windows API函数

    Demon's Blog 忘记了,喜欢一个人的感觉 Demon's Blog  »  程序设计  »  VBS调用Windows API函数 « 用VBS修改Windows用户密码 在VB中创建和使用 ...

  5. LeetCode 69. Sqrt(x) (平方根)

    Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...

  6. hdu 1671 Phone List 字典树

    // hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...

  7. hdoj--1205--吃糖果(规律)

     吃糖果 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Sub ...

  8. ::before和::after伪元素的使用

    :before和:after伪元素在CSS中可以被用来添加元素.加小标.清浮动等. 基本用法: p::before {} p::after {} 1.string <style type=&qu ...

  9. redhat6 改 centos yum 源

    **redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装其他yum源,再配置其他源.** 本文包括配置本地源及第三方源.第三方源包括:网 ...

  10. struts2基础学习----struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...