Just a Hook

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.
题目大意:Hook有一个由n个扣子组成的长链,初始值都为铜质,求q次改变之后长链的总值,每一次改变可以将一个扣子改成银质或金质。
思路:即一个区间修改查询的线段树模板题啦,有q次的区间修改,最后查询一次1-n的总和即可
 
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio> using namespace std;
const int maxn = ;
int sum[maxn<<],add[maxn<<];
int n,q,T;
void pushup(int rt){
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt, int ln, int rn)
{
if(add[rt])
{
sum[rt << ] = ln * add[rt];
sum[rt << | ] = rn * add[rt];
add[rt << ] = add[rt];
add[rt << | ] = add[rt];
add[rt] = ;
}
return;
}
void build(int l,int r,int rt){
add[rt] = ;
if(l==r){
sum[rt] = ;
return ;
}
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
void update(int l,int r,int rt,int L,int R,int C)
{
if(L<=l&&r<=R){
sum[rt] = (r-l+)*C;
add[rt] = C;
return ;
}
int mid = (l+r)>>;
pushdown(rt,mid-l+,r-mid);
if(L<=mid)update(l,mid,rt<<,L,R,C);
if(R>mid)update(mid+,r,rt<<|,L,R,C);
pushup(rt);
}
int query(int l,int r,int rt,int L,int R)
{
if(L<=l&&r<=R)return sum[rt];
int mid = (l+r)>>;
pushdown(rt,mid-l+,r-mid);
int ans = ;
if(L<=mid) ans += query(l,mid,rt<<,L,R);
if(R>mid) ans += query(mid+,r,rt<<|,L,R);
return ans;
}
int main()
{
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%d",&n);
build(,n,);
scanf("%d",&q);
int l,r,x;
while(q--){
scanf("%d%d%d",&l,&r,&x);
update(,n,,l,r,x);
}
printf("Case %d: The total value of the hook is %d.\n",t,query(,n,,,n));
}
return ;
}

HDU 1698 Just a Hook (线段树模板题-区间求和)的更多相关文章

  1. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  2. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  3. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

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

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

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

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

  6. [HDU] 1698 Just a Hook [线段树区间替换]

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

  7. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

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

  8. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

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

  9. 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 ...

随机推荐

  1. Ajax--Ajax基于原生javascript:创建Ajax对象、链接服务器、发送请求、接受响应结果

    Ajax概述 异步:指某段程序执行时不会阻塞其它程序执行,其表现形式为程序的执行顺序不依赖程序本身的书写顺序,相反则为同步. 同步请求: 请求是由浏览器发送 页面会刷新 异步请求: 请求是由浏览器的一 ...

  2. django查看数据库

    #views import pymysql def get_date(request): conn = pymysql.connect( host='localhost', port=3306, us ...

  3. objectarx之画多段线和画直线

    void CCommonFuntion::DrowPloyLine(AcGePoint2dArray& inputpoints){ if (inputpoints.length() < ...

  4. Oracle函数——MINUS

    解释 “minus”直接翻译为中文是“减”的意思,在Oracle中也是用来做减法操作的,只不过它不是传统意义上对数字的减法,而是对查询结果集的减法.A minus B就意味着将结果集A去除结果集B中所 ...

  5. beanstalkd 启动跟停止

    启动命令: nohup /usr/bin/beanstalkd -l xxx.xxx.xxx.xxx -p 11300 & >> /dev/null 2>&1 正常启 ...

  6. postman 百度网盘下载 64位

    最近找了一下postman的下载资源,竟然发现有些用户的资源要用csdn的积分下载,很是不爽.所以才想到在这里贴出我的百度网盘的地址 下载地址: 链接:https://pan.baidu.com/s/ ...

  7. [java]struts2 模型驱动 2016-05-01 21:40 702人阅读 评论(19) 收藏

    一开始敲网上商城的时候,对于数据的传递方式我是很惊艳了一把的,感觉特别高大上,就感觉,竟然不用像.net一样取谁的值,给谁赋值这样,只要需要用的时候,简单的get一下就ok了,简直高大上啊. 然后发现 ...

  8. 2014年山东省第五届ACM大学生程序设计竞赛F题:Full Binary Tree

    题目描述 In computer science, a binary tree is a tree data structure in which each node has at most two ...

  9. 免费的容器架构可视化工具 | 阿里云应用高可用服务 AHAS 发布重大新特性

    工具下载链接:点这里.活动发布链接:点这里. 采用容器服务后,了解容器之间的关系及依赖是一个比较有挑战的问题.容器化改造后的实际架构模型可能与预想的架构存在较大的差异,架构师或系统运维人员需要精确地了 ...

  10. Quick BI 3.0 - 强大的多维分析表格:交叉表

    写在开头 对于普通的表格展示数据,相信大家都非常熟悉了,今天给大家介绍的是BI领域的分析利器-交叉表,这个在BI分析场景中使用占比最多的分析利器.通过交叉表对数据的承载和管理,用户可以一目了然地分析出 ...