hdu-------(1698)Just a Hook(线段树区间更新)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17124 Accepted Submission(s): 8547
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.
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.
10
2
1 5 2
5 9 3
成段更新(通常这对初学者来说是一道坎),需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候
代码:
#include<cstring>
#include<cstdio>
const int maxn=;
struct node
{
int lef,rig,sum;
int cnt;
int mid(){
return lef+(rig-lef>>);
}
}; node sac[maxn*]; void Build(int left,int right,int pos)
{
sac[pos]=(node){left,right,};
if(left==right)return ;
int mid=sac[pos].mid();
Build(left,mid,pos<<);
Build(mid+,right,pos<<|);
sac[pos].sum=sac[pos<<].sum+sac[pos<<|].sum;
}
void Update(int left,int right,int pos,int val)
{
if(left<=sac[pos].lef&&sac[pos].rig<=right){
sac[pos].sum=val*(sac[pos].rig-sac[pos].lef+);
sac[pos].cnt=val;
return ;
}
if(sac[pos].cnt!=){ //向下更新一次
sac[pos<<].sum=sac[pos].cnt*(sac[pos<<].rig-sac[pos<<].lef+);
sac[pos<<|].sum=sac[pos].cnt*(sac[pos<<|].rig-sac[pos<<|].lef+);
sac[pos<<|].cnt=sac[pos<<].cnt=sac[pos].cnt;
sac[pos].cnt=;
}
int mid=sac[pos].mid();
if(mid>=left)
Update(left,right,pos<<,val);
if(mid<right)
Update(left,right,pos<<|,val);
sac[pos].sum=sac[pos<<].sum+sac[pos<<|].sum;
}
int main()
{
int test,n,Q;
int a,b,c;
scanf("%d",&test);
for(int i=;i<=test;i++){
scanf("%d%d",&n,&Q);
Build(,n,);
while(Q--)
{
scanf("%d%d%d",&a,&b,&c);
Update(a,b,,c);
}
printf("Case %d: The total value of the hook is %d.\n",i,sac[].sum);
}
}
hdu-------(1698)Just a Hook(线段树区间更新)的更多相关文章
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
随机推荐
- 【转载】ODBC, OLEDB, ADO, ADO.Net的演化简史
原文:ODBC, OLEDB, ADO, ADO.Net的演化简史 1.演变历史 它们是按照这个时间先后的顺序逐步出现的,史前->ODBC->OLEDB->ADO->ADO.N ...
- 在CentOS下安装Redis
Redis比较傲娇,在windows上还没有很好的安装方式,不得已搞了个虚拟机玩玩. 装Redis十分简单,按照下面的几个命令来就行了. 安装命令 wget http://download.redis ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- python_way ,day1 编译安装python3、基础及流程控制
本节内容: 1,Python介绍发展史 2,安装 3,Hello World 4,程序 5,变量,字符编码 6,用户输入 7,模块初识 一.python介绍 python的创始人为吉多·范罗苏姆(Gu ...
- [转载] ORMs under the hood
原文: http://www.vertabelo.com/blog/technical-articles/orms-under-the-hood It often happens that if so ...
- mysql delimiter
默认情况下,mysql遇到分号; 就认为是一个命令的终止符, 就会执行命令.而有些时候,我们不希望这样,比如存储过程中包含多个语句,这些语句以分号分割,我们希望这些语句作为一个命令,一起执行,怎么解决 ...
- python利用xmlrpc方式对odoo数据表进行增删改查操作
# -*- encoding: utf-8 -*- import xmlrpclib #导入xmlrpc库,这个库是python的标准库. username ='admin' #用户登录名 pwd = ...
- 【Todo】pthread_key_t 和 pthread_once_t学习
这两个函数应该都是和线程局部变量有关的.有时间学习一下. 可以参考如下文章: <Linux线程私有数据pthread_key_t> <posix多线程有感--线程高级编程(pthre ...
- number-of-segments-in-a-string
https://leetcode.com/problems/number-of-segments-in-a-string/ package com.company; class Solution { ...
- Objective-C与C++的区别
1.两者的最大相同:都是从C演化而来的面相对象语言,两者都兼容标准C语言 2.两者的最大不同:Objective-C提供了运行期动态绑定机制,而C++是编译静态绑定,并且通过嵌入类(多重继承)和虚函数 ...