HDU1698(KB7-E 线段树)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34362 Accepted Submission(s): 16774
Problem Description
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
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
Sample Input
10
2
1 5 2
5 9 3
Sample Output
Source
//2017-08-11
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson (id<<1)
#define rson ((id<<1)|1)
#define mid ((tree[id].l+tree[id].r)>>1) using namespace std; const int N = ;
struct Node{
int l, r, sum, lazy;
}tree[N<<]; void build(int id, int l, int r){
tree[id].l = l;
tree[id].r = r;
tree[id].lazy = ;
if(l == r){
tree[id].sum = ;
return;
}
build(lson, l, mid);
build(rson, mid+, r);
tree[id].sum = tree[lson].sum + tree[rson].sum;
} void push_down(int id){
tree[id].sum = (tree[id].r-tree[id].l+)*tree[id].lazy;
tree[lson].lazy = tree[id].lazy;
tree[rson].lazy = tree[id].lazy;
tree[id].lazy = ;
} void update(int id, int l, int r, int val){
if(tree[id].lazy)push_down(id);
if(tree[id].l == l && tree[id].r == r){
tree[id].sum = (r-l+)*val;
tree[id].lazy = ;
tree[lson].lazy = val;
tree[rson].lazy = val;
return;
}
if(l > mid)update(rson, l, r, val);
else if(r <= mid)update(lson, l, r, val);
else{
update(lson, l, mid, val);
update(rson, mid+, r, val);
}
if(tree[lson].lazy)push_down(lson);
if(tree[rson].lazy)push_down(rson);
tree[id].sum = tree[lson].sum + tree[rson].sum;
} int query(int id, int l, int r){
if(tree[id].l == l && tree[id].r == r){
return tree[id].sum;
}
if(l > mid)return query(rson, l, r);
else if(r <= mid)return query(lson, l, r);
else return query(lson, l, mid)+query(rson, mid+, r);
} int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
build(, , n);
int m, l, r, z;
scanf("%d", &m);
while(m--){
scanf("%d%d%d", &l, &r, &z);
update(, l, r, z);
}
printf("Case %d: The total value of the hook is %d.\n", ++kase, query(, , n));
} return ;
}
HDU1698(KB7-E 线段树)的更多相关文章
- hdu1698 线段树区间更新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 线段树---成段更新hdu1698 Just a Hook
hdu1698 Just a Hook 题意:O(-1) 思路:O(-1) 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) 题意:给一组棍子染色,不同 ...
- HDU-1698 JUST A HOOK 线段树
最近刚学线段树,做了些经典题目来练手 Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 【线段树成段更新-模板】【HDU1698】Just a Hook
题意 Q个操作,将l,r 的值改为w 问最后1,n的sum 为多少 成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更 ...
- hdu1698 Just a Hook 线段树:成段替换,总区间求和
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem ...
- hdu1698(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) ...
- hdu1698线段树区间更新
题目链接:https://vjudge.net/contest/66989#problem/E 坑爹的线段树照着上一个线段树更新写的,结果发现有一个地方就是不对,找了半天,发现是延迟更新标记加错了!! ...
- hdu1698线段树的区间更新区间查询
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
随机推荐
- 异常处理,约束,MD5加密日志处理
程序运行过程中产生的错误, 不正常 def chufa(a, b): try: # 尝试执行xxx代码 ret = a/b # 如果这里出现了错误. 异常. 系统内部会产生一个异常对象. 系统会把这个 ...
- Dubbo实现原理之基于SPI思想实现Dubbo内核
dubbo中SPI接口的定义如下: @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) public ...
- python 多版本管理pyenv和virtualenv虚拟开发环境
pyenv是管理多个python版本的工具. 1.pyenv的安装 git clone https://github.com/yyuu/pyenv.git ~/.pyenv 2.将PYENV_ROOT ...
- cStringIO 实现指定大小的字符串缓存
StringIO经常被用来作为字符串的缓存,以下实现无论写入多少字符串,总能返回一个指定大小的缓存 from cStringIO import StringIO class CustomStringI ...
- python读取文件首行和最后一行
python读取文件最后一行两种方式 1)常规方法:从前往后依次读取 步骤:open打开文件. 读取文件,把文件所有行读入内存. 遍历所有行,提取指定行的数据. 优点:简单,方便 缺点:当文件大了以后 ...
- tomcat常见错误处理
1 .java.lang.IllegalArgumentException: Document base /XXX/tomcat/webapps/manager does not exist 解决方法 ...
- 利用Makefile安装helloworld模块(速成)
这学期对了一门操作系统,满怀着好奇装了虚拟机然后安了Ubuntu,这周作业是编译内核和安装个模块,妈耶,折腾了我一两天.终于弄完,CSDN上有挺多类似的教程,例如陈皓的跟我一起写Makefile,写的 ...
- 5_Python OOP
1. 实例属性和类属性 (1) 实例属性在构造函数__init__中定义,定义时以self作为前缀,只能通过实例名访问 (2) 类属性在类中方法之外单独定义,还可以在程序中 ...
- win10关机之后自动重启(系统更新之后出现这个问题)
最近更新了一把win10之后出现无法关机,关机之后直接又开机,无限循环状态.最近几天没空处理一直是强关笔记本下班的. 今天打了一把命令: shutdown /s /t 0 发现关机正常,本来打算整个脚 ...
- (转)面向对象(深入)|python描述器详解
原文:https://zhuanlan.zhihu.com/p/32764345 https://www.cnblogs.com/aademeng/articles/7262645.html----- ...