hdu 1698 (延迟标记+区间修改+区间求和)

时间:2021-06-28 10:20:13

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.

Source

2008 “Sunline Cup” National Invitational Contest

思路:第一个想法是对不同区间的不通材料的分别做判断求和,后来根据lazy标记可以发现,在标记下压的过程中其实已经给区间分类了,膜法

#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn=100000+100;
int sum[maxn<<2],add[maxn<<2];//lazy
void pushup(int rt) {//sum
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m) {
if(add[rt]) {
add[rt<<1]=add[rt<<1|1]=add[rt];
sum[rt<<1]=(m-(m>>1))*add[rt];
sum[rt<<1|1]=(m>>1)*add[rt];
add[rt]=0;
}
}
void build(int l,int r,int rt) {
add[rt]=0;
if(l==r) {
sum[rt]=1;
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushup(rt);
} void update(int c,int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) {
add[rt]=c;
sum[rt]=c*(r-l+1);
return;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m) update(c,L,R,l,m,rt<<1);
if(R>m) update(c,L,R,m+1,r,rt<<1|1);
pushup(rt);
}
int main() {
// freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++) {
int n;
scanf("%d",&n);
build(1,n,1);
int m;
scanf("%d",&m);
while(m--) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(c,a,b,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",i,sum[1]);
}
return 0;
}

hdu 1698 (延迟标记+区间修改+区间求和)的更多相关文章

  1. HDU 1698 【线段树,区间修改 &plus; 维护区间和】

    题目链接 HDU 1698 Problem Description: In the game of DotA, Pudge’s meat hook is actually the most horri ...

  2. Just a Hook &lpar;HDU 1698&rpar; 懒惰标记

    Just a Hook (HDU 1698) 题链 每一次都将一个区间整体进行修改,需要用到懒惰标记,懒惰标记的核心在于在查询前才更新,比如将当前点rt标记为col[rt],那么此点的左孩子和右孩子标 ...

  3. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  4. 【codevs1690】开关灯 线段树 区间修改&plus;区间求和(标记)

    [codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...

  5. 【codevs1690】开关灯 (线段树 区间修改&plus;区间求和 (标记))

    [codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...

  6. hdu 1698&plus;poj 3468 &lpar;线段树 区间更新&rpar;

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...

  7. 【树状数组区间修改区间求和】codevs 1082 线段树练习 3

    http://codevs.cn/problem/1082/ [AC] #include<bits/stdc++.h> using namespace std; typedef long ...

  8. 区间修改区间求和cdq分治

    https://www.luogu.org/problemnew/show/P3372 #include<bits/stdc++.h> #define fi first #define s ...

  9. bzoj2631 tree LCT 区间修改,求和

    tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 4962  Solved: 1697[Submit][Status][Discuss] Des ...

随机推荐

  1. 黄聪:Access-Control-Allow-Origin,JS跨域解决办法

    .htaccess添加下面代码: <IfModule mod_headers.c> Header add Access-Control-Allow-Origin "*" ...

  2. ShortestPath&colon;Wormholes&lpar;POJ 3259&rpar;

    田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...

  3. 笔记2:傻瓜式盗QQ程序

    1.一个简单的盗QQ号的方法 仿做一个QQ的窗体 PS:当然里面有用的控件只有两个输入框和一个登陆按钮,其他的尽量做得像一些吧! 点登陆时的后台代码: PS:需要导入两个包:using System. ...

  4. javaEE学习笔记-利用DOM4J解析xml至数据库

    xml代码文件名:test02.xml <ACCESOS> <item> <SOCIO> <NUMERO>00045050</NUMERO> ...

  5. javaweb css教程

    CSS 1.css的简介 * css: 层叠样式表 ** 层叠:一层一层的 ** 样式表: 很多的属性和属性值 * 是页面显示效果更加好 * CSS将网页内容和显示样式进行分离,提高了显示功能. 2. ...

  6. 【转】MFC中调试过程中查看输出信息 -- 不错

    原文网址:http://blog.sina.com.cn/s/blog_4e24d9c501014o39.html 笔记&&方便查阅. ~~~~~~~~~~~~~~~~~~~~~~~~ ...

  7. 给sftp创建新用户、默认打开和限制在某个目录

    一.环境: CentOS 6.8 使用 FileZilla 进行 sftp 连接 二.背景 给外包的工作人员提供我司服务器的某一目录的访问(包括读写)权限,方便他们部署代码文件. 之所以是某一目录的访 ...

  8. NIO学习

    1. NIO客户端与服务端网络编程关键: 理解各个监听事件的驱动事件,总结以下几点: (1)ServerSocketChannel注册了OP_ACCEPT事件,需要客户端发起连接请求,服务端selec ...

  9. Xamarin 学习笔记 - Layout(布局)

    本文翻译自CodeProject文章:https://www.codeproject.com/Articles/1227733/Xamarin-Notes-Xamarin-Forms-Layouts ...

  10. ServiceMesh究竟解决什么问题?

    服务网格(ServiceMesh)这两年异常之火,号称是下一代微服务架构,互联网公司经常使用的是微服务分层架构. 随着数据量不断增大,吞吐量不断增加,业务越来越复杂,服务的个数会越来越多,分层会越来越 ...