hdoj 1698 Just a Hook【线段树区间修改】
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23137 Accepted Submission(s):
11600
most horrible thing for most of the heroes. The hook is made up of several
consecutive metallic sticks which are of the same length.
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.
line of the input is the number of the cases. There are no more than 10
cases.
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.
the total value of the hook after the operations. Use the format in the
example.
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 100100
#define INF 0x3f3f3f
using namespace std;
int sum[MAX<<2];
int change[MAX<<2];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void pushdown(int o,int m)
{
if(change[o])
{
change[o<<1]=change[o<<1|1]=change[o];
sum[o<<1]=change[o]*(m-(m>>1));
sum[o<<1|1]=change[o]*(m>>1);
change[o]=0;
}
}
void gettree(int o,int l,int r)
{
sum[o]=1;change[o]=0;
if(l==r)
return ;
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int L,int R,int v)
{
if(L<=l&&R>=r)
{
change[o]=v;
sum[o]=v*(r-l+1);
return ;
}
pushdown(o,r-l+1);
int mid=(r+l)>>1;
if(L<=mid)
update(o<<1,l,mid,L,R,v);
if(R>mid)
update(o<<1|1,mid+1,r,L,R,v);
pushup(o);
}
int find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
{
return sum[o];
}
pushdown(o,r-l+1);
int ans=0;
int mid=(r+l)>>1;
if(L<=mid)
ans+=find(o<<1,l,mid,L,R);
if(R>mid)
ans+=find(o<<1|1,mid+1,r,L,R);
return ans;
}
int main()
{
int t,k,i,j;
int n,m;
k=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int a,b,c;
gettree(1,1,n);
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,c);
}
printf("Case %d: The total value of the hook is ",k++);
printf("%d.\n",find(1,1,n,1,n));
}
return 0;
}
hdoj 1698 Just a Hook【线段树区间修改】的更多相关文章
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- (简单) 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 [线段树区间替换]
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 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU1698Just a Hook(线段树 + 区间修改 + 求和)
题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)
题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...
随机推荐
- 使用localstorage及js模版引擎 开发 m站设想
目前 m站开发的方式,依然请求完整的html,这样造成的问题就是每次请求的数据量过大过多,在没有wifi的情况下,导致页面打开的速度很慢,耗费的流量也较多:访问m站的多是移动端设备,其浏览器的版本都较 ...
- thinkphp+mysql+bootstrap
#thinkphp+mysql+bootstrapthinkphp3.2.3,bootstrap V3一个简易的企业cms网站系统,只要将clients的host改为对应的域名即可.thinkphp. ...
- TDirectory.GetCreationTime、TDirectory.SetCreationTime获取和设置文件夹创建时间
使用函数: System.IOUtils.TDirectory.GetCreationTime//获取创建时间 System.IOUtils.TDirectory.SetCreationTime//设 ...
- JBPM4中常用概念总结
1. 流程定义(Process Definition) 流程定义是记录在xml文件中的对流程的描述,它包含唯一的流程开始节点和多个流程功能节点,每个节点之间使用transition进行连接. P ...
- wpf RadioButton控件的一个bug,onpropertychanged后会修改旧属性的值
测试代码下载:http://files.cnblogs.com/djangochina/RadioButtonBug.zip 从上面列表选择不同的行,再设置下面不同的radiobutton看看结果 b ...
- c# .net使用SqlDataReader注意的几点
转自:http://blog.knowsky.com/258608.htm 1.当SqlDataReader没有关闭之前,数据库连接会一直保持open状态,所以在使用SqlDataReader时,使用 ...
- EntityFramework动态组合多排序字段
前言:在使用EF当中,肯定会遇到动态查询的需求,建立一个公共调用的动态组合表达式查询也是必不可少的,以下是建立动态组合多排序字段做个记录,供以后调用 1.建立一个结构,用于多个排序字段组合,这个结构体 ...
- PHP获得文件的md5并检验是否被修改
由于需要判断上传的文件是否被修改过,需要记录上传文件的md5值,所以这里说一下一下获取文件md5值的方法. md5_file() md5_file() 函数计算文件的 MD5 散列.md5() 函 ...
- MySQL中的max_connections和max_user_connections 及 MySQL服务器最大连接数的合理设置
max_connections 是指整个mysql服务器的最大连接数: max_user_connections 是指每个数据库用户的最大连接数,比如:虚拟主机可以用这个参数控制每个虚拟主机用户的数据 ...
- 意犹未尽而来的第一篇Android 逆向
游戏:咕噜王国大冒险 平台:android 目标: 1. 去除乱七八糟提示(本篇目标) 2. 去除google弹窗 3. 破解“all stages” 破文开始: 1. 使用APKIDE反编译:搜索字 ...