HDU 1698 Just a Hook (段树更新间隔)
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.
1
10
2
1 5 2
5 9 3
Case 1: The total value of the hook is 24.
题目意思:就是给一个n个位置(1~n),刚開始每个位置价值赋值为1,后来跟新一段的值 比如 1 5 2 表示把1~5的位置所有变成价值为2,最后输出1~n所有位置价值和
不好理解的地方是pushdown()函数。比如输入1,n 2。我标记1到n是一样的价值(f[pos].va=1),又输入1 3 1时,我须要吧
前面的2传递给4到n,再更新1~3
不懂就好好理解吧,都是那么苦逼过来的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 100005 int a[N]; struct stud{
int le,ri;
int cover;
int va;
}f[N*4]; void pushdown(int pos)
{
if(f[pos].cover)
{
f[L(pos)].va=f[R(pos)].va=f[pos].va;
f[L(pos)].cover=f[R(pos)].cover=1;
f[pos].cover=0;
}
}
void build(int pos,int le,int ri)
{
f[pos].le=le;
f[pos].ri=ri; f[pos].va=1;
f[pos].cover=1;
if(le==ri) return ; int mid=MID(le,ri); build(L(pos),le,mid);
build(R(pos),mid+1,ri);
} void update(int pos,int le,int ri,int va)
{
if(f[pos].le==le&&f[pos].ri==ri)
{
f[pos].va=va;
f[pos].cover=1;
return ;
} pushdown(pos); int mid=MID(f[pos].le,f[pos].ri);
if(mid>=ri)
update(L(pos),le,ri,va);
else
if(mid<le)
update(R(pos),le,ri,va);
else
{
update(L(pos),le,mid,va);
update(R(pos),mid+1,ri,va);
}
} int query(int pos)
{
if(f[pos].cover)
return f[pos].va*(f[pos].ri-f[pos].le+1); pushdown(pos); return query(L(pos))+query(R(pos));
} int main()
{
int n,m,i,t,ca=0;
scanf("%d",&t); while(t--)
{
scanf("%d",&n); build(1,1,n); scanf("%d",&m); int le,ri,va; while(m--)
{
scanf("%d%d%d",&le,&ri,&va);
update(1,le,ri,va);
} printf("Case %d: The total value of the hook is %d.\n",++ca,query(1));
}
return 0;
}
HDU 1698 Just a Hook (段树更新间隔)的更多相关文章
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- PKU A Simple Problem with Integers (段树更新间隔总和)
意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include& ...
- HDU 1698 Just a Hook(线段树成段更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- (简单) 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 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- firefox同步数据时无响应问题
之前设置了firefox的数据同步,可以在不同电脑上,同步自己的书签等信息,感觉很方便实用,最近在点工具立即同步时,不报错,书签也没有同步,没有任何响应: 后来查了许多网上资料,都不见效,无意间看到 ...
- 部署、收回和删除解决方式----STSADM和PowerShell
部署.收回和删除解决方式----STSADM和PowerShell 由于近期总是要部署wsp解决方式,所以常常要用到命令行或者PowerShell.所以有必要将命令集中放在这里.在部署 ...
- Linux C 编程内存泄露检測工具(二):memwatch
Memwatch简单介绍 在三种检測工具其中,设置最简单的算是memwatch,和dmalloc一样,它能检測未释放的内存.同一段内存被释放多次.位址存取错误及不当使用未分配之内存区域.请往http: ...
- FZU 1686(重复覆盖)
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31370 题意:用尽量少r*c的小矩形覆盖大矩形n*m中的所有1,将 ...
- Determining IP information for eth1... failed; no link present. Check cable? 解决办法
有时候会遇到这种问题 解决办法为 进入网卡配置,将 BOOTPROTO 改为 none 然后 ifconfig –a 查看可以得到 eth1 已经可以寻到 iP 地址,如下: Service netw ...
- Thinkphp中field和getField
在数据库查询操作中field和getField方法是使用最频繁的.可是两者是有一定差别的.在这里记录下. field方法是用于定义要查询的字段(支持字段排除). getField方法获取数据表中的某个 ...
- java IO流文件的读写具体实例
IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Output ...
- hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)
HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包 ...
- angularjs入门学习【指令篇】
一.首先我们来了解下指令API 属性 含义 restrict 申明标识符在模版中作为元素,属性,类,凝视或组合,怎样使用 priority 设置模版中相对于其它标识符的运行顺序 Template 指定 ...
- 【PhotoShop】采用PS让美丽的咖啡泡沫
稀土一杯咖啡,如何你不能击败张(常苦黑咖啡饮料实在受不了! ) 得到例如以下图 看着还不错,但是总感觉空空荡荡的,所以就拿来PS练手了.终于效果图例如以下: 以下讲下制作过程: 首先是给照片加下咖啡泡 ...