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是区间内连续 ...
随机推荐
- Linux 抓包命令
可使用如下命令,在Linux系统上进行抓包命令 tcpdump -n -i eth0 tcp port and host 192.168.9.45 -w ./filename.cap 说明: eth0 ...
- ffmpeg编译
CFLAGS=-g ./configure --enable-opengl --disable-yasm --enable-shared --enable-pic
- ElasticSearch(1)-入门
下一篇 Elastic Search基础(2) 相关文档: Gitbook[中文未完整]: http://learnes.net/ Gitbook[英文完整]:https://allen8807.gi ...
- Kettle jdbc连接hive出现问题
jdbc连接时报如下错误: Error connecting to database [k] : org.pentaho.di.core.exception.KettleDatabaseExcepti ...
- gcc-config: Active gcc profile is invalid解决办法
错误描述 Gentoo软件安装错误,提示: gcc-config: Active gcc profile is invalid 解决方法: 列出可用的profile gcc-config -l gcc ...
- ios书籍推荐
1.Objective-C Programming 内容不多, 却都是精华, 有了一点 C 语言基础可以快速阅读此书, 大概一天时间就可以看完, 看完后对 iOS 开发能够有个基本的印象. 2.iO ...
- PureLayout
PureLayout 是 iOS & OS X Auto Layout 的终极 API——非常简单,又非常强大.PureLayout 通过一个全面的Auto Layout API 扩展了 UI ...
- 神经网络工具箱nntool的使用方法
关于如何使用nntool神经网络工具箱进行“数据训练”的方法: 1. 在命令窗口键入nntool命令打开神经网络工具箱: 2. 点击Import按钮两次,分别把输入向量和目标输出加入到对应的窗口([I ...
- 进入BIOS SHELL DUMP 命令
LINUX系统 进入SHELL 输入命令 fs1: or fs0: 就进入了U盘目录 然后输入 ACPIRW.efi -d -s dsdt.bat 就会产生结果到U盘 ——————————————— ...
- hadoop namespace
As underscore(_) is not allowed. It may be the problem if your other configuration are ok. Your conf ...