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

11021 5 25 9 3
 
 Sample Output

Case 1: The total value of the hook is 24.

思路:

区间覆盖,还是用lazy标签,没看到最后一个句点疯狂WA

代码:

#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#define ll long long
const int N=100005;
const int MOD=20071027;
using namespace std;
int node[N<<2],lazy[N<<2];
void push_down(int rt,int l,int r){
int m=(l+r)/2;
if(lazy[rt]!=-1){
node[rt<<1]=lazy[rt]*(m-l+1);
node[rt<<1|1]=lazy[rt]*(r-(m+1)+1);
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
lazy[rt]=-1;
}
}
void update(int rt,int l,int r,int L,int R,int v){ //l r搜索区间
if(L<=l && R>=r){
node[rt]=v*(r-l+1);
lazy[rt]=v;
return;
}
push_down(rt,l,r);
int m=(l+r)/2;
node[rt]=0;
if(L<=m){
update(rt<<1,l,m,L,R,v);
}
if(R>m){
update(rt<<1|1,m+1,r,L,R,v);
}
node[rt]+=node[rt<<1]+node[rt<<1|1];
} int main(){
int n,q,a,b,c,T,num=1;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(node,-1,sizeof(node));
int len=n<<2;
for(int i=0;i<len;i++){
lazy[i]=1;
}
scanf("%d",&q);
node[1]=n;
while(q--){
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n",num++,node[1]); }
return 0;
}

HDU1698 Just a Hook(线段树&区间覆盖)题解的更多相关文章

  1. 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

    学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...

  2. hdu1698 Just a hook 线段树区间更新

    题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...

  3. HDU1698 Just a Hook —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...

  4. hdu1698 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(线段树区间更新)

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

  6. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

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

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

  8. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  9. [NOI2015] 软件包管理器【树链剖分+线段树区间覆盖】

    Online Judge:Luogu-P2146 Label:树链剖分,线段树区间覆盖 题目大意 \(n\)个软件包(编号0~n-1),他们之间的依赖关系用一棵含\(n-1\)条边的树来描述.一共两种 ...

  10. Mayor's posters POJ - 2528 线段树区间覆盖

    //线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...

随机推荐

  1. Python命令行解析库argparse(转)

    原文:http://www.cnblogs.com/linxiyue/p/3908623.html 2.7之后python不再对optparse模块进行扩展,python标准库推荐使用argparse ...

  2. Android开发之改动屏幕方向

    有的场景下.我们须要把手机屏幕方向改变,以下是我写的一个样例. xml页面文件: <RelativeLayout xmlns:android="http://schemas.andro ...

  3. centos 基础修改文件权限

    在centos 下 nginx 默认用户是user = apachegroup = apache 所以需要更改文件和文件夹权限时候需要满足apache用户才能进行 常用方式: $ chmod Runt ...

  4. 十天精通CSS3(11)

    Media Queries——媒体类型(一) 随着科学技术不断的向前发展,网页的浏览终端越来越多样化,用户可以通过:宽屏电视.台式电脑.笔记本电脑.平板电脑和智能手机来访问你的网站.尽管你无法保证一个 ...

  5. Android APP安装后不在桌面显示图标的应用场景举例和实现方法

    最近在为公司做一款车联网的产品,由于公司本身擅长于汽车解码器的研发,所以该产品的诊断功能的实现除了使用目前市面上车联网产品中大量使用的OBD协议外,还会使用一些专车专用协议去实现一些特殊的诊断功能,如 ...

  6. Bootstrap 网格系统(Grid System)的工作原理 - 媒体查询

    媒体查询 媒体查询是非常别致的"有条件的 CSS 规则".它只适用于一些基于某些规定条件的 CSS.如果满足那些条件,则应用相应的样式. Bootstrap 中的媒体查询允许您基于 ...

  7. 使用nvm管理node版本时,各个版本下公用npm安装的插件问题

    因为使用了NVM(node版本管理工具),所以在切换node版本的时候安装的插件不能共享使用,必须重新安装,导致不必要的工作量 所以我将npm(node包管理工具提取出来) 进行node版本之间的共享 ...

  8. html select 和dropdownlist小结收集

    //html select var x = $("#selectSort").val();  //获取选中的value值 获取select选中的索引: $("#selec ...

  9. Winter Storm Warning

    2019-01-02 11:22:33 ...WINTER STORM WARNING REMAINS IN EFFECT UNTIL 6 AM AKST TUESDAY... * WHAT...He ...

  10. PHP获取http头信息

    PHP手册提供了现成的函数: getallheaders (PHP 4, PHP 5) getallheaders — Fetch all HTTP request headers 说明 array ...