题目连接

题意:

一条长为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>的更多相关文章

  1. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  2. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  3. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

  5. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  6. HDU(1698),线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...

  7. HDU 1698 (线段树 区间更新) Just a Hook

    有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...

  8. hdu 1698 线段树 区间修改

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

  9. Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  10. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

随机推荐

  1. 理解WebKit和Chromium(电子书)

    前言   基础篇 WebKit, WebKit2, Chromium和Chrome介绍 WebKit和Blink WebKit和Chromium代码目录结构介绍 WebKit和Chromium功能模块 ...

  2. Linux中kettle连接hadoop并传数据(5)

    http://wiki.pentaho.com/display/BAD/Loading+Data+into+HDFS 新建job

  3. Cross Product

    Cross Product These are two vectors: They can be multiplied using the "Cross Product" (als ...

  4. python中的矩阵运算

    摘自:http://m.blog.csdn.net/blog/taxueguilai1992/46581861 python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入nu ...

  5. ASP.NET之.NET FrameWork框架

    .NET FrameWork框架 是一套应用程序开发框架,主要目的提供一个开发模型. 主要的两个组件: 公共语言运行时(Common Language Runtime)(CLR): 提供内存管理.线 ...

  6. power oj/2360/Change

    题目链接[https://www.oj.swust.edu.cn/problem/show/2360] 题意:给出两个四位数A.B,目地是用最少的步骤使A变成B.变换规则如下:1.相邻的两位数可以交换 ...

  7. 正则表达式判断ip地址

    html: <div class="configuration"><form action="" name="myformcon&q ...

  8. java中Class对象详解

    java中把生成Class对象和实例对象弄混了,更何况生成Class对象和生成instance都有多种方式.所以只有弄清其中的原理,才可以深入理解.首先要生成Class对象,然后再生成Instance ...

  9. CentOS挂载硬盘

    1.查看当前硬盘使用状况: [root@gluster_node1 ~]# df -h 文件系统     容量 已用 可用 已用%% 挂载点 /dev/sda3 14G 2.4G 11G 19% / ...

  10. ASP.NET弹出提示点击确定之后再跳转页面的方法

    //ASP.NET弹出提示点击确定之后再跳转页面的方法 //弹出了提示并且通过location.href转到了DeskTop.aspx页面 Response.Write("<scrip ...