Just a Hook

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

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.
/*
hdu1698 线段树区间更新
更新到指定区间时打一个标记 ,然后用push_up,push_down对标记进行处理即可
hhh-2016-03-04 20:29:58
*/
#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
#include <vector>
#include <functional>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
typedef long long ll;
const int maxn = 150050;
const int inf = 0x3f3f3f3f; struct node
{
int val,sum;
int l,r;
int mid()
{
return (l+r)>>1;
}
} tree[maxn<<2]; void push_up(int i)
{
tree[i].sum = tree[lson].sum + tree[rson].sum;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].val = 0;
tree[i].sum = 1;
if(l == r)
{
return ;
}
build(lson,l,tree[i].mid());
build(rson,tree[i].mid()+1,r);
push_up(i);
} void push_down(int i)
{
if(tree[i].val)
{
tree[lson].val = tree[i].val;
tree[rson].val = tree[i].val;
tree[lson].sum = tree[i].val*(tree[lson].r-tree[lson].l+1);
tree[rson].sum = tree[i].val*(tree[rson].r-tree[rson].l+1);
tree[i].val = 0;
}
} void update(int i,int l,int r,int v)
{
if(tree[i].l >= l && tree[i].r <= r)
{
tree[i].val = v;
tree[i].sum = v*(tree[i].r-tree[i].l+1);
return ;
}
push_down(i);
if(l <= tree[i].mid())
update(lson,l,r,v);
if(r > tree[i].mid())
update(rson,l,r,v);
push_up(i);
} int main()
{
int T,n,q;
int cas = 1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
scanf("%d",&q);
build(1,1,n);
for(int i =1; i <= q; i++)
{
int l,r,val;
scanf("%d%d%d",&l,&r,&val);
update(1,l,r,val);
}
printf("Case %d: The total value of the hook is %d.\n",cas++,tree[1].sum);
}
return 0;
}

  

hdu1698 线段树区间更新的更多相关文章

  1. hdu1698线段树区间更新

    题目链接:https://vjudge.net/contest/66989#problem/E 坑爹的线段树照着上一个线段树更新写的,结果发现有一个地方就是不对,找了半天,发现是延迟更新标记加错了!! ...

  2. HDU1698 线段树(区间更新区间查询)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Spring MVC Restful Put方法无法获取参数值

    Spring MVC Restful 无法通过@ReqeustParam获取参数值 原因是Tomcat只支持POST/GET获取参数值,对于PUT这些方法需要通过HttpPutFormContentF ...

  2. Struts2之配置文件中Action的详细配置(续)

    承接上一篇 4.处理结果的配置 Action类的实例对象调用某个方法,处理完用户请求之后,将返回一个逻辑视图名的字符串.核心Filter收到返回的逻辑视图名字符串,根据struts.xml中的逻辑视图 ...

  3. Codeforces 837E. Vasya's Function

    http://codeforces.com/problemset/problem/837/E   题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...

  4. SpaceVim - 让你的vim变得更加高效和强大

    SpaceVim 中文手册 项 目 主 页: https://spacevim.org Github 地址 : https://github.com/SpaceVim/SpaceVim SpaceVi ...

  5. 解决java.lang.NoSuchMethodError:org.joda.time.DateTime.withTimeAtStartOfDay() Lorg/joda/time/DateTime

    问题:项目放在weblogic运行,报错 java.lang.NoSuchMethodError: org.joda.time.DateTime.withTimeAtStartOfDay()Lorg/ ...

  6. C语言Linix服务器网络爬虫项目(二)项目设计和通过一个http请求抓取网页的简单实现

    我们通过上一篇了解了爬虫具体要实现的工作之后,我们分析得出的网络爬虫的基本工作流程如下: 1.首先选取一部分精心挑选的种子URL: 2.将这些URL放入待抓取URL队列: 3.从待抓取URL队列中取出 ...

  7. node框架koa

    node的两大常见web服务器框架有express和koa,之前已经介绍过express了现在来介绍下koa吧~ koa也是express团队的出品,意在利用es7新出的async来告别"回 ...

  8. Linq 透明标识符

    IEnumerable<Person> list = new List<Person> { , Id = }, , Id = }, , Id = }, , Id = }, , ...

  9. python利用递归函数输出嵌套列表的每个元素

    1.先用 for 循环取. for item in l: if isinstance(item ,list): for newitem in item: print(newitem) else: pr ...

  10. hadoop2.6.0实践:000 虚拟机配置