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思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
随机推荐
- C++调用API获取当前时间
#include <string> #include<iostream> #include<windows.h> #include <sstream> ...
- jQuery基础(3)- ajax
一.jQuery的ajax 1.什么是ajax AJAX = 异步的javascript和XML(Asynchronous Javascript and XML). 简言之,在不重载整个网页的情况下, ...
- vscode 学习笔记 —— 重构
一.vscode 自带 1.提取变量 2.提取方法 上面都是通过选中文本后出现的小灯泡操作的: 3.全局替换(多个文件中的)某个变量 选中变量按 F2,输入完成后按回车 二.vscode 插件 js- ...
- JS简单表单验证
这里我是写了一个简单的注册表单验证功能,亲测有效,一起来看看吧! 首先我的HTML代码是这样的: class大家可以忽略一下,这里我项目使用的是bootstrap的样式. 输入用户名和密码用的是正则表 ...
- iOS UIFileSharingEnabled
一.让iOS App通过iTunes进行文件交换Documents 让iOS App通过iTunes进行文件交换 有一些App需要通过使用iTunes让用户上传和下载文档.要让iOS程序支持iTune ...
- CentOS6.8 安装 Oracle11.2.0.4
1. 安装操作系统 安装的时候选择中文+英文支持 注意分区: swap sda盘做系统盘 sdb盘做数据盘 配置完成后的服务器分区路径信息: [root@dbserver ~]# df -h File ...
- python面试(3)
一.语言 推荐一本看过最好的python书籍? 拉开话题好扯淡 谈谈python的装饰器,迭代器,yield? 标准库线程安全的队列是哪一个?不安全的是哪一个?logging是线程安全的吗? pyth ...
- 用鼠标键盘来控制你的Android手机——同屏显示简单教程
今天在微博上看到有人用电脑鼠标操作iPhone手机玩打飞机游戏,非常炫,虽然自己用的不是iPhone,但相信Android手机肯定也能实现这样的功能,于是网上各种搜索方法,终于看到了一篇试用成功的帖子 ...
- 通过DataTrigger绑定Tag属性值进行判断(.net 3.5的环境)
如下,在UserControl中通过Tag传递不同的值,然后在Style中使用DataTrigger进行判断, <UserControl Style="{DynamicResource ...
- MySQL进程-状态等参数详解
查看连接 mysql -uroot -p123456 -e "show processlist;" 查看正在执行的sql语句 mysql -uroot -p123456 -e &q ...