hdu1698 Just a Hook (线段树区间更新 懒惰标记)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22730 Accepted Submission(s): 11366
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.
pid=1394" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1394
pid=2795" target="_blank" style="color:rgb(26,92,200); text-decoration:none">2795
pid=1255" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1255
1828Statistic | Submit | Discuss | pid=1698" style="color:rgb(26,92,200); text-decoration:none">Note
更新的时候不要直接更新究竟部,会超时的。不用尝试。
。我已经当了小白鼠。
。
要用懒惰标记..就是更新的时候仅仅更新到区间。同一时候lazy[]数组标记一下。假设下次更新的时候用到了上次已经更新的点
在把上次lazy[]数组标记的往下更新
想感叹。。做ACM的 太累了。。你做对还不行 还要不能超时
#include <stdio.h>
struct node
{
int left,right,val,lazy,tag;
}c[100000*3];
void build_tree(int l,int r,int root)
{
c[root].left=l;
c[root].right=r;
c[root].lazy=0;
c[root].tag=0;
if(l==r)
{
c[root].val=1;
return ;
}
int mid=(c[root].left+c[root].right)/2;
build_tree(l,mid,root*2);
build_tree(mid+1,r,root*2+1);
c[root].val=c[root*2].val+c[root*2+1].val;
}
void update_tree(int l,int r,int x,int root)
{
if(c[root].left==l&&r==c[root].right)
{
c[root].val=(r-l+1)*x;
c[root].lazy=1;
c[root].tag=x;
return ;
}
int mid=(c[root].left+c[root].right)/2;
if(c[root].lazy==1)
{
c[root].lazy=0;
update_tree(c[root].left,mid,c[root].tag,root*2);
update_tree(mid+1,c[root].right,c[root].tag,root*2+1);
c[root].tag=0;
}
if(mid<l)
update_tree(l,r,x,root*2+1);
else if(mid>=r)
update_tree(l,r,x,root*2);
else
{
update_tree(l,mid,x,root*2);
update_tree(mid+1,r,x,root*2+1);
}
c[root].val=c[root*2].val+c[root*2+1].val;
}
int main()
{
int ncase,n,k;
scanf("%d",&ncase);
for(int t=1;t<=ncase;t++)
{
scanf("%d",&n);
build_tree(1,n,1);
scanf("%d",&k);
while(k--)
{
int a,b,x;
scanf("%d %d %d",&a,&b,&x);
update_tree(a,b,x,1);
}
printf("Case %d: The total value of the hook is %d.\n",t,c[1].val);
}
return 0;
}
hdu1698 Just a Hook (线段树区间更新 懒惰标记)的更多相关文章
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- hdu1698 Just a hook 线段树区间更新
题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...
- hdu-------(1698)Just a Hook(线段树区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【HDU 4614】Vases and Flowers(线段树区间更新懒惰标记)
题目0到n-1的花瓶,操作1在下标a开始插b朵花,输出始末下标.操作2清空[a,b]的花瓶,求清除的花的数量.线段树懒惰标记来更新区间.操作1,先查询0到a-1有num个空瓶子,然后用线段树的性质,或 ...
- 扶桑号战列舰 (单调栈+线段树区间更新懒惰标记 or 栈)
传送门 •题目描述 题目描述 众所周知,一战过后,在世界列强建造超无畏级战列舰的竞争之中,旧日本海军根据“个舰优越主义”,建造了扶桑级战列舰,完工时为当时世界上武装最为强大的舰只. 同时,扶桑号战列舰 ...
- 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 , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- HDU1698:Just a Hook(线段树区间更新)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
随机推荐
- [BZOJ1853][Scoi2010]幸运数字 容斥+搜索剪枝
1853: [Scoi2010]幸运数字 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 3202 Solved: 1198[Submit][Status ...
- 山东省第六届省赛 H题:Square Number
Description In mathematics, a square number is an integer that is the square of an integer. In other ...
- ChannelFactory
通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了.我们不禁感叹WCF模型的简单,寥寥数行代码和配置,就可以把通信建立起 ...
- ZCMU训练赛-A(模拟)
A - Applications https://vjudge.net/contest/174208#overview Recently, the ACM/ICPC team of Marjar Un ...
- 使用 Hibernate 完成 HibernateUtils 类 (适用于单独使用Hibernate或Struts+Hibernate)
package com.istc.Utilities; import org.hibernate.Session; import org.hibernate.SessionFactory; impor ...
- 整数快速乘法/快速幂+矩阵快速幂+Strassen算法
快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c 二.矩 ...
- Carthage 不知不觉用到了
Carthage 是什么? 这个问题我们简单点一句话总结: 你知道什么事CocoaPods吧,我相信只要是做iOS的都知道,Carthage 的属性和作用和我们常用的CocoaPods都是一样的,这样 ...
- 【状压基础题】poj3254 Corn Fields
题目大意 :农夫约翰有n*m块地,其中一些地荒掉了.玉米是一种傲娇的植物,种在相邻的地里会导致不孕不育.求所有种法数对100000000求余. 读入:第一行一个n一个m, 接下来是一个n行m列的矩形, ...
- Dfs【p1454】 圣诞夜的极光
题目描述-->p1454 圣诞夜的极光 题意概括: 寻找联通块数量,这里的连通块定义与其他的不同. 这里定义为曼哈顿距离不超过2的都属于一个联通块. 什么?不知道曼哈顿距离是啥? 曼哈顿距离简易 ...
- sed 很棒的介绍
选项与参数:-n :使用安静(silent)模式.在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上.但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作) ...