HDU 1698 <线段树,区间set>
题意:
一条长为N的铜链子,每个结点的价值为1。有两种修改,l,r,z;
z=2:表示把[l,r]区间内链子改为银质,价值为2.
z=3:表示把[l,r]区间内链子改为金质,价值为3.
思路:
线段树,区间重设,求和。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100000+10;
struct node
{
int l;
int r;
int v;//标记该区间内的链条的属性,-1代表有杂色,1,2,3代表为纯色,并且属性值为;
};
node tree[maxn*4];
void maketree(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
tree[i].v=1;起始为铜
if(r==l)
return ;
int mid=(l+r)>>1;
maketree(i<<1, l, mid);
maketree(i<<1|1, mid+1, r);
}
void update(int i,int l,int r,int v)
{
if(tree[i].v==v) //如果该区域内值一样则不用修改,以保证tree[i].l<=l,r<=tree[i].r;
return ;
if(tree[i].l==l&&tree[i].r==r)//如果区间与change区间一样则直接将tree[i].v=v;
{
tree[i].v=v;
return ;
}
if(tree[i].v!=-1)//修改区间不一致,并且为纯色则,向下延伸
{
tree[i<<1].v=tree[i<<1|1].v=tree[i].v;//颜色向下分配
tree[i].v=-1;//代表杂色
}
int mid=(tree[i].l+tree[i].r)>>1;
if(l>=mid+1)
update(i<<1|1, l, r, v);
else if(r<=mid)
update(i<<1, l, r, v);
else
{
update(i<<1, l, mid, v);
update(i<<1|1, mid+1,r,v);
}
}
int sum(int i)
{
if(tree[i].v!=-1)//叶子节点一定是纯色。
return (tree[i].r-tree[i].l+1)*tree[i].v;
return sum(i<<1)+sum(i<<1|1);
}
int main ()
{
int T;scanf("%d",&T);
int k=1;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
maketree(1,1,n);
int l,r,v;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&l,&r,&v);
update(1, l, r, v);
}
printf("Case %d: The total value of the hook is %d.\n",k++,sum(1));
}
return 0;
}
HDU 1698 <线段树,区间set>的更多相关文章
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU - 1698 线段树区间修改,区间查询
这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...
- Hdu 1698(线段树 区间修改 区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- HDU(1698),线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...
- HDU 1698 (线段树 区间更新) Just a Hook
有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...
- hdu 1698 线段树 区间修改
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...
- Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
随机推荐
- strpos 返回0时 ,比较false 不能加单引号
$a = 'a.approve'; $num = strpos($a,'a.admin'); if(strpos($a,'a.approve') !== 'false'){ //不能加单引号.变字符 ...
- openwrt启动过程(脚本)
来源: http://wiki.openwrt.org/doc/techref/preinit_mount#first.boot 基本的openwrt启动顺序为: 1.boot loader loa ...
- Linux(power服务器)中kettle(2)
Hadoop集群硬件环境 4台机器 ip地址 172.16.1.131 172.16.1.132 172.16.1.133 172.16.1.134 每台内存16G 8核cpu 直接使用报错:
- 大规模Schedule任务实现方案
package com.itlong.bjxizhan.support.web.job.base; import com.itlong.bjxizhan.common.DbContext; impor ...
- 浅谈SharePoint 2013 站点模板开发 转载自http://www.cnblogs.com/jianyus/p/3511550.html
一直以来所接触的SharePoint开发,都是Designer配合Visual Studio,前者设计页面,后者开发功能,相互合作,完成SharePoint网站开发.直到SharePoint 2013 ...
- rabbitmq——镜像队列
转自:http://my.oschina.net/hncscwc/blog/186350?p=1 1. 镜像队列的设置 镜像队列的配置通过添加policy完成,policy添加的命令为: rabbit ...
- 阿里云ECOS 集群方案
转载 https://it.toggle.cn/article_detail/7e6f674b2564d6c319f807b4fda87eac.html 架构说明 前端由阿里云SLB统一分发Web请求 ...
- A - 小彭玉的扫荡食堂计划
A - 小彭玉的扫荡食堂计划 Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- C# 调用C++ DLL 的类型转换
//C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byt ...
- wpf之ListBox中ListBoxItem横向排列
ListBox中ListBoxItem默认是纵向排列,可以通过自定义样式,让其横向排列, 如下Demo: XAML: <Window x:Class="ListBoxItemStyle ...